From a74ae2a52ec9f37d3a38a032eae899f35072d0e3 Mon Sep 17 00:00:00 2001 From: RussellDash332 Date: Sat, 9 Nov 2024 16:24:33 +0800 Subject: [PATCH] Add cron workflow --- .github/workflows/deluxe.yml | 5 ++- .github/workflows/pancake.yml | 5 ++- main.py | 64 +++++++++++++++-------------------- telegram.py | 6 +--- 4 files changed, 32 insertions(+), 48 deletions(-) diff --git a/.github/workflows/deluxe.yml b/.github/workflows/deluxe.yml index 868b17f..2ac875c 100644 --- a/.github/workflows/deluxe.yml +++ b/.github/workflows/deluxe.yml @@ -2,7 +2,7 @@ name: deluxe waffle on: workflow_dispatch: schedule: - - cron: "0 16 * * 0" # 4PM GMT Sunday + - cron: "45 15 * * 0" # 4PM GMT Sunday - 15min jobs: waffle: runs-on: ubuntu-latest @@ -19,10 +19,9 @@ jobs: sudo apt purge google-chrome-stable sudo apt purge chromium-browser sudo apt install -y chromium-browser - chromium-browser --version > version.txt pip install -r requirements.txt - name: Solve deluxe Waffle :) - run: python telegram.py 2 + run: python telegram.py -m 2 -c 1 env: TOKEN: ${{ secrets.TOKEN }} CHATS: ${{ secrets.CHATS }} diff --git a/.github/workflows/pancake.yml b/.github/workflows/pancake.yml index f13698b..054816e 100644 --- a/.github/workflows/pancake.yml +++ b/.github/workflows/pancake.yml @@ -2,7 +2,7 @@ name: daily waffle on: workflow_dispatch: schedule: - - cron: "55 15 * * *" # 4PM GMT - 5min + - cron: "45 15 * * *" # 4PM GMT - 15min jobs: waffle: runs-on: ubuntu-latest @@ -19,10 +19,9 @@ jobs: sudo apt purge google-chrome-stable sudo apt purge chromium-browser sudo apt install -y chromium-browser - chromium-browser --version > version.txt pip install -r requirements.txt - name: Solve daily Waffle :) - run: python telegram.py 1 + run: python telegram.py -m 1 -c 1 env: TOKEN: ${{ secrets.TOKEN }} CHATS: ${{ secrets.CHATS }} diff --git a/main.py b/main.py index 10bb463..8136185 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +import argparse import logging import os import platform @@ -9,9 +10,7 @@ from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service -from urllib.request import urlretrieve from webdriver_manager.chrome import ChromeDriverManager -from zipfile import ZipFile logging.basicConfig( level=logging.INFO, @@ -19,15 +18,6 @@ datefmt='%H:%M:%S' ) -with open('version.txt', 'r') as f: - __VERSION__ = f.readline().split()[1] - -def get_mode(): - if len(sys.argv) == 1: mode = int(input('Daily Waffle or Deluxe Waffle? (1 or 2)\n')) - else: mode = int(sys.argv[1]) - assert mode in [1, 2], 'Invalid mode selected' - return mode - def loop_resolve(f, resolution, lim, *args): if lim == 0: raise Exception('Reached the limit for number of tries') @@ -38,27 +28,16 @@ def loop_resolve(f, resolution, lim, *args): resolution() return loop_resolve(f, resolution, lim-1, *args) -def download_chromedriver(): - version = __VERSION__ #requests.get('https://chromedriver.storage.googleapis.com/LATEST_RELEASE').text - try: os.remove('chromedriver.zip') - except: pass - logging.info('Downloading "chromedriver.exe"...') - url = f'https://chromedriver.storage.googleapis.com/{version}/chromedriver_win32.zip' - urlretrieve(url, 'chromedriver.zip') - with ZipFile('chromedriver.zip') as chromezip: - chromezip.extractall() - os.remove('chromedriver.zip') - def get_windows_browser(): + service = Service() options = webdriver.ChromeOptions() - options.add_argument('--headless') # to debug, comment this line + options.add_argument('--headless') options.add_experimental_option('excludeSwitches', ['enable-logging']) - browser = webdriver.Chrome(options=options) + browser = webdriver.Chrome(service=service, options=options) return browser def get_linux_browser(): - version = __VERSION__ #requests.get('https://chromedriver.storage.googleapis.com/LATEST_RELEASE').text - chrome_service = Service(ChromeDriverManager(chrome_type='chromium', driver_version=version).install()) + chrome_service = Service(ChromeDriverManager(chrome_type='chromium').install()) chrome_options = Options() options = [ "--headless", @@ -112,21 +91,32 @@ def parse(soup): new_board.append(board[(i, j)].lower()) new_verdict.append(verdict[(i, j)].upper()) new_board, new_verdict = map(''.join, [new_board, new_verdict]) - if size == 5: return Pancake(board=new_board, verdict=new_verdict).solve() - elif size == 7: return DeluxePancake(board=new_board, verdict=new_verdict).solve() + return new_board, new_verdict, size + +def solve(board, verdict, size): + if size == 5: return Pancake(board=board, verdict=verdict).solve() + elif size == 7: return DeluxePancake(board=board, verdict=verdict).solve() else: print(f'Ignoring invalid size of {size}...') -def handle_chromedriver(): - curr_os = (pf:=platform.platform())[:pf.find('-')] - if curr_os == 'Windows': download_chromedriver() - -if __name__ == '__main__': +def run(): curr_os = (pf:=platform.platform())[:pf.find('-')] supplier = {'Windows': get_windows_browser, 'Linux': get_linux_browser}.get(curr_os) assert supplier, f'Pancake not supported for {curr_os} yet :(' - mode = get_mode() - soup = loop_resolve(start_browser, handle_chromedriver, 5, mode, supplier) + parser = argparse.ArgumentParser(prog='pancake', description='Solve Waffle in no time') + parser.add_argument('-m', '--mode', help='Waffle mode (1 for daily, 2 for deluxe)') + parser.add_argument('-c', '--cron', default=0, help='Delay solving until new day (0 or 1)') + args = parser.parse_args() + + if int(args.cron): + while (t:=int(time.time()%86400))//3600 < 16: # not 4PM GMT yet + time.sleep(10) + logging.info(f'Waiting... Current time: {str(t//3600).zfill(2)}:{str(t//60%60).zfill(2)}:{str(t%60).zfill(2)} GMT') + + soup = loop_resolve(start_browser, lambda: None, 5, int(args.mode), supplier) logging.info('Source page obtained! Parsing source page now...\n') - parse(soup) - input('Press enter to quit...') + board, verdict, size = parse(soup) + return solve(board, verdict, size) + +if __name__ == '__main__': + run() \ No newline at end of file diff --git a/telegram.py b/telegram.py index 0f8e705..ea7d5be 100644 --- a/telegram.py +++ b/telegram.py @@ -14,9 +14,5 @@ def send(token, chat_id, bot_message): }) logging.info(resp.ok) -curr_os = (pf:=platform.platform())[:pf.find('-')] -supplier = {'Windows': get_windows_browser, 'Linux': get_linux_browser}.get(curr_os) -assert supplier, f'Pancake not supported for {curr_os} yet :(' - -bot_message = parse(loop_resolve(start_browser, handle_chromedriver, 5, get_mode(), supplier)) +bot_message = run() for chat_id in CHATS.split(','): send(TOKEN, chat_id, bot_message)