1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00
royalnet/database.py

154 lines
5.3 KiB
Python
Raw Normal View History

2017-04-26 08:46:36 +00:00
import datetime
2017-03-28 15:43:07 +00:00
import sqlalchemy.exc
2017-04-28 16:54:20 +00:00
import discord
2017-03-30 10:13:10 +00: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 08:46:36 +00:00
import lol
2017-03-10 10:59:00 +00:00
2017-03-10 09:34:27 +00:00
class NoUsersMatchingError(Exception):
pass
class InvalidPasswordError(Exception):
pass
# Initialize the database
2017-03-10 09:11:06 +00:00
engine = create_engine("sqlite:///db.sqlite")
Base = declarative_base()
Session = sessionmaker(bind=engine)
2017-03-30 10:13:10 +00:00
class Diario(Base):
__tablename__ = "diario"
id = Column(Integer, primary_key=True)
2017-04-26 08:46:36 +00:00
2017-03-30 10:13:10 +00:00
text = Column(String, nullable=False)
date = Column(DateTime, nullable=False)
def __repr__(self):
return f"<Diario {self.date} {self.text}>"
2017-03-09 14:52:02 +00:00
2017-04-26 08:46:36 +00:00
class Account(Base):
__tablename__ = "account"
2017-03-30 10:13:10 +00:00
2017-04-26 08:46:36 +00:00
id = Column(Integer, primary_key=True)
2017-03-09 14:52:02 +00:00
2017-04-26 08:46:36 +00:00
lol = relationship("LoL")
2017-03-10 09:34:27 +00:00
2017-04-26 08:46:36 +00:00
class LoL(Base):
__tablename__ = "lol"
2017-03-10 09:34:27 +00:00
2017-04-26 08:46:36 +00:00
id = Column(Integer, primary_key=True)
2017-04-28 16:54:20 +00:00
parent_id = Column(Integer, ForeignKey("account.id"))
2017-04-26 08:46:36 +00:00
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 09:11:06 +00:00
2017-04-26 08:46:36 +00:00
def __repr__(self):
return f"<LoL {self.id} {self.summoner_name}>"
2017-03-10 09:11:06 +00:00
2017-04-26 08:46:36 +00:00
2017-04-29 14:59:02 +00:00
async def update_data(self):
2017-04-29 15:14:43 +00:00
# Copy the old stats
soloq_tier = self.soloq_tier
soloq_division = self.soloq_division
flexq_tier = self.flexq_tier
flexq_division = self.flexq_division
ttq_tier = self.ttq_tier
ttq_division = self.ttq_division
# Get and apply the new data
2017-04-29 14:59:02 +00:00
try:
soloq, flexq, ttq = await lol.get_rank_data("euw", self.id)
except lol.LoLAPIError as e:
# LoL returns 404 if the account is unranked
if e.status_code == 404:
self.soloq_tier = None
self.soloq_division = None
self.flexq_tier = None
self.flexq_division = None
self.ttq_tier = None
self.ttq_division = None
else:
# Update the user data
if soloq is not None:
self.soloq_tier = lol.tiers.index(soloq["tier"])
self.soloq_division = lol.divisions.index(soloq["entries"][0]["division"])
else:
self.soloq_tier = None
self.soloq_division = None
if flexq is not None:
self.flexq_tier = lol.tiers.index(flexq["tier"])
self.flexq_division = lol.divisions.index(flexq["entries"][0]["division"])
else:
self.flexq_tier = None
self.flexq_division = None
if ttq is not None:
self.ttq_tier = lol.tiers.index(ttq["tier"])
self.ttq_division = lol.divisions.index(ttq["entries"][0]["division"])
else:
self.ttq_tier = None
self.ttq_division = None
# Mark the user as updated
self.last_updated = datetime.datetime.now()
2017-04-29 15:14:43 +00:00
# Return if any stat has changed
return (soloq_tier != self.soloq_tier) or (soloq_division != self.soloq_division) or (flexq_tier != self.flexq_tier) or (flexq_division != self.flexq_division) or (ttq_tier != self.ttq_tier) or (ttq_division != self.ttq_division)
2017-04-29 14:59:02 +00:00
2017-04-28 16:54:20 +00:00
def generate_discord_embed(self):
embed = discord.Embed(type="rich")
# TODO: change the icon
embed.set_author(name="League of Legends", url="http://euw.leagueoflegends.com/", icon_url="https://cdn.discordapp.com/attachments/152150752523976704/307558194824216578/icon.png")
embed.add_field(name="Summoner", value=str(self.summoner_name))
embed.add_field(name="Level", value=str(self.level))
if self.soloq_tier is not None:
embed.add_field(name="Solo/duo SR", value=f"{lol.tiers[self.soloq_tier].capitalize()} {lol.divisions[self.soloq_division]}", inline=False)
embed.set_thumbnail(url=f"https://royal.steffo.me/loltiers/{lol.tiers[self.soloq_tier].lower()}_{lol.divisions[self.soloq_division].lower()}.png")
if self.flexq_tier is not None:
embed.add_field(name="Flex SR", value=f"{lol.tiers[self.flexq_tier].capitalize()} {lol.divisions[self.flexq_division]}", inline=False)
if self.ttq_tier is not None:
embed.add_field(name="Twisted Treeline", value=f"{lol.tiers[self.ttq_tier].capitalize()} {lol.divisions[self.ttq_division]}", inline=False)
embed.colour = discord.Colour(0x09AEBB)
return embed
2017-04-29 14:59:02 +00:00
2017-04-26 08:46:36 +00:00
Base.metadata.create_all(engine)
2017-03-29 07:44:32 +00:00
2017-03-30 10:13:10 +00: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 08:08:39 +00:00
def new_diario_entry(dt, text):
2017-03-30 10:13:10 +00: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()