diff --git a/.gitignore b/.gitignore index 099d0e4e..ac329a62 100644 --- a/.gitignore +++ b/.gitignore @@ -142,3 +142,4 @@ discordtoken.txt db.json leaguetoken.txt telegramtoken.txt +osutoken.txt diff --git a/main.py b/main.py index d9bff1bc..cbcad223 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ import strings as s import telegram import bs4 import brawlhalla +import osu loop = asyncio.get_event_loop() d_client = discord.Client() @@ -287,6 +288,49 @@ async def opendota_last_match(timeout): await asyncio.sleep(1) +async def osu_pp(timeout): + while True: + if discord_is_ready: + print("[Osu!] Starting pp check...") + for mode in range(0, 4): + for player in db: + try: + r = await osu.get_user(db[player]["osu"]["id"], mode) + except KeyError: + continue + else: + if r["pp_raw"] is not None: + pp = float(r["pp_raw"]) + else: + pp = 0 + if pp != 0: + try: + old = db[player]["osu"][str(mode)] + except KeyError: + old = 0 + if pp != old: + db[player]["osu"][str(mode)] = pp + f = { + "player": player, + "mode": s.osu_modes[mode], + "pp": int(pp), + "change": int(pp - old) + } + loop.create_task(send_event(s.osu_pp_change, **f)) + else: + db[player]["osu"][str(mode)] = 0.0 + f = open("db.json", "w") + json.dump(db, f) + f.close() + finally: + await asyncio.sleep(1) + print("[Osu!] Check successful.") + await asyncio.sleep(timeout) + else: + await asyncio.sleep(1) + + + # Send a new event to both Discord and Telegram async def send_event(eventmsg: str, player: str, **kwargs): # Create arguments dict @@ -309,20 +353,23 @@ async def send_event(eventmsg: str, player: str, **kwargs): # Send the message loop.create_task(telegram.send_message(msg, -2141322)) -loop.create_task(overwatch_status_change(600)) -print("[Overwatch] Added level up check to the queue.") +#loop.create_task(overwatch_status_change(600)) +#print("[Overwatch] Added level up check to the queue.") -loop.create_task(league_rank_change(900)) -print("[League] Added rank change check to the queue.") +#loop.create_task(league_rank_change(900)) +#print("[League] Added rank change check to the queue.") -loop.create_task(league_level_up(900)) -print("[League] Added level change check to the queue.") +#loop.create_task(league_level_up(900)) +#print("[League] Added level change check to the queue.") -loop.create_task(brawlhalla_update_mmr(7200)) -print("[Brawlhalla] Added mmr change check to the queue.") +#loop.create_task(brawlhalla_update_mmr(7200)) +#print("[Brawlhalla] Added mmr change check to the queue.") -loop.create_task(opendota_last_match(600)) -print("[OpenDota] Added last match check to the queue.") +#loop.create_task(opendota_last_match(600)) +#print("[OpenDota] Added last match check to the queue.") + +loop.create_task(osu_pp(600)) +print("[Osu!] Added pp change check to the queue.") try: loop.run_until_complete(d_client.start(token)) diff --git a/osu.py b/osu.py new file mode 100644 index 00000000..5262266c --- /dev/null +++ b/osu.py @@ -0,0 +1,22 @@ +import asyncio +import requests +loop = asyncio.get_event_loop() + +# Load Osu API key from the osutoken.txt file +file = open("osutoken.txt", "r") +token = file.read() +file.close() + +async def get_user(user, mode=0): + print("[Osu!] Getting profile data for {} mode {}".format(user, mode)) + params = { + "k": token, + "m": mode, + "u": user + } + # Get the data + r = await loop.run_in_executor(None, requests.get, 'https://osu.ppy.sh/api/get_user?k={k}&m={m}&u={u}'.format(**params)) + if r.status_code == 200: + return r.json()[0] + else: + raise Exception("[Osu!] Unhandled exception during the API request") diff --git a/strings.py b/strings.py index 7c084572..696ee7a7 100644 --- a/strings.py +++ b/strings.py @@ -44,6 +44,15 @@ brawlhalla_new_mmr = "{player} è passato da {oldmmr} MMR a **{mmr}** MMR su _Br # Dota: new match dota_new_match = "{player} ha **{result}** a _Dota 2_ con {k}/{d}/{a}, giocando **{hero}**!" +# Osu: modes +osu_modes = ["osu!", + "osu!taiko", + "osu!catch", + "osu!mania"] + +# Osu: moar pp +osu_pp_change = "{player} è salito a **{pp}pp** (+{change}) su _{mode}_ !" + # Win: past participle won = "vinto"