From aed099f76a8b09abf2639f92b08d560bafab34be Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 6 Feb 2019 20:11:10 +0100 Subject: [PATCH] Create old isaac runs parser --- db.py | 31 ++++++++++++++++++++++++++-- isaacfetcher.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 isaacfetcher.py diff --git a/db.py b/db.py index 841c6a26..d201afe4 100644 --- a/db.py +++ b/db.py @@ -1316,8 +1316,35 @@ class MatchPartecipation(Base): return f"" -class IsaacQuest(Base, Mini): - ... +class BindingOfIsaac(Base, Mini): + __tablename__ = "bindingofisaac" + + steam_id = Column(String, ForeignKey("steam.steam_id"), primary_key=True) + steam = relationship("Steam", backref="binding_of_isaac", lazy="joined") + + daily_victories = Column(Integer, default=0) + + def __repr__(self): + return f"" + + def recalc_victories(self): + raise NotImplementedError() # TODO + + +class BindingOfIsaacRun(Base): + __tablename__ = "bindingofisaacruns" + __table_args__ = (PrimaryKeyConstraint("date", "player_id"),) + + date = Column(Date) + + player_id = Column(String, ForeignKey("bindingofisaac.steam_id")) + player = relationship("BindingOfIsaac", backref="runs", lazy="joined") + + score = Column(BigInteger) + # time = Column(???) + + def __repr__(self): + return f"" # If run as script, create all the tables in the db diff --git a/isaacfetcher.py b/isaacfetcher.py new file mode 100644 index 00000000..2a2fedb0 --- /dev/null +++ b/isaacfetcher.py @@ -0,0 +1,55 @@ +import steamleaderboards +import datetime +import db +import time +import telegram +import configparser + + +def dates_generator(last_date: datetime.date): + date = datetime.date.today() + while True: + if date < last_date: + return + yield date + date -= datetime.timedelta(days=1) + + +session = db.Session() +players = session.query(db.BindingOfIsaac).all() + +config = configparser.ConfigParser() +config.read("config.ini") + +print("Fetching leaderboardgroup...") +isaac = steamleaderboards.LeaderboardGroup(250900) + +telegram_bot = telegram.Bot(config["Telegram"]["bot_token"]) + +for date in dates_generator(datetime.date(year=2017, month=1, day=3)): + lb_name = "{year:04d}{month:02d}{day:02d}_scores+".format(year=date.year, month=date.month, day=date.day) + print(f"Fetching {lb_name}...") + leaderboard = isaac.get(name=lb_name) + print(f"Finding players...") + runs = [] + for player in players: + entry = leaderboard.find_entry(player.steam_id) + if entry is None: + continue + print(f"Found new entry: {entry}") + run = db.BindingOfIsaacRun(player=player, score=entry.score, date=date) + runs.append(run) + session.add(run) + if len(runs) > 1: + runs.sort(key=lambda x: x.score) + best = runs[-1] + best.player.daily_victories += 1 + try: + telegram_bot.send_message(config["Telegram"]["main_group"], + f"🏆 **{best.player.steam.persona_name}** ha vinto la Daily Run di Isaac del {date.isoformat()}!", + parse_mode="HTML", disable_web_page_preview=True, disable_notification=True) + except Exception: + pass + session.commit() + print("Sleeping 5s...") + time.sleep(5)