1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2025-02-17 10:53:57 +00:00
royalnet/database.py

127 lines
3.4 KiB
Python
Raw Normal View History

2017-04-26 10:46:36 +02:00
import datetime
2017-03-28 17:43:07 +02:00
import sqlalchemy.exc
2017-03-30 12:13:10 +02:00
from sqlalchemy import create_engine, Column, Integer, String, Boolean, DateTime, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy.ext.declarative import declarative_base
2017-04-26 10:46:36 +02:00
import lol
2017-03-10 11:59:00 +01:00
2017-03-10 10:34:27 +01:00
class NoUsersMatchingError(Exception):
pass
class InvalidPasswordError(Exception):
pass
# Initialize the database
2017-03-10 10:11:06 +01:00
engine = create_engine("sqlite:///db.sqlite")
Base = declarative_base()
Session = sessionmaker(bind=engine)
2017-03-30 12:13:10 +02:00
class Diario(Base):
__tablename__ = "diario"
id = Column(Integer, primary_key=True)
2017-04-26 10:46:36 +02:00
2017-03-30 12:13:10 +02:00
text = Column(String, nullable=False)
date = Column(DateTime, nullable=False)
def __repr__(self):
return f"<Diario {self.date} {self.text}>"
2017-03-09 15:52:02 +01:00
2017-04-26 10:46:36 +02:00
class Account(Base):
__tablename__ = "account"
2017-03-30 12:13:10 +02:00
2017-04-26 10:46:36 +02:00
id = Column(Integer, primary_key=True)
2017-03-09 15:52:02 +01:00
2017-04-26 10:46:36 +02:00
lol = relationship("LoL")
2017-03-10 10:34:27 +01:00
2017-04-26 10:46:36 +02:00
class LoL(Base):
__tablename__ = "lol"
2017-03-10 10:34:27 +01:00
2017-04-26 10:46:36 +02:00
id = Column(Integer, primary_key=True)
parentid = Column(Integer, ForeignKey("account.id"))
last_updated = Column(DateTime)
summoner_name = Column(String, nullable=False)
level = Column(Integer)
soloq_division = Column(Integer)
soloq_tier = Column(Integer)
flexq_division = Column(Integer)
flexq_tier = Column(Integer)
ttq_division = Column(Integer)
ttq_tier = Column(Integer)
2017-03-10 10:11:06 +01:00
2017-04-26 10:46:36 +02:00
def __repr__(self):
return f"<LoL {self.id} {self.summoner_name}>"
2017-03-10 10:11:06 +01:00
2017-04-26 10:46:36 +02:00
Base.metadata.create_all(engine)
2017-03-29 09:44:32 +02:00
2017-03-30 12:13:10 +02:00
def migrate_diario():
import datetime
session = Session()
file = open("diario.txt", encoding="utf8")
for row in file:
entry = row.split("|", 1)
new = Diario()
new.date = datetime.datetime.fromtimestamp(int(entry[0]))
new.text = entry[1]
session.add(new)
session.commit()
2017-04-20 10:08:39 +02:00
def new_diario_entry(dt, text):
2017-03-30 12:13:10 +02:00
# Create a new session
session = Session()
# Create a new diario entry
entry = Diario()
entry.date = dt
entry.text = text
# Add the entry to the database
session.add(entry)
# Commit the change
session.commit()
2017-04-20 10:08:39 +02:00
2017-04-26 10:46:36 +02:00
2017-04-26 11:59:44 +02:00
# TODO: improve this
2017-04-26 10:46:36 +02:00
async def update_lol(lid):
# Create a new database session
session = Session()
# Find the user
user = session.query(Account).join(LoL).filter_by(id=lid).first()
# Poll the League API for more information
data = await lol.get_summoner_data("euw", summoner_id=user.lol.id)
# Update the user data
user.lol.summoner_name = data["name"]
user.lol.level = data["level"]
# Poll the League API for ranked data
soloq, flexq, ttq = await lol.get_rank_data("euw", lid)
# Update the user data
if soloq is not None:
user.lol.soloq_tier = lol.tiers[soloq["tier"]]
user.lol.soloq_division = lol.divisions[soloq["entries"][0]["division"]]
else:
user.lol.soloq_tier = None
user.lol.soloq_division = None
if flexq is not None:
user.lol.flexq_tier = lol.tiers[flexq["tier"]]
user.lol.flexq_division = lol.divisions[flexq["entries"][0]["division"]]
else:
user.lol.flexq_tier = None
user.lol.flexq_division = None
if ttq is not None:
user.lol.ttq_tier = lol.tiers[ttq["tier"]]
user.lol.ttq_division = lol.divisions[ttq["entries"][0]["division"]]
else:
user.lol.ttq_tier = None
user.lol.ttq_division = None
# Mark the user as updated
2017-04-26 11:59:44 +02:00
user.lol.last_updated = datetime.datetime.now()
# Commit the changes
session.commit()