from __future__ import annotations import datetime import typing import urllib.parse from workers import Response, WorkerEntrypoint, fetch if typing.TYPE_CHECKING: from workers import Request class Default(WorkerEntrypoint): async def fetch(self, request: Request) -> Response: url = urllib.parse.urlparse(request.url) match url.path: case '/gy-694c': return await gy_694c(self.env.punoted_api_key) case _: return Response('404', status=404) async def gy_694c(punoted_api_key: str) -> Response: headers = {'X-Data-Token': punoted_api_key} response = await fetch('https://api.punoted.net/v1/storages/user', headers=headers) storages = await response.json() # pyright: ignore[reportGeneralTypeIssues] (storage,) = (s for s in storages if s['Location'] == 'GY-694c') tio = 0 for item in storage['StorageItems']: if item['MaterialTicker'] == 'TIO': tio = item['MaterialAmount'] last_update = datetime.datetime.fromtimestamp(storage['LastUpdatedEpochMs'] / 1000) delta = datetime.datetime.now() - last_update response = await fetch('https://api.punoted.net/v1/production/user/burn?location=gy-694c', headers=headers) production = await response.json() # pyright: ignore[reportGeneralTypeIssues] (tio_production,) = (p for p in production['GY-694c'] if p['MaterialTicker'] == 'TIO') daily_production = tio_production['Production'] estimated = int(tio + daily_production * (delta.total_seconds() / 60 / 60 / 24)) return Response(f'raylu had {tio} TIO in storage as of {last_update} UTC ({delta.total_seconds() / 60 / 60:.1f} hours ago)\n' f'raylu is producing {daily_production}/day\n' f'estimated TIO in inventory: {estimated}\n')