1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Added opendota.com support

This commit is contained in:
Steffo 2016-11-08 19:37:39 +01:00
parent 00820f1454
commit d796c69242
3 changed files with 54 additions and 3 deletions

40
main.py
View file

@ -1,8 +1,11 @@
import asyncio import asyncio
import discord import discord
import json import json
import opendota
import overwatch import overwatch
import league import league
import steam
import strings as s import strings as s
import telegram import telegram
import bs4 import bs4
@ -215,7 +218,7 @@ async def brawlhalla_update_mmr(timeout):
# Compare the mmr with the value saved in the database # Compare the mmr with the value saved in the database
if mmr != old_mmr: if mmr != old_mmr:
# Send a message # 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 # Update database
db[player]["brawlhalla"]["mmr"] = mmr db[player]["brawlhalla"]["mmr"] = mmr
f = open("db.json", "w") f = open("db.json", "w")
@ -229,6 +232,40 @@ async def brawlhalla_update_mmr(timeout):
else: else:
await asyncio.sleep(1) 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 # Send a new event to both Discord and Telegram
async def send_event(eventmsg: str, player: str, **kwargs): async def send_event(eventmsg: str, player: str, **kwargs):
# Create arguments dict # Create arguments dict
@ -251,7 +288,6 @@ async def send_event(eventmsg: str, player: str, **kwargs):
# Send the message # Send the message
loop.create_task(telegram.send_message(msg, -2141322)) loop.create_task(telegram.send_message(msg, -2141322))
loop.create_task(overwatch_status_change(600)) loop.create_task(overwatch_status_change(600))
print("[Overwatch] Added level up check to the queue.") print("[Overwatch] Added level up check to the queue.")

12
opendota.py Normal file
View file

@ -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")

View file

@ -40,3 +40,6 @@ league_level_up = "{player} è salito al livello **{level}** su _League of Legen
# Brawlhalla: new MMR # Brawlhalla: new MMR
brawlhalla_new_mmr = "{player} è passato da {oldmmr} MMR a **{mmr}** MMR su _Brawlhalla_!" 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}!"