Skip to content

Commit b905770

Browse files
committed
Add utility functions for avatar & thumbnail urls
1 parent 222f376 commit b905770

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

app/cog.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11

22
from discord.ext.commands import Cog
3-
from sqlalchemy.orm import Session
3+
from config import DOMAIN_NAME
44
from typing import Callable
55

6-
from app.common.database.objects import DBUser
6+
from app.common.database.objects import DBUser, DBBeatmapset
77
from app.common.database import users
88
from app import session
99

10+
import hashlib
1011
import asyncio
1112
import app
1213

@@ -26,6 +27,19 @@ def __init__(self) -> None:
2627
async def run_async(func: Callable, *args):
2728
return await asyncio.get_event_loop().run_in_executor(None, func, *args)
2829

30+
@staticmethod
31+
def avatar_url(user: DBUser) -> str:
32+
url = f"http://osu.{DOMAIN_NAME}/a/{user.id}"
33+
url += f"?c={user.avatar_hash}" if user.avatar_hash else ""
34+
return url
35+
36+
@staticmethod
37+
def thumbnail_url(beatmapset: DBBeatmapset) -> str:
38+
update_hash = hashlib.md5(f'{beatmapset.last_update}'.encode()).hexdigest()
39+
url = f"http://osu.{DOMAIN_NAME}/mt/{beatmapset.id}"
40+
url += f"?c={update_hash}"
41+
return url
42+
2943
async def resolve_user(self, discord_id: int) -> DBUser | None:
3044
return await self.run_async(
3145
users.fetch_by_discord_id,

0 commit comments

Comments
 (0)