1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-24 03:54:20 +00:00

Database improvements

This commit is contained in:
Steffo 2018-08-28 15:14:29 +02:00
parent 873bb2335d
commit ab9ff93a41
3 changed files with 53 additions and 171 deletions

205
db.py
View file

@ -45,11 +45,11 @@ class Royal(Base):
class Telegram(Base): class Telegram(Base):
__tablename__ = "telegram" __tablename__ = "telegram"
royal_id = Column(Integer, ForeignKey("royals.id"), nullable=False) royal_id = Column(Integer, ForeignKey("royals.id"))
royal = relationship("Royal", backref="telegram", lazy="joined") royal = relationship("Royal", backref="telegram", lazy="joined")
telegram_id = Column(BigInteger, primary_key=True) telegram_id = Column(BigInteger, primary_key=True)
first_name = Column(String, nullable=False) first_name = Column(String)
last_name = Column(String) last_name = Column(String)
username = Column(String) username = Column(String)
@ -91,7 +91,7 @@ class Telegram(Base):
class Steam(Base): class Steam(Base):
__tablename__ = "steam" __tablename__ = "steam"
royal_id = Column(Integer, ForeignKey("royals.id"), nullable=False) royal_id = Column(Integer, ForeignKey("royals.id"))
royal = relationship("Royal", backref="steam", lazy="joined") royal = relationship("Royal", backref="steam", lazy="joined")
steam_id = Column(String, primary_key=True) steam_id = Column(String, primary_key=True)
@ -198,74 +198,8 @@ class RocketLeague(Base):
def __repr__(self): def __repr__(self):
return f"<RocketLeague {self.steam_id}>" return f"<RocketLeague {self.steam_id}>"
# @staticmethod def update(self, data=None):
# def create(session: Session, steam_id: str): raise NotImplementedError("rlstats API is no longer available.")
# rl = session.query(RocketLeague).get(steam_id)
# if rl is not None:
# raise AlreadyExistingError(repr(rl))
# r = requests.get(f"https://api.rocketleaguestats.com/v1/player?apikey={config['Rocket League']['rlstats_api_key']}&unique_id={str(steam_id)}&platform_id=1")
# if r.status_code == 404:
# raise NotFoundError("The specified user has never played Rocket League")
# elif r.status_code != 200:
# raise RequestError("Rocket League Stats returned {r.status_code}")
# new_record = RocketLeague(steam_id=steam_id)
# new_record.update(data=r.json())
# return new_record
# def update(self, data=None):
# if data is None:
# r = requests.get(f"https://api.rocketleaguestats.com/v1/player?apikey={config['Rocket League']['rlstats_api_key']}&unique_id={self.steam_id}&platform_id=1")
# if r.status_code != 200:
# raise RequestError(f"Rocket League Stats returned {r.status_code}")
# data = r.json()
# # Get current season
# current_season = 0
# for season in data["rankedSeasons"]:
# if int(season) > current_season:
# current_season = int(season)
# if current_season == 0:
# return
# self.season = current_season
# current_season = str(current_season)
# # Get wins
# self.wins = data["stats"]["wins"]
# # Get ranked data
# # Single 1v1
# if "10" in data["rankedSeasons"][current_season]:
# self.single_mmr = data["rankedSeasons"][current_season]["10"]["rankPoints"]
# if data["rankedSeasons"][current_season]["10"]["matchesPlayed"] >= 10:
# self.single_rank = data["rankedSeasons"][current_season]["10"]["tier"]
# self.single_div = data["rankedSeasons"][current_season]["10"]["division"]
# else:
# self.single_rank = None
# self.single_div = None
# # Doubles 2v2
# if "11" in data["rankedSeasons"][current_season]:
# self.doubles_mmr = data["rankedSeasons"][current_season]["11"]["rankPoints"]
# if data["rankedSeasons"][current_season]["11"]["matchesPlayed"] >= 10:
# self.doubles_rank = data["rankedSeasons"][current_season]["11"]["tier"]
# self.doubles_div = data["rankedSeasons"][current_season]["11"]["division"]
# else:
# self.doubles_rank = None
# self.doubles_div = None
# # Standard 3v3
# if "13" in data["rankedSeasons"][current_season]:
# self.standard_mmr = data["rankedSeasons"][current_season]["13"]["rankPoints"]
# if data["rankedSeasons"][current_season]["13"]["matchesPlayed"] >= 10:
# self.standard_rank = data["rankedSeasons"][current_season]["13"]["tier"]
# self.standard_div = data["rankedSeasons"][current_season]["13"]["division"]
# else:
# self.standard_rank = None
# self.standard_div = None
# # Solo Standard 3v3
# if "12" in data["rankedSeasons"][current_season]:
# self.solo_std_mmr = data["rankedSeasons"][current_season]["12"]["rankPoints"]
# if data["rankedSeasons"][current_season]["12"]["matchesPlayed"] >= 10:
# self.solo_std_rank = data["rankedSeasons"][current_season]["12"]["tier"]
# self.solo_std_div = data["rankedSeasons"][current_season]["12"]["division"]
# else:
# self.solo_std_rank = None
# self.solo_std_div = None
def solo_rank_image(self): def solo_rank_image(self):
if self.single_rank is None: if self.single_rank is None:
@ -304,8 +238,10 @@ class Dota(Base):
rank_tier = Column(Integer) rank_tier = Column(Integer)
wins = Column(Integer, nullable=False) wins = Column(Integer)
losses = Column(Integer, nullable=False) losses = Column(Integer)
most_played_hero = Column(Integer)
def get_rank_icon_url(self): def get_rank_icon_url(self):
# Rank icon is determined by the first digit of the rank tier # Rank icon is determined by the first digit of the rank tier
@ -343,7 +279,7 @@ class Dota(Base):
return str(self.rank_tier)[1] return str(self.rank_tier)[1]
@staticmethod @staticmethod
def create(session: Session, steam_id: int): def create(session: Session, steam_id: int) -> "Dota":
d = session.query(Dota).get(steam_id) d = session.query(Dota).get(steam_id)
if d is not None: if d is not None:
raise AlreadyExistingError(repr(d)) raise AlreadyExistingError(repr(d))
@ -353,28 +289,27 @@ class Dota(Base):
data = r.json() data = r.json()
if "profile" not in data: if "profile" not in data:
raise NotFoundError("The specified user has never played Dota or has a private match history") raise NotFoundError("The specified user has never played Dota or has a private match history")
r = requests.get(f"https://api.opendota.com/api/players/{Steam.to_steam_id_3(steam_id)}/wl") new_record = Dota(steam_id=str(steam_id))
if r.status_code != 200: new_record.update()
raise RequestError("OpenDota returned {r.status_code}")
wl = r.json()
new_record = Dota(steam_id=str(steam_id),
rank_tier=data["rank_tier"],
wins=wl["win"],
losses=wl["lose"])
return new_record return new_record
def update(self): def update(self) -> None:
r = requests.get(f"https://api.opendota.com/api/players/{Steam.to_steam_id_3(self.steam_id)}") r = requests.get(f"https://api.opendota.com/api/players/{Steam.to_steam_id_3(self.steam_id)}")
if r.status_code != 200: if r.status_code != 200:
raise RequestError("OpenDota returned {r.status_code}") raise RequestError("OpenDota / returned {r.status_code}")
data = r.json() data = r.json()
r = requests.get(f"https://api.opendota.com/api/players/{Steam.to_steam_id_3(self.steam_id)}/wl") r = requests.get(f"https://api.opendota.com/api/players/{Steam.to_steam_id_3(self.steam_id)}/wl")
if r.status_code != 200: if r.status_code != 200:
raise RequestError("OpenDota returned {r.status_code}") raise RequestError("OpenDota /wl returned {r.status_code}")
wl = r.json() wl = r.json()
r = requests.get(f"https://api.opendota.com/api/players/{Steam.to_steam_id_3(self.steam_id)}/heroes")
if r.status_code != 200:
raise RequestError("OpenDota /heroes returned {r.status_code}")
heroes = r.json()
self.rank_tier = data["rank_tier"] self.rank_tier = data["rank_tier"]
self.wins = wl["win"] self.wins = wl["win"]
self.losses = wl["lose"] self.losses = wl["lose"]
self.most_played_hero = heroes[0]
class LeagueOfLegendsRanks(enum.Enum): class LeagueOfLegendsRanks(enum.Enum):
@ -398,13 +333,13 @@ class RomanNumerals(enum.Enum):
class LeagueOfLegends(Base): class LeagueOfLegends(Base):
__tablename__ = "leagueoflegends" __tablename__ = "leagueoflegends"
royal_id = Column(Integer, ForeignKey("royals.id"), nullable=False) royal_id = Column(Integer, ForeignKey("royals.id"))
royal = relationship("Royal", backref="lol", lazy="joined") royal = relationship("Royal", backref="lol", lazy="joined")
summoner_id = Column(BigInteger, primary_key=True) summoner_id = Column(BigInteger, primary_key=True)
summoner_name = Column(String, nullable=False) summoner_name = Column(String)
level = Column(Integer, nullable=False) level = Column(Integer)
solo_division = Column(Enum(LeagueOfLegendsRanks)) solo_division = Column(Enum(LeagueOfLegendsRanks))
solo_rank = Column(Enum(RomanNumerals)) solo_rank = Column(Enum(RomanNumerals))
flex_division = Column(Enum(LeagueOfLegendsRanks)) flex_division = Column(Enum(LeagueOfLegendsRanks))
@ -412,6 +347,8 @@ class LeagueOfLegends(Base):
twtr_division = Column(Enum(LeagueOfLegendsRanks)) twtr_division = Column(Enum(LeagueOfLegendsRanks))
twtr_rank = Column(Enum(RomanNumerals)) twtr_rank = Column(Enum(RomanNumerals))
highest_mastery_champ = Column(Integer)
@staticmethod @staticmethod
def create(session: Session, royal_id, summoner_name=None, summoner_id=None): def create(session: Session, royal_id, summoner_name=None, summoner_id=None):
if summoner_name: if summoner_name:
@ -419,7 +356,7 @@ class LeagueOfLegends(Base):
elif summoner_id: elif summoner_id:
lol = session.query(LeagueOfLegends).get(summoner_id) lol = session.query(LeagueOfLegends).get(summoner_id)
else: else:
raise SyntaxError("Neither summoner_name or summoner_id are specified") raise SyntaxError("Neither summoner_name or summoner_id were specified.")
if lol is not None: if lol is not None:
raise AlreadyExistingError(repr(lol)) raise AlreadyExistingError(repr(lol))
# Get the summoner_id # Get the summoner_id
@ -440,12 +377,16 @@ class LeagueOfLegends(Base):
def update(self): def update(self):
r = requests.get(f"https://euw1.api.riotgames.com/lol/summoner/v3/summoners/{self.summoner_id}?api_key={config['League of Legends']['riot_api_key']}") r = requests.get(f"https://euw1.api.riotgames.com/lol/summoner/v3/summoners/{self.summoner_id}?api_key={config['League of Legends']['riot_api_key']}")
if r.status_code != 200: if r.status_code != 200:
return RequestError(f"League of Legends API returned {r.status_code}") return RequestError(f"League of Legends API /summoner returned {r.status_code}")
data = r.json() data = r.json()
r = requests.get(f"https://euw1.api.riotgames.com/lol/league/v3/positions/by-summoner/{self.summoner_id}?api_key={config['League of Legends']['riot_api_key']}") r = requests.get(f"https://euw1.api.riotgames.com/lol/league/v3/positions/by-summoner/{self.summoner_id}?api_key={config['League of Legends']['riot_api_key']}")
if r.status_code != 200: if r.status_code != 200:
return RequestError(f"League of Legends API returned {r.status_code}") return RequestError(f"League of Legends API /league returned {r.status_code}")
rank = r.json() rank = r.json()
r = requests.get(f"https://euw1.api.riotgames.com/lol/champion-mastery/v3/champion-masteries/by-summoner/{self.summoner_id}?api_key={config['League of Legends']['riot_api_key']}")
if r.status_code != 200:
return RequestError(f"League of Legends API /champion-mastery returned {r.status_code}")
mastery = r.json()
solo_rank = None solo_rank = None
flex_rank = None flex_rank = None
twtr_rank = None twtr_rank = None
@ -477,6 +418,7 @@ class LeagueOfLegends(Base):
else: else:
self.twtr_division = None self.twtr_division = None
self.twtr_rank = None self.twtr_rank = None
self.highest_mastery_champ = mastery[0]["championId"]
class Osu(Base): class Osu(Base):
@ -486,7 +428,7 @@ class Osu(Base):
royal = relationship("Royal", backref="osu", lazy="joined") royal = relationship("Royal", backref="osu", lazy="joined")
osu_id = Column(Integer, primary_key=True) osu_id = Column(Integer, primary_key=True)
osu_name = Column(String, nullable=False) osu_name = Column(String)
std_pp = Column(Float) std_pp = Column(Float)
taiko_pp = Column(Float) taiko_pp = Column(Float)
@ -540,12 +482,12 @@ class Discord(Base):
__tablename__ = "discord" __tablename__ = "discord"
__table_args__ = tuple(UniqueConstraint("name", "discriminator")) __table_args__ = tuple(UniqueConstraint("name", "discriminator"))
royal_id = Column(Integer, ForeignKey("royals.id"), nullable=False) royal_id = Column(Integer, ForeignKey("royals.id"))
royal = relationship("Royal", backref="discord", lazy="joined") royal = relationship("Royal", backref="discord", lazy="joined")
discord_id = Column(BigInteger, primary_key=True) discord_id = Column(BigInteger, primary_key=True)
name = Column(String, nullable=False) name = Column(String)
discriminator = Column(Integer, nullable=False) discriminator = Column(Integer)
avatar_hex = Column(String) avatar_hex = Column(String)
def __str__(self): def __str__(self):
@ -556,6 +498,8 @@ class Discord(Base):
@staticmethod @staticmethod
def create(session: Session, royal_username, discord_user: DiscordUser): def create(session: Session, royal_username, discord_user: DiscordUser):
# TODO: remove this
print("Discord.create is deprecated and should be removed soon.")
d = session.query(Discord).filter(Discord.discord_id == discord_user.id).first() d = session.query(Discord).filter(Discord.discord_id == discord_user.id).first()
if d is not None: if d is not None:
raise AlreadyExistingError(repr(d)) raise AlreadyExistingError(repr(d))
@ -589,9 +533,9 @@ class Overwatch(Base):
battletag = Column(String, primary_key=True) battletag = Column(String, primary_key=True)
discriminator = Column(Integer, primary_key=True) discriminator = Column(Integer, primary_key=True)
icon = Column(String, nullable=False) icon = Column(String)
level = Column(Integer, nullable=False) level = Column(Integer)
rank = Column(Integer) rank = Column(Integer)
def __str__(self, separator="#"): def __str__(self, separator="#"):
@ -607,22 +551,10 @@ class Overwatch(Base):
o = session.query(Overwatch).filter_by(battletag=battletag, discriminator=discriminator).first() o = session.query(Overwatch).filter_by(battletag=battletag, discriminator=discriminator).first()
if o is not None: if o is not None:
raise AlreadyExistingError(repr(o)) raise AlreadyExistingError(repr(o))
r = requests.get(f"https://owapi.net/api/v3/u/{battletag}-{discriminator}/stats", headers={
"User-Agent": "Royal-Bot/4.0",
"From": "ste.pigozzi@gmail.com"
})
if r.status_code != 200:
raise RequestError(f"OWAPI.net returned {r.status_code}")
try:
j = r.json()["eu"]["stats"]["quickplay"]["overall_stats"]
except TypeError:
raise RequestError("Something went wrong when retrieving the stats.")
o = Overwatch(royal_id=royal_id, o = Overwatch(royal_id=royal_id,
battletag=battletag, battletag=battletag,
discriminator=discriminator, discriminator=discriminator)
icon=re.search(r"https://.+\.cloudfront\.net/game/unlocks/(0x[0-9A-F]+)\.png", j["avatar"]).group(1), o.update()
level=j["prestige"] * 100 + j["level"],
rank=j["comprank"])
return o return o
def icon_url(self): def icon_url(self):
@ -630,13 +562,13 @@ class Overwatch(Base):
def update(self): def update(self):
r = requests.get(f"https://owapi.net/api/v3/u/{self.battletag}-{self.discriminator}/stats", headers={ r = requests.get(f"https://owapi.net/api/v3/u/{self.battletag}-{self.discriminator}/stats", headers={
"User-Agent": "Royal-Bot/4.0", "User-Agent": "Royal-Bot/4.1",
"From": "ste.pigozzi@gmail.com" "From": "ste.pigozzi@gmail.com"
}) })
if r.status_code != 200: if r.status_code != 200:
raise RequestError(f"OWAPI.net returned {r.status_code}") raise RequestError(f"OWAPI.net returned {r.status_code}")
try: try:
j = r.json()["eu"]["stats"]["quickplay"]["overall_stats"] j = r.json()["eu"]["stats"]["competitive"]["overall_stats"]
except TypeError: except TypeError:
raise RequestError("Something went wrong when retrieving the stats.") raise RequestError("Something went wrong when retrieving the stats.")
try: try:
@ -812,15 +744,6 @@ class VoteAnswer(Base):
return f"<VoteAnswer {self.question_id} {self.user} {self.choice}>" return f"<VoteAnswer {self.question_id} {self.user} {self.choice}>"
class AprilFoolsBan(Base):
__tablename__ = "aprilfoolsban"
id = Column(Integer, primary_key=True)
from_user_id = Column(BigInteger, nullable=False)
to_user_id = Column(BigInteger, nullable=False)
datetime = Column(DateTime, nullable=False)
class ProfileData(Base): class ProfileData(Base):
__tablename__ = "profiledata" __tablename__ = "profiledata"
@ -871,44 +794,6 @@ class Event(Base):
self.time = datetime.datetime.now() + value self.time = datetime.datetime.now() + value
class GameProgress(enum.Enum):
NOT_STARTED = 0
IN_PROGRESS = 1
BEATEN = 2
COMPLETED = 3
MASTERED = 4
class GameOrigins(enum.Enum):
DIGITAL = 0
PHYSICAL = 1
SOLD = 2
BORROWED = 3
RENTED = 4
MYSTERIOUS = 5 # yarr
class LibraryGame(Base):
__tablename__ = "librarygames"
id = Column(BigInteger, primary_key=True)
owner_id = Column(Integer, ForeignKey("royals.id"), nullable=False)
owner = relationship("Royal", lazy="joined")
name = Column(String)
platform = Column(String)
steam_game_id = Column(BigInteger)
progress = Column(Enum(GameProgress), default=GameProgress.NOT_STARTED)
progress_notes = Column(Text)
time_played = Column(Float)
rating = Column(Integer)
review = Column(Text)
origin = Column(Enum(GameOrigins))
physical = Column(Boolean, default=False)
current_achievements = Column(Integer)
maximum_achievements = Column(Integer)
extra_notes = Column(Text)
# If run as script, create all the tables in the db # If run as script, create all the tables in the db
if __name__ == "__main__": if __name__ == "__main__":
print("Creating new tables...") print("Creating new tables...")

View file

@ -19,12 +19,6 @@ else:
print(e) print(e)
else: else:
session.add(dota) session.add(dota)
try:
rl = db.RocketLeague.create(session, steam.steam_id)
except Exception as e:
print(e)
else:
session.add(rl)
try: try:
osu = db.Osu.create(session, user.id, input("Osu! username: ")) osu = db.Osu.create(session, user.id, input("Osu! username: "))
except Exception as e: except Exception as e:

View file

@ -103,19 +103,22 @@ try:
finally: finally:
sleep_time = 1 - time.clock() + t sleep_time = 1 - time.clock() + t
time.sleep(sleep_time if sleep_time > 0 else 0) time.sleep(sleep_time if sleep_time > 0 else 0)
print("Committing...\t\t") print("Committing...", end="\t\t")
session.commit() session.commit()
print("OK") print("OK")
print("Closing...\n\n") print("Closing...", end="\n\n")
session.close() session.close()
print("OK") print("OK")
print("Waiting 1800s...\t\t") print("Waiting 1800s...", end="\t\t")
for i in range(0, 20): for i in range(0, 20):
time.sleep(90) time.sleep(90)
print("") print("", end="")
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
finally: finally:
print("Committing...", end="\t\t")
session.commit()
print("OK")
print("Closing...") print("Closing...")
try: try:
session.close() session.close()