fix hanging if no available mirrors
This commit is contained in:
doms9 2025-12-12 10:46:56 -05:00
parent 3a1d2742f1
commit 00000d9233
8 changed files with 24 additions and 16 deletions

View file

@ -25,7 +25,7 @@ def fix_league(s: str) -> str:
async def refresh_api_cache( async def refresh_api_cache(
client: httpx.AsyncClient, client: httpx.AsyncClient,
url: str, url: str,
ts: float, now_ts: float,
) -> dict[str, dict[str, str]]: ) -> dict[str, dict[str, str]]:
log.info("Refreshing API cache") log.info("Refreshing API cache")
@ -37,9 +37,10 @@ async def refresh_api_cache(
return {} return {}
data = r.json() if not (data := r.json()):
return {}
data["timestamp"] = ts data["timestamp"] = now_ts
return data return data

View file

@ -115,6 +115,8 @@ async def scrape(client: httpx.AsyncClient) -> None:
CACHE_FILE.write(cached_urls) CACHE_FILE.write(cached_urls)
return return
log.info(f'Scraping from "{base_url}"')
events = await get_events( events = await get_events(
client, client,
api_url, api_url,

View file

@ -45,9 +45,10 @@ async def refresh_api_cache(
except Exception as e: except Exception as e:
log.error(f'Failed to fetch "{url}": {e}') log.error(f'Failed to fetch "{url}": {e}')
return {} return []
data = r.json() if not (data := r.json()):
return []
data[-1]["timestamp"] = now_ts data[-1]["timestamp"] = now_ts

View file

@ -59,7 +59,8 @@ async def get_api_data(client: httpx.AsyncClient, url: str) -> list[dict[str, An
async def refresh_api_cache( async def refresh_api_cache(
client: httpx.AsyncClient, ts: float client: httpx.AsyncClient,
now_ts: float,
) -> list[dict[str, Any]]: ) -> list[dict[str, Any]]:
log.info("Refreshing API cache") log.info("Refreshing API cache")
@ -70,12 +71,13 @@ async def refresh_api_cache(
results = await asyncio.gather(*tasks) results = await asyncio.gather(*tasks)
data = list(chain(*results)) if not (data := list(chain(*results))):
return []
for ev in data: for ev in data:
ev["ts"] = ev.pop("timestamp") ev["ts"] = ev.pop("timestamp")
data[-1]["timestamp"] = ts data[-1]["timestamp"] = now_ts
return data return data

View file

@ -38,7 +38,7 @@ def fix_sport(s: str) -> str:
async def refresh_api_cache( async def refresh_api_cache(
client: httpx.AsyncClient, client: httpx.AsyncClient,
url: str, url: str,
ts: float, now_ts: float,
) -> list[dict[str, Any]]: ) -> list[dict[str, Any]]:
log.info("Refreshing API cache") log.info("Refreshing API cache")
@ -49,11 +49,12 @@ async def refresh_api_cache(
except Exception as e: except Exception as e:
log.error(f'Failed to fetch "{url}": {e}') log.error(f'Failed to fetch "{url}": {e}')
return {} return []
data = r.json() if not (data := r.json()):
return []
data[-1]["timestamp"] = ts data[-1]["timestamp"] = now_ts
return data return data

View file

@ -53,7 +53,7 @@ class Network:
async def check_status(self, url: str) -> bool: async def check_status(self, url: str) -> bool:
try: try:
r = await self.client.get(url) r = await self.client.get(url, timeout=5)
r.raise_for_status() r.raise_for_status()
return r.status_code == 200 return r.status_code == 200
except (httpx.HTTPError, httpx.TimeoutException) as e: except (httpx.HTTPError, httpx.TimeoutException) as e:

View file

@ -67,7 +67,8 @@ async def refresh_api_cache(
results = await asyncio.gather(*tasks) results = await asyncio.gather(*tasks)
data = list(chain(*results)) if not (data := list(chain(*results))):
return []
for ev in data: for ev in data:
ev["ts"] = ev.pop("timestamp") ev["ts"] = ev.pop("timestamp")