From d796c69242fc04d4fafd489b4ee056d8e8050a83 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 8 Nov 2016 19:37:39 +0100 Subject: [PATCH 1/3] Added opendota.com support --- main.py | 40 ++++++++++++++++++++++++++++++++++++++-- opendota.py | 12 ++++++++++++ strings.py | 5 ++++- 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 opendota.py diff --git a/main.py b/main.py index 40b221ab..641e0004 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,11 @@ import asyncio import discord import json + +import opendota import overwatch import league +import steam import strings as s import telegram import bs4 @@ -215,7 +218,7 @@ async def brawlhalla_update_mmr(timeout): # Compare the mmr with the value saved in the database if mmr != old_mmr: # Send a message - loop.create_task(send_event(s.brawlhalla_new_mmr, player, mmr=mmr, oldmmr=old_mmr)) + loop.create_task(send_event(s.brawlhalla_new_mmr, player=player, mmr=mmr, oldmmr=old_mmr)) # Update database db[player]["brawlhalla"]["mmr"] = mmr f = open("db.json", "w") @@ -229,6 +232,40 @@ async def brawlhalla_update_mmr(timeout): else: await asyncio.sleep(1) +# Every timeout seconds, report the last match +async def opendota_last_match(timeout): + while True: + if discord_is_ready: + print("[OpenDota] Starting last match check...") + # Check for new dota match for every player in the database + for player in db: + try: + # TODO: Se uno non ha mai giocato a dota, cosa succede? Aggiungere handling + r = await opendota.get_latest_match(steam.convert_sid_1_3(db[player]["steam"]["steamid"])) + except KeyError: + continue + else: + try: + old_last = db[player]["dota"]["lastmatch"] + except KeyError: + old_last = 0 + last = r["match_id"] + if last > old_last: + # Send a message + loop.create_task(send_event(s.dota_new_match, player=player, k=r["kills"], d=r["deaths"], a=r["assists"])) + # Update database + db[player]["dota"]["lastmatch"] = last + f = open("db.json", "w") + json.dump(db, f) + f.close() + finally: + await asyncio.sleep(2) + print("[OpenDota] 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 @@ -251,7 +288,6 @@ 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.") diff --git a/opendota.py b/opendota.py new file mode 100644 index 00000000..43ead964 --- /dev/null +++ b/opendota.py @@ -0,0 +1,12 @@ +import asyncio +import requests +loop = asyncio.get_event_loop() + +async def get_latest_match(steamidtre: int): + print("[OpenDota] Getting latest match for: {steamid}".format(steamid=steamidtre)) + r = await loop.run_in_executor(None, requests.get, 'https://api.opendota.com/api/players/{steamidtre}/matches?limit=1'.format(steamidtre=steamidtre)) + if r.status_code == 200: + pj = r.json() + return pj[0] + else: + raise Exception("OpenDota request error") diff --git a/strings.py b/strings.py index 39d7bd7b..bc97c19b 100644 --- a/strings.py +++ b/strings.py @@ -39,4 +39,7 @@ league_roman_list = ["I", league_level_up = "{player} è salito al livello **{level}** su _League of Legends_!" # Brawlhalla: new MMR -brawlhalla_new_mmr = "{player} è passato da {oldmmr} MMR a **{mmr}** MMR su _Brawlhalla_!" \ No newline at end of file +brawlhalla_new_mmr = "{player} è passato da {oldmmr} MMR a **{mmr}** MMR su _Brawlhalla_!" + +# Dota: new match +dota_new_match = "{player} ha finito una partita a _Dota 2_ con {k}/{d}/{a}!" \ No newline at end of file From ac62ac1897b0800abd9bd7c08843b75da11ae798 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 8 Nov 2016 21:07:23 +0100 Subject: [PATCH 2/3] Now it works --- main.py | 21 +++++++++++---------- opendota.py | 3 ++- steam.py | 9 --------- 3 files changed, 13 insertions(+), 20 deletions(-) delete mode 100644 steam.py diff --git a/main.py b/main.py index 641e0004..f4439b22 100644 --- a/main.py +++ b/main.py @@ -5,7 +5,6 @@ import json import opendota import overwatch import league -import steam import strings as s import telegram import bs4 @@ -241,7 +240,7 @@ async def opendota_last_match(timeout): for player in db: try: # TODO: Se uno non ha mai giocato a dota, cosa succede? Aggiungere handling - r = await opendota.get_latest_match(steam.convert_sid_1_3(db[player]["steam"]["steamid"])) + r = await opendota.get_latest_match(db[player]["steam"]["steamid"]) except KeyError: continue else: @@ -288,18 +287,20 @@ 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.") try: loop.run_until_complete(d_client.start(token)) diff --git a/opendota.py b/opendota.py index 43ead964..85415126 100644 --- a/opendota.py +++ b/opendota.py @@ -2,7 +2,8 @@ import asyncio import requests loop = asyncio.get_event_loop() -async def get_latest_match(steamidtre: int): +async def get_latest_match(steamidtre: str): + steamidtre = steamidtre[1:-1].split(":")[2] print("[OpenDota] Getting latest match for: {steamid}".format(steamid=steamidtre)) r = await loop.run_in_executor(None, requests.get, 'https://api.opendota.com/api/players/{steamidtre}/matches?limit=1'.format(steamidtre=steamidtre)) if r.status_code == 200: diff --git a/steam.py b/steam.py deleted file mode 100644 index be68b1a2..00000000 --- a/steam.py +++ /dev/null @@ -1,9 +0,0 @@ -def convert_sid_1_3(steamid: int, group=False): - """Convert SteamID1 to SteamID3""" - accuniverse = steamid % 2 - if group: - acctype = 0x0170000000000000 - else: - acctype = 0x0110000100000000 - accid = (steamid - acctype - accuniverse) / 2 - return accid From a949a1a3a61bd7f670c398de6ab1a3180a34a2ce Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 22 Nov 2016 17:35:20 +0100 Subject: [PATCH 3/3] added dota last match support --- main.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index f4439b22..c3d461d0 100644 --- a/main.py +++ b/main.py @@ -287,20 +287,20 @@ 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.") try: loop.run_until_complete(d_client.start(token))