2017-04-26 08:46:36 +00:00
|
|
|
import datetime
|
2017-03-28 15:43:07 +00:00
|
|
|
import sqlalchemy.exc
|
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
|
2017-03-09 11:52:20 +00:00
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
2017-04-26 08:46:36 +00:00
|
|
|
import lol
|
2017-03-09 11:52:20 +00:00
|
|
|
|
2017-03-10 10:59:00 +00:00
|
|
|
|
2017-03-10 09:34:27 +00:00
|
|
|
class NoUsersMatchingError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidPasswordError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-03-09 11:52:20 +00:00
|
|
|
# Initialize the database
|
2017-03-10 09:11:06 +00:00
|
|
|
engine = create_engine("sqlite:///db.sqlite")
|
2017-03-09 11:52:20 +00:00
|
|
|
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)
|
|
|
|
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 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
|
|
|
|
|
|
|
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()
|
2017-04-20 08:08:39 +00:00
|
|
|
|
2017-04-26 08:46:36 +00:00
|
|
|
|
2017-04-28 10:55:36 +00:00
|
|
|
async def update_lol(discord_id):
|
2017-04-26 08:46:36 +00:00
|
|
|
# Create a new database session
|
|
|
|
session = Session()
|
|
|
|
# Find the user
|
2017-04-28 10:55:36 +00:00
|
|
|
user = session.query(Account).filter_by(id=discord_id).join(LoL).first()
|
|
|
|
for account in user.lol:
|
|
|
|
# Find the League of Legends ID
|
|
|
|
lid = account.id
|
|
|
|
# Poll the League API for more information
|
|
|
|
data = await lol.get_summoner_data("euw", summoner_id=lid)
|
|
|
|
# Update the user data
|
|
|
|
account.summoner_name = data["name"]
|
|
|
|
account.level = data["summonerLevel"]
|
|
|
|
# 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:
|
|
|
|
account.soloq_tier = lol.tiers[soloq["tier"]]
|
|
|
|
account.soloq_division = lol.divisions[soloq["entries"][0]["division"]]
|
|
|
|
else:
|
|
|
|
account.soloq_tier = None
|
|
|
|
account.soloq_division = None
|
|
|
|
if flexq is not None:
|
|
|
|
account.flexq_tier = lol.tiers[flexq["tier"]]
|
|
|
|
account.flexq_division = lol.divisions[flexq["entries"][0]["division"]]
|
|
|
|
else:
|
|
|
|
account.flexq_tier = None
|
|
|
|
account.flexq_division = None
|
|
|
|
if ttq is not None:
|
|
|
|
account.ttq_tier = lol.tiers[ttq["tier"]]
|
|
|
|
account.ttq_division = lol.divisions[ttq["entries"][0]["division"]]
|
|
|
|
else:
|
|
|
|
account.ttq_tier = None
|
|
|
|
account.ttq_division = None
|
|
|
|
# Mark the user as updated
|
|
|
|
account.last_updated = datetime.datetime.now()
|
2017-04-26 09:59:44 +00:00
|
|
|
# Commit the changes
|
|
|
|
session.commit()
|