Skip to content

Instantly share code, notes, and snippets.

@Cidolfas
Last active August 8, 2020 17:57
Show Gist options
  • Save Cidolfas/ee694b3a0256b5c8d55831466337087d to your computer and use it in GitHub Desktop.
Save Cidolfas/ee694b3a0256b5c8d55831466337087d to your computer and use it in GitHub Desktop.
#! python
import asyncio
import datetime
import json
import pickle
from aiohttp import web
digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
class Server:
def __init__(self):
file = open("blaseballws.stream", 'br')
self.messages = pickle.load(file)
print("Loaded stream: " + str(len(self.messages)) + " items, last at " + str(self.messages[-1][0]))
self.games = []
self.webapp = web.Application()
self.webapp.add_routes([web.get('/games', self.handle_http_games), web.get('/ws', self.start_ws)])
async def start_ws(self, request):
ws = web.WebSocketResponse()
await ws.prepare(request)
start_time = datetime.datetime.now()
next_message = 0
print("Started playback due to WS connection")
while len(self.messages) > next_message:
now = datetime.datetime.now()
time_since_start = now - start_time
seconds = time_since_start.total_seconds()
if seconds >= self.messages[next_message][0]:
print("WS: " + str(self.messages[next_message][0]))
await ws.send_str(self.messages[next_message][1])
next_message += 1
else:
await asyncio.sleep(0.05)
print("Finished WS playback")
async def start_http_playback(self):
start_time = datetime.datetime.now()
next_message = 0
print("Started HTTP playback")
while len(self.messages) > next_message:
now = datetime.datetime.now()
time_since_start = now - start_time
seconds = time_since_start.total_seconds()
if seconds >= self.messages[next_message][0]:
print("HTTP: " + str(self.messages[next_message][0]))
message = self.messages[next_message][1]
while len(message) > 0 and message[0] in digits:
message = message[1:]
if len(message) > 0:
m = json.loads(message)
if type(m) is list:
if (m[0] == "gameDataUpdate"):
self.games = m[1]["schedule"]
next_message += 1
else:
await asyncio.sleep(0.05)
print("Finished HTTP playback")
async def start_http_server(self):
runner = web.AppRunner(self.webapp)
await runner.setup()
site = web.TCPSite(runner, 'localhost', 8080)
await site.start()
async def handle_http_games(self, request):
return web.json_response(self.games)
server = Server()
async def start_server(server):
await asyncio.gather(
server.start_http_server(),
server.start_http_playback()
)
asyncio.get_event_loop().run_until_complete(start_server(server))
#! python
import asyncio
import websockets
import datetime
import sys
import pickle
startTime = datetime.datetime.now()
messages = []
async def start():
uri = "wss://blaseball.com/socket.io/?EIO=3&transport=websocket"
async with websockets.connect(uri) as ws:
await asyncio.gather(
ping(ws),
process(ws)
)
async def ping(ws):
while True:
await asyncio.sleep(25)
await ws.send("3")
async def process(ws):
async for message in ws:
now = datetime.datetime.now()
timeSinceStart = now - startTime
messages.append((timeSinceStart.total_seconds(), message))
print(str(timeSinceStart.total_seconds()) + ": " + message[0:30])
try:
asyncio.get_event_loop().run_until_complete(start())
except KeyboardInterrupt:
file = open("blaseballws.stream", 'bw')
pickle.dump(messages, file)
print("Recorded " + str(len(messages)) + " messages")
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment