save full state, not just stats
This commit is contained in:
parent
f6c6c6c66e
commit
71f391ec69
3
b2.py
3
b2.py
@ -45,10 +45,9 @@ class Client:
|
|||||||
|
|
||||||
def put_object(self, bucket_id, path, data):
|
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()
|
upload = self.post(f'{self.api_url}/b2api/v2/b2_get_upload_url', json={'bucketId': bucket_id}).json()
|
||||||
filename = path.split('/')[-1]
|
|
||||||
headers = {
|
headers = {
|
||||||
'Authorization': upload['authorizationToken'],
|
'Authorization': upload['authorizationToken'],
|
||||||
'X-Bz-File-Name': urlquote(filename),
|
'X-Bz-File-Name': urlquote(path),
|
||||||
'Content-Type': 'b2/x-auto',
|
'Content-Type': 'b2/x-auto',
|
||||||
'Content-Length': str(len(data)),
|
'Content-Length': str(len(data)),
|
||||||
'X-Bz-Content-Sha1': hashlib.sha1(data).hexdigest()
|
'X-Bz-Content-Sha1': hashlib.sha1(data).hexdigest()
|
||||||
|
51
bot.py
51
bot.py
@ -1,5 +1,6 @@
|
|||||||
import collections
|
import collections
|
||||||
import datetime
|
import datetime
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
import time
|
import time
|
||||||
@ -86,32 +87,40 @@ class State:
|
|||||||
self.bucket_id = os.environ['B2_BUCKET_ID']
|
self.bucket_id = os.environ['B2_BUCKET_ID']
|
||||||
|
|
||||||
def restore(self):
|
def restore(self):
|
||||||
# use yesterday's state if available
|
# use the latest state that isn't from today
|
||||||
# if not, use the latest previous
|
|
||||||
files = self.b2_client.list_objects(self.bucket_id, 'wordle/')
|
files = self.b2_client.list_objects(self.bucket_id, 'wordle/')
|
||||||
data = None
|
state = None
|
||||||
for filename in reversed(files):
|
for file in reversed(files):
|
||||||
|
path = file['fileName']
|
||||||
|
filename = path.split('/')[-1]
|
||||||
if filename < f'state_{datetime.date.today()}.json':
|
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
|
break
|
||||||
|
|
||||||
if data:
|
if state:
|
||||||
self.driver.execute_script(
|
self.driver.execute_script(
|
||||||
"window.localStorage.setItem('statistics', arguments[1]);",
|
"window.localStorage.setItem('gameState', arguments[0]);",
|
||||||
data.decode('utf-8')
|
state['gameState']
|
||||||
|
)
|
||||||
|
self.driver.execute_script(
|
||||||
|
"window.localStorage.setItem('statistics', arguments[0]);",
|
||||||
|
state['statistics']
|
||||||
)
|
)
|
||||||
print('Restored state: ', filename)
|
print('Restored state: ', filename)
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
state = self.driver.execute_script("return window.localStorage.getItem('statistics')")
|
game = self.driver.execute_script("return window.localStorage.getItem('gameState')")
|
||||||
if state:
|
stats = self.driver.execute_script("return window.localStorage.getItem('statistics')")
|
||||||
filename = f'state_{datetime.date.today()}.json'
|
state = {'gameState': game, 'statistics': stats} # yes, these are going to be double-encoded
|
||||||
self.b2_client.put_object(
|
|
||||||
self.bucket_id,
|
filename = f'state_{datetime.date.today()}.json'
|
||||||
f'wordle/{filename}',
|
self.b2_client.put_object(
|
||||||
state.encode('utf-8')
|
self.bucket_id,
|
||||||
)
|
f'wordle/{filename}',
|
||||||
print('Saved state: ', filename)
|
json.dumps(state).encode('utf-8')
|
||||||
|
)
|
||||||
|
print('Saved state: ', filename)
|
||||||
|
|
||||||
|
|
||||||
class Solver:
|
class Solver:
|
||||||
@ -217,8 +226,8 @@ if __name__ == '__main__':
|
|||||||
solver.state.restore()
|
solver.state.restore()
|
||||||
result = solver.solve()
|
result = solver.solve()
|
||||||
solver.state.save()
|
solver.state.save()
|
||||||
board_img = solver.capture_board()
|
board_path = solver.capture_board()
|
||||||
stats_img = solve.capture_state()
|
stats_path = solver.capture_stats()
|
||||||
finally:
|
finally:
|
||||||
solver.driver.close()
|
solver.driver.close()
|
||||||
|
|
||||||
@ -236,10 +245,10 @@ if __name__ == '__main__':
|
|||||||
slack_request(
|
slack_request(
|
||||||
'files.upload',
|
'files.upload',
|
||||||
params={'thread_ts': msg['ts'], 'channels': channel},
|
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(
|
slack_request(
|
||||||
'files.upload',
|
'files.upload',
|
||||||
params={'thread_ts': msg['ts'], 'channels': channel},
|
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())},
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user