From 71f391ec696f915b52d969e37a52c71846f627c5 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro <=> Date: Tue, 1 Feb 2022 10:05:13 -0800 Subject: [PATCH] save full state, not just stats --- b2.py | 3 +-- bot.py | 51 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/b2.py b/b2.py index 15bece8..960078d 100644 --- a/b2.py +++ b/b2.py @@ -45,10 +45,9 @@ class Client: def put_object(self, bucket_id, path, data): upload = self.post(f'{self.api_url}/b2api/v2/b2_get_upload_url', json={'bucketId': bucket_id}).json() - filename = path.split('/')[-1] headers = { 'Authorization': upload['authorizationToken'], - 'X-Bz-File-Name': urlquote(filename), + 'X-Bz-File-Name': urlquote(path), 'Content-Type': 'b2/x-auto', 'Content-Length': str(len(data)), 'X-Bz-Content-Sha1': hashlib.sha1(data).hexdigest() diff --git a/bot.py b/bot.py index b157450..005a3ce 100644 --- a/bot.py +++ b/bot.py @@ -1,5 +1,6 @@ import collections import datetime +import json import os import pathlib import time @@ -86,32 +87,40 @@ class State: self.bucket_id = os.environ['B2_BUCKET_ID'] def restore(self): - # use yesterday's state if available - # if not, use the latest previous + # use the latest state that isn't from today files = self.b2_client.list_objects(self.bucket_id, 'wordle/') - data = None - for filename in reversed(files): + state = None + for file in reversed(files): + path = file['fileName'] + filename = path.split('/')[-1] if filename < f'state_{datetime.date.today()}.json': - data = self.b2_client.get_object('cupboard', filename) + data = self.b2_client.get_object('cupboard', path) + state = json.loads(data.decode('utf-8')) break - if data: + if state: self.driver.execute_script( - "window.localStorage.setItem('statistics', arguments[1]);", - data.decode('utf-8') + "window.localStorage.setItem('gameState', arguments[0]);", + state['gameState'] + ) + self.driver.execute_script( + "window.localStorage.setItem('statistics', arguments[0]);", + state['statistics'] ) print('Restored state: ', filename) def save(self): - state = self.driver.execute_script("return window.localStorage.getItem('statistics')") - if state: - filename = f'state_{datetime.date.today()}.json' - self.b2_client.put_object( - self.bucket_id, - f'wordle/{filename}', - state.encode('utf-8') - ) - print('Saved state: ', filename) + game = self.driver.execute_script("return window.localStorage.getItem('gameState')") + stats = self.driver.execute_script("return window.localStorage.getItem('statistics')") + state = {'gameState': game, 'statistics': stats} # yes, these are going to be double-encoded + + filename = f'state_{datetime.date.today()}.json' + self.b2_client.put_object( + self.bucket_id, + f'wordle/{filename}', + json.dumps(state).encode('utf-8') + ) + print('Saved state: ', filename) class Solver: @@ -217,8 +226,8 @@ if __name__ == '__main__': solver.state.restore() result = solver.solve() solver.state.save() - board_img = solver.capture_board() - stats_img = solve.capture_state() + board_path = solver.capture_board() + stats_path = solver.capture_stats() finally: solver.driver.close() @@ -236,10 +245,10 @@ if __name__ == '__main__': slack_request( 'files.upload', params={'thread_ts': msg['ts'], 'channels': channel}, - files={'file': (board_img, open(board_img, 'rb').read())}, + files={'file': (board_path, open(board_path, 'rb').read())}, ) slack_request( 'files.upload', params={'thread_ts': msg['ts'], 'channels': channel}, - files={'file': (stats_img, open(stats_img, 'rb').read())}, + files={'file': (stats_path, open(stats_path, 'rb').read())}, )