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

90 lines
2.9 KiB
Python
Raw Normal View History

2018-09-17 22:07:00 +00:00
import db
import time
import logging
import raven
import configparser
import os
import typing
import telegram
import sys
2018-09-17 22:26:01 +00:00
import coloredlogs
2018-09-17 22:07:00 +00:00
2018-09-18 22:02:39 +00:00
logging.getLogger().disabled = True
2018-09-17 22:07:00 +00:00
logger = logging.getLogger(__name__)
2018-09-18 22:13:41 +00:00
os.environ["COLOREDLOGS_LOG_FORMAT"] = "%(asctime)s %(levelname)s %(name)s %(message)s"
2018-09-17 22:26:01 +00:00
coloredlogs.install(level="DEBUG", logger=logger)
2018-09-17 22:07:00 +00:00
# Init the config reader
config = configparser.ConfigParser()
config.read("config.ini")
# Init the Sentry client
sentry = raven.Client(config["Sentry"]["token"],
release=raven.fetch_git_sha(os.path.dirname(__file__)),
install_logging_hook=False,
hook_libraries=[])
telegram_bot = telegram.Bot(config["Telegram"]["bot_token"])
def update_block(block: list, delay: float=0, change_callback: typing.Callable=None):
for item in block:
logger.debug(f"Updating {repr(item)}.")
t = time.clock()
try:
change = item.update()
except Exception as e:
logger.error(f"Error {sys.exc_info()} while updating {repr(item)}.")
sentry.extra_context({
"item": repr(item)
})
sentry.captureException()
continue
if change:
change_callback(item)
sleep_time = delay - time.clock() + t
time.sleep(sleep_time if sleep_time > 0 else 0)
def new_dota_rank(item: db.Dota):
try:
telegram_bot.send_message(config["Telegram"]["main_group"],
f"✳️ {item.steam.royal.username} è salito a"
f" {item.get_rank_name()} {item.get_rank_number()} su Dota 2!")
except Exception:
logger.warning(f"Couldn't notify on Telegram: {item}")
def new_lol_rank(item: db.LeagueOfLegends):
try:
telegram_bot.send_message(config["Telegram"]["main_group"],
f"✳️ {item.royal.username} è salito di rank su League of Legends!")
except Exception:
logger.warning(f"Couldn't notify on Telegram: {item}")
def process():
while True:
2018-09-17 22:11:28 +00:00
logger.info("Pausing for 30 minutes.")
time.sleep(1800)
2018-09-17 22:07:00 +00:00
session = db.Session()
2018-09-17 22:11:28 +00:00
logger.info("Now updating Steam data.")
update_block(session.query(db.Steam).all())
session.commit()
logger.info("Now updating Dota data.")
2018-09-18 22:02:39 +00:00
update_block(session.query(db.Dota).all(), delay=5, change_callback=new_dota_rank)
2018-09-17 22:11:28 +00:00
session.commit()
logger.info("Now updating League of Legends data.")
2018-09-18 22:02:39 +00:00
update_block(session.query(db.LeagueOfLegends).all(), delay=5, change_callback=new_lol_rank)
2018-09-17 22:11:28 +00:00
session.commit()
logger.info("Now updating osu! data.")
2018-09-18 22:02:39 +00:00
update_block(session.query(db.Osu).all(), delay=5)
2018-09-17 22:11:28 +00:00
session.commit()
2018-09-17 22:07:00 +00:00
logger.info("Now updating Overwatch data.")
2018-09-18 22:02:39 +00:00
update_block(session.query(db.Overwatch).all(), delay=5)
2018-09-17 22:07:00 +00:00
session.commit()
if __name__ == "__main__":
process()