1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-30 15:04:18 +00:00

holy cow it works

This commit is contained in:
Steffo 2019-01-09 16:04:57 +01:00
parent f952b3cc8b
commit 958c486e85
3 changed files with 52 additions and 18 deletions

1
.gitignore vendored
View file

@ -7,3 +7,4 @@ music.opus
opusfiles/ opusfiles/
ignored/* ignored/*
markovmodel.json markovmodel.json
prova.py

66
db.py
View file

@ -10,6 +10,7 @@ from sqlalchemy import Column, BigInteger, Integer, String, DateTime, ForeignKey
UniqueConstraint, PrimaryKeyConstraint, Boolean, LargeBinary, Text, Date, func UniqueConstraint, PrimaryKeyConstraint, Boolean, LargeBinary, Text, Date, func
from sqlalchemy.inspection import inspect from sqlalchemy.inspection import inspect
import requests import requests
import errors
from errors import NotFoundError, AlreadyExistingError, PrivateError from errors import NotFoundError, AlreadyExistingError, PrivateError
import re import re
import enum import enum
@ -73,15 +74,20 @@ def relationship_link_chain(starting_class, ending_class) -> typing.Optional[tup
for _relationship in set(relationships): for _relationship in set(relationships):
if _relationship.mapper in inspected: if _relationship.mapper in inspected:
continue continue
result = search(_relationship.mapper, chain + (_relationship,)) try:
if result is not None: return search(_relationship.mapper, chain + (_relationship,))
return result except errors.NotFoundError:
return None continue
raise errors.NotFoundError()
return search(inspect(starting_class), tuple()) return search(inspect(starting_class), tuple())
class Mini(object): class Mini(object):
"""Mixin for every table that has an associated mini."""
_mini_full_name = NotImplemented
_mini_name = NotImplemented
@classmethod @classmethod
def mini_get_all(cls, session: Session) -> list: def mini_get_all(cls, session: Session) -> list:
return session.query(cls).all() return session.query(cls).all()
@ -92,12 +98,20 @@ class Mini(object):
@classmethod @classmethod
def mini_get_single_from_royal(cls, session: Session, royal: "Royal"): def mini_get_single_from_royal(cls, session: Session, royal: "Royal"):
chain = recursive_relationship_name_search(cls, "royal") chain = relationship_link_chain(cls, Royal)
pass if chain is None:
chain = []
start = session.query(cls)
for connection in chain:
start = start.join(connection.mapper.class_)
start = start.filter(Royal.id == royal.id)
return start.one()
class Royal(Base, Mini): class Royal(Base, Mini):
__tablename__ = "royals" __tablename__ = "royals"
_mini_full_name = "Royalnet"
_mini_name = "ryg"
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
username = Column(String, unique=True, nullable=False) username = Column(String, unique=True, nullable=False)
@ -128,8 +142,9 @@ class Royal(Base, Mini):
return royal return royal
class Telegram(Base): class Telegram(Base, Mini):
__tablename__ = "telegram" __tablename__ = "telegram"
_mini_full_name = "Telegram"
royal_id = Column(Integer, ForeignKey("royals.id")) royal_id = Column(Integer, ForeignKey("royals.id"))
royal = relationship("Royal", backref="telegram", lazy="joined") royal = relationship("Royal", backref="telegram", lazy="joined")
@ -178,8 +193,10 @@ class Telegram(Base):
return royal.telegram return royal.telegram
class Steam(Base): class Steam(Base, Mini):
__tablename__ = "steam" __tablename__ = "steam"
_mini_full_name = "Steam"
_mini_name = "steam"
royal_id = Column(Integer, ForeignKey("royals.id")) royal_id = Column(Integer, ForeignKey("royals.id"))
royal = relationship("Royal", backref="steam", lazy="joined") royal = relationship("Royal", backref="steam", lazy="joined")
@ -270,8 +287,10 @@ class Steam(Base):
return royal.steam return royal.steam
class RocketLeague(Base): class RocketLeague(Base, Mini):
__tablename__ = "rocketleague" __tablename__ = "rocketleague"
_mini_full_name = "Rocket League"
_mini_name = "rl"
steam_id = Column(String, ForeignKey("steam.steam_id"), primary_key=True) steam_id = Column(String, ForeignKey("steam.steam_id"), primary_key=True)
steam = relationship("Steam", backref="rl", lazy="joined") steam = relationship("Steam", backref="rl", lazy="joined")
@ -331,8 +350,10 @@ class RocketLeague(Base):
return f"https://rocketleaguestats.com/assets/img/rocket_league/ranked/season_four/{rank}.png" return f"https://rocketleaguestats.com/assets/img/rocket_league/ranked/season_four/{rank}.png"
class Dota(Base): class Dota(Base, Mini):
__tablename__ = "dota" __tablename__ = "dota"
_mini_full_name = "DOTA 2"
_mini_name = "dota"
steam_id = Column(String, ForeignKey("steam.steam_id"), primary_key=True) steam_id = Column(String, ForeignKey("steam.steam_id"), primary_key=True)
steam = relationship("Steam", backref="dota", lazy="joined") steam = relationship("Steam", backref="dota", lazy="joined")
@ -442,8 +463,10 @@ class RomanNumerals(enum.Enum):
return self.name return self.name
class LeagueOfLegends(Base): class LeagueOfLegends(Base, Mini):
__tablename__ = "leagueoflegends" __tablename__ = "leagueoflegends"
_mini_full_name = "League of Legends"
_mini_name = "lol"
royal_id = Column(Integer, ForeignKey("royals.id")) royal_id = Column(Integer, ForeignKey("royals.id"))
royal = relationship("Royal", backref="lol", lazy="joined") royal = relationship("Royal", backref="lol", lazy="joined")
@ -539,8 +562,10 @@ class LeagueOfLegends(Base):
return loldata.get_champ_icon(champ["name"]) return loldata.get_champ_icon(champ["name"])
class Osu(Base): class Osu(Base, Mini):
__tablename__ = "osu" __tablename__ = "osu"
_mini_full_name = "osu!"
_mini_name = "osu"
royal_id = Column(Integer, ForeignKey("royals.id"), nullable=False) royal_id = Column(Integer, ForeignKey("royals.id"), nullable=False)
royal = relationship("Royal", backref="osu", lazy="joined") royal = relationship("Royal", backref="osu", lazy="joined")
@ -605,9 +630,11 @@ class Osu(Base):
return f"<db.Osu {self.osu_name}>" return f"<db.Osu {self.osu_name}>"
class Discord(Base): class Discord(Base, Mini):
__tablename__ = "discord" __tablename__ = "discord"
__table_args__ = tuple(UniqueConstraint("name", "discriminator")) __table_args__ = tuple(UniqueConstraint("name", "discriminator"))
_mini_full_name = "Discord"
_mini_name = "discord"
royal_id = Column(Integer, ForeignKey("royals.id")) royal_id = Column(Integer, ForeignKey("royals.id"))
royal = relationship("Royal", backref="discord", lazy="joined") royal = relationship("Royal", backref="discord", lazy="joined")
@ -650,8 +677,10 @@ class Discord(Base):
return f"https://cdn.discordapp.com/avatars/{self.discord_id}/{self.avatar_hex}.png?size={size}" return f"https://cdn.discordapp.com/avatars/{self.discord_id}/{self.avatar_hex}.png?size={size}"
class Overwatch(Base): class Overwatch(Base, Mini):
__tablename__ = "overwatch" __tablename__ = "overwatch"
_mini_full_name = "Overwatch"
_mini_name = "ow"
royal_id = Column(Integer, ForeignKey("royals.id"), nullable=False) royal_id = Column(Integer, ForeignKey("royals.id"), nullable=False)
royal = relationship("Royal", backref="overwatch", lazy="joined") royal = relationship("Royal", backref="overwatch", lazy="joined")
@ -995,9 +1024,11 @@ class LoginToken(Base):
return f"<LoginToken for {self.royal.username}>" return f"<LoginToken for {self.royal.username}>"
class Halloween(Base): class Halloween(Base, Mini):
"""This is some nice spaghetti, don't you think?""" """This is some nice spaghetti, don't you think?"""
__tablename__ = "halloween" __tablename__ = "halloween"
_mini_full_name = "Halloween 2018"
_mini_name = "halloween2018"
royal_id = Column(Integer, ForeignKey("royals.id"), primary_key=True) royal_id = Column(Integer, ForeignKey("royals.id"), primary_key=True)
royal = relationship("Royal", backref="halloween", lazy="joined") royal = relationship("Royal", backref="halloween", lazy="joined")
@ -1110,8 +1141,10 @@ class Quest(Base):
return f"<Quest {self.id}: {self.title}>" return f"<Quest {self.id}: {self.title}>"
class Terraria13(Base): class Terraria13(Base, Mini):
__tablename__ = "terraria13" __tablename__ = "terraria13"
_mini_full_name = "Terraria 13"
_mini_name = "terraria13"
game_name = "Terraria 13" game_name = "Terraria 13"
@ -1125,7 +1158,6 @@ class Terraria13(Base):
return f"<Terraria13 {self.character_name} {self.contribution}>" return f"<Terraria13 {self.character_name} {self.contribution}>"
# 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

@ -49,5 +49,6 @@ class VideoIsNotReady(Exception):
class PrivateError(Exception): class PrivateError(Exception):
pass pass
class PastDateError(Exception): class PastDateError(Exception):
pass pass