Skip to content

Commit

Permalink
ensure user data on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Aug 21, 2024
1 parent 98f1cb8 commit 636025f
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions server/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import difflib
import html
import mimetypes
Expand Down Expand Up @@ -277,25 +276,24 @@ async def startup_fetch_missing_users(*args: Any, **kwargs: Any) -> None:
x[0] for x in await pg.fetch("select user_id from patch_users where user_id = any($1)", s)
]

async def background() -> None:
for user in s:
if user in user_fetched:
continue
r = await http_client.get(f"https://api.bgm.tv/user/{user}")
data = r.json()
await pg.execute(
"""
insert into patch_users (user_id, username, nickname) VALUES ($1, $2, $3)
on conflict (user_id) do update set
username = excluded.username,
nickname = excluded.nickname
""",
data["id"],
data["username"],
html.unescape(data["nickname"]),
)
s = {x for x in s if x not in user_fetched}
if not s:
return

asyncio.create_task(background()) # noqa: RUF006
for user in s:
r = await http_client.get(f"https://api.bgm.tv/user/{user}")
data = r.json()
await pg.execute(
"""
insert into patch_users (user_id, username, nickname) VALUES ($1, $2, $3)
on conflict (user_id) do update set
username = excluded.username,
nickname = excluded.nickname
""",
data["id"],
data["username"],
html.unescape(data["nickname"]),
)


app = litestar.Litestar(
Expand Down

0 comments on commit 636025f

Please sign in to comment.