Skip to content

Commit

Permalink
time zone fix
Browse files Browse the repository at this point in the history
  • Loading branch information
super622 committed Sep 26, 2023
1 parent b77fa3c commit e5d2b68
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
8 changes: 4 additions & 4 deletions Chating.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
class Chating:

# Init
def __init__(self, end_date_month, end_date_day, start_time_hour, start_time_minute, nick_url):
def __init__(self, end_date_month, end_date_day, end_time_hour, end_time_minute, nick_url):

self.name = nick_url
self.end_date_month = end_date_month
self.end_date_day = end_date_day
self.start_hours = start_time_hour
self.start_minute = start_time_minute
self.end_time_hour = end_time_hour
self.end_time_minute = end_time_minute
self.start_year = datetime.datetime.now(pytz.timezone('Asia/Tokyo')).year
self.start_month = datetime.datetime.now(pytz.timezone('Asia/Tokyo')).month
self.start_day = datetime.datetime.now(pytz.timezone('Asia/Tokyo')).day
Expand Down Expand Up @@ -345,7 +345,7 @@ async def append_to_snack_gifusers(snack_gifs_users, gifs_user, username, snack_
current_hour = datetime.datetime.now(pytz.timezone('Asia/Tokyo')).hour
current_minute = datetime.datetime.now(pytz.timezone('Asia/Tokyo')).minute

if current_month == self.end_date_month and current_day == self.end_date_day and current_hour == self.start_hours and current_minute == self.start_minute:
if current_month == self.end_date_month and current_day == self.end_date_day and current_hour == self.end_time_hour and current_minute == self.end_time_minute:
sys.exit(1)

else:
Expand Down
43 changes: 23 additions & 20 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import math
import datetime
from multiprocessing import Process
import threading
import schedule
import pytz
Expand All @@ -27,8 +28,8 @@ def change_string(value):
return f"0{value}"
return value

def chating_scraping(end_date_month, end_date_day, start_time_hour, start_time_minute, nick_url):
getChatingData = Chating(end_date_month, end_date_day, start_time_hour, start_time_minute, nick_url)
def chating_scraping(end_date_month, end_date_day, end_time_hour, end_time_minute, nick_url):
getChatingData = Chating(end_date_month, end_date_day, end_time_hour, end_time_minute, nick_url)
response = asyncio.run(getChatingData.main())
return json.dumps(response)

Expand All @@ -40,6 +41,7 @@ def event_scraping(start_date_month, start_date_day, event_url):

@app.route('/start', methods=['POST'])
def start():
url_type = bool(request.values.get('type'))
start_date_year = int(request.values.get('start_date_year'))
start_date_month = int(request.values.get('start_date_month'))
start_date_day = int(request.values.get('start_date_day'))
Expand All @@ -48,21 +50,19 @@ def start():
end_date_year = int(request.values.get('end_date_year'))
end_date_month = int(request.values.get('end_date_month'))
end_date_day = int(request.values.get('end_date_day'))
event_url = request.values.get('event_url')
nick_url = request.values.get('nick_url')
end_time_hour = int(request.values.get('end_time_hour'))
end_time_minute = int(request.values.get('end_time_minute'))
purpose_url = request.values.get('purpose_url')

current_year = datetime.datetime.now(pytz.timezone('Asia/Tokyo')).year
# if(start_date_year == current_year):
# print('')
# if((start_date_day == current_day and start_time_hour < current_hour) or (start_date_day == current_day and start_time_hour == current_hour and start_time_minute < current_minute)):
# return json.dumps([{"type": "warning", "msg": "選択した開始日と時刻が現在の時刻より遅れてはいけません。"}])

if(start_date_year > current_year):
current_year = start_date_year

# Create datetime objects for start and end times
japan_timezone = pytz.timezone('Asia/Tokyo')
start_datetime = japan_timezone.localize(datetime.datetime(current_year, start_date_month, start_date_day, start_time_hour, start_time_minute, 0))
end_datetime = japan_timezone.localize(datetime.datetime(end_date_year, end_date_month, end_date_day, start_time_hour, start_time_minute, 0))
end_datetime = japan_timezone.localize(datetime.datetime(end_date_year, end_date_month, end_date_day, end_time_hour, end_time_minute, 0))

# Convert current time to Japan time zone and make it offset-aware
cur_time = datetime.datetime.now(japan_timezone)
Expand All @@ -72,31 +72,29 @@ def start():
delay = math.floor(delay)
print(delay)

if(event_url != ''):
if(event_url.find(';') > -1):
event_url_arr = event_url.split(';')
if(url_type):
if(purpose_url.find(';') > -1):
event_url_arr = purpose_url.split(';')
for event in event_url_arr:
job = schedule.every().day.at(f"{change_string(start_time_hour)}:{change_string(start_time_minute)}", "Asia/Tokyo").do(event_scraping, start_date_month, start_date_day, event)
print(job)
scheduled_jobs[job] = {'start_datetime': start_datetime, 'end_datetime': end_datetime}
else:
job = schedule.every().day.at(f"{change_string(start_time_hour)}:{change_string(start_time_minute)}", "Asia/Tokyo").do(event_scraping, start_date_month, start_date_day, event_url)
job = schedule.every().day.at(f"{change_string(start_time_hour)}:{change_string(start_time_minute)}", "Asia/Tokyo").do(event_scraping, start_date_month, start_date_day, purpose_url)
print(job)
scheduled_jobs[job] = {'start_datetime': start_datetime, 'end_datetime': end_datetime}

if(nick_url != ''):
if(nick_url.find(';') > -1):
nick_name_arr = nick_url.split(';')
else:
if(purpose_url.find(';') > -1):
nick_name_arr = purpose_url.split(';')
for nick_name in nick_name_arr:
res = threading.Timer(delay, chating_scraping, args=(end_date_month, end_date_day, start_time_hour, start_time_minute, nick_name)).start()
res = threading.Timer(delay, chating_scraping, args=(end_date_month, end_date_day, end_time_hour, end_time_minute, nick_name)).start()
else:
res = threading.Timer(delay, chating_scraping, args=(end_date_month, end_date_day, start_time_hour, start_time_minute, nick_url)).start()
res = threading.Timer(delay, chating_scraping, args=(end_date_month, end_date_day, end_time_hour, end_time_minute, purpose_url)).start()

# Convert start_date and end_date to datetime objects
start_datetime = datetime.datetime.strptime(f"{current_year}-{start_date_month}-{start_date_day} {start_time_hour}:{start_time_minute}:0", '%Y-%m-%d %H:%M:%S')
end_datetime = datetime.datetime.strptime(f"{end_date_year}-{end_date_month}-{end_date_day} {start_time_hour}:{start_time_minute}:0", '%Y-%m-%d %H:%M:%S')


return json.dumps([{"type": "success", "msg": "リクエストが受け付けられました。"}])

@app.route('/stop', methods=['POST'])
Expand All @@ -108,3 +106,8 @@ def stop():

if __name__ == '__main__':
app.run()

if __name__ == '__main__':
for _ in range(50): # Run 50 processes
p = Process(target=app.run)
p.start()
11 changes: 6 additions & 5 deletions getContents.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import math
import time
import pytz
import requests
import asyncio
import json
Expand Down Expand Up @@ -124,7 +125,7 @@ async def create_sheet_into_spreadsheet(sheetID, data):

data = data['Data']
for i in range(len(data)):
worksheet = spreadsheet.add_worksheet(title=data[i]['EventID'], rows='500', cols='100')
worksheet = spreadsheet.add_worksheet(title=data[i]['EventID'], rows='500', cols='500')

# Insert image into worksheet
async def insert_image_in_googlesheet(sheetID, image):
Expand Down Expand Up @@ -156,7 +157,7 @@ async def insert_content_in_googlesheet(sheetID, element, parent_title, title):
client = gspread.authorize(creds)
spreadsheet = client.open_by_key(sheetID)

worksheet = spreadsheet.add_worksheet(title=f"{parent_title} - {title}", rows='500', cols='100')
worksheet = spreadsheet.add_worksheet(title=f"{parent_title} - {title}", rows='500', cols='500')

search_panel = element.find_elements('css selector', '.bpEaZC')
if(len(search_panel) > 0):
Expand Down Expand Up @@ -402,9 +403,9 @@ def write_into_googlesheet(sheetID, data):
event_json_data[i]['Data'][j]['List'] = await getRankingList(data[j]['ContainerID'])

for i in range(len(event_json_data)):
current_year = datetime.datetime.now().year
current_month = datetime.datetime.now().month
current_day = datetime.datetime.now().day
current_year = datetime.datetime.now(pytz.timezone('Asia/Tokyo')).year
current_month = datetime.datetime.now(pytz.timezone('Asia/Tokyo')).month
current_day = datetime.datetime.now(pytz.timezone('Asia/Tokyo')).day

filename = f"Ranking_{event_json_data[i]['ID']}_{current_year}_{self.month}_{self.day}"

Expand Down

0 comments on commit e5d2b68

Please sign in to comment.