1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00
royalnet/royalpack/tables/osu.py

94 lines
2.5 KiB
Python
Raw Permalink Normal View History

2020-07-21 23:39:04 +00:00
from typing import *
import aiohttp
import datetime
from sqlalchemy import *
from sqlalchemy.orm import relationship, backref
from sqlalchemy.ext.declarative import declared_attr
from ..types import Updatable, oauth_refresh
# noinspection PyAttributeOutsideInit
class Osu(Updatable):
__tablename__ = "osu"
@declared_attr
def user_id(self):
return Column(Integer, ForeignKey("users.uid"))
@declared_attr
def user(self):
return relationship("User", backref=backref("osu"))
@declared_attr
def access_token(self):
return Column(String, nullable=False)
@declared_attr
def refresh_token(self):
return Column(String, nullable=False)
@declared_attr
def expiration_date(self):
return Column(DateTime, nullable=False)
@declared_attr
def osu_id(self):
return Column(Integer, primary_key=True)
@declared_attr
def username(self):
2020-07-22 23:14:25 +00:00
return Column(String, nullable=False)
@declared_attr
def avatar_url(self):
2020-07-23 00:23:47 +00:00
return Column(String)
2020-07-22 23:14:25 +00:00
@declared_attr
def standard_pp(self):
return Column(Float)
@declared_attr
def taiko_pp(self):
return Column(Float)
@declared_attr
def catch_pp(self):
return Column(Float)
@declared_attr
def mania_pp(self):
return Column(Float)
2020-07-21 23:39:04 +00:00
2020-08-28 15:36:14 +00:00
async def refresh(self, *, client_id, client_secret):
2020-07-21 23:39:04 +00:00
j = await oauth_refresh(url="https://osu.ppy.sh/oauth/token",
client_id=client_id,
client_secret=client_secret,
refresh_code=self.refresh_token)
self.access_token = j["access_token"]
2020-08-30 17:00:31 +00:00
self.refresh_token = j.get("refresh_token") or self.refresh_token
2020-07-21 23:39:04 +00:00
self.expiration_date = datetime.datetime.now() + datetime.timedelta(seconds=j["expires_in"])
2020-08-28 15:36:14 +00:00
async def refresh_if_expired(self, *, client_id, client_secret):
2020-07-21 23:39:04 +00:00
if datetime.datetime.now() >= self.expiration_date:
2020-08-28 15:36:14 +00:00
await self.refresh(client_id=client_id, client_secret=client_secret)
2020-07-22 23:14:25 +00:00
def json(self) -> dict:
return {
"osu_id": self.osu_id,
"username": self.username,
"avatar_url": self.avatar_url,
"standard": {
"pp": self.standard_pp,
},
"taiko": {
"pp": self.taiko_pp,
},
"catch": {
"pp": self.catch_pp,
},
"mania": {
"pp": self.mania_pp,
},
}