Skip to content

Commit

Permalink
Add cron workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
RussellDash332 committed Nov 9, 2024
1 parent ca00862 commit a74ae2a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 48 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/deluxe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}
5 changes: 2 additions & 3 deletions .github/workflows/pancake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}
64 changes: 27 additions & 37 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import argparse
import logging
import os
import platform
Expand All @@ -9,25 +10,14 @@
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,
format= '[%(asctime)s] %(levelname)s - %(message)s',
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')
Expand All @@ -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",
Expand Down Expand Up @@ -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()
6 changes: 1 addition & 5 deletions telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit a74ae2a

Please sign in to comment.