From e5d2b6886fbb237b3b02e4dfc812c5a7a00fe370 Mon Sep 17 00:00:00 2001 From: superhorse622 Date: Mon, 25 Sep 2023 20:02:26 -0400 Subject: [PATCH] time zone fix --- Chating.py | 8 ++++---- app.py | 43 +++++++++++++++++++++++-------------------- getContents.py | 11 ++++++----- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/Chating.py b/Chating.py index f04a8f2..34f2ae8 100644 --- a/Chating.py +++ b/Chating.py @@ -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 @@ -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: diff --git a/app.py b/app.py index a615f6b..db819c3 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,7 @@ import json import math import datetime +from multiprocessing import Process import threading import schedule import pytz @@ -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) @@ -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')) @@ -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) @@ -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']) @@ -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() diff --git a/getContents.py b/getContents.py index 237acb8..7552cf4 100644 --- a/getContents.py +++ b/getContents.py @@ -1,5 +1,6 @@ import math import time +import pytz import requests import asyncio import json @@ -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): @@ -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): @@ -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}"