59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
import plugin, requests, traceback
|
|
from contextlib import contextmanager
|
|
from functools import partialmethod
|
|
from datetime import datetime,tzinfo,timedelta
|
|
|
|
def readFile(filename):
|
|
with open(filename) as f:
|
|
return f.read()
|
|
|
|
GRAPHQL_URL = "https://api.platform.aaf.com/v1/graphql"
|
|
|
|
class UTC(tzinfo): # timestamps for the API are easier to do with UTC
|
|
def utcoffset(self,d):
|
|
return timedelta(0)
|
|
def tzname(self):
|
|
return "UTC"
|
|
def dst(self,d):
|
|
return timedelta(0)
|
|
|
|
def get_query():
|
|
gql = readFile("games.gql")
|
|
return gql
|
|
|
|
def query_games():
|
|
r = requests.get(GRAPHQL_URL,dict(query=get_query()),verify=False,headers={"Cache-Control":"no-cache"})
|
|
r.raise_for_status()
|
|
return r.json()["data"]["gamesConnection"]["nodes"]
|
|
|
|
def get_game():
|
|
games = query_games()
|
|
game = list(filter(lambda x: x["status"] is not None,games))
|
|
if len(game)<1:
|
|
return {"status":{"quarter":0}}
|
|
print(game)
|
|
return game[-1]
|
|
|
|
@plugin.group("aaf","<live>")
|
|
def aaf(bot,channel,nick,subcmd,*args):
|
|
if subcmd not in "live".split():
|
|
return True
|
|
|
|
@aaf.command("live")
|
|
def aaf_live(bot,channel,nick,subcmd,*args):
|
|
try:
|
|
game = get_game()
|
|
if game["status"]["quarter"]==0:
|
|
bot.say(channel,"{}: No games are active currently.".format(nick))
|
|
return
|
|
quarter = game["status"]["quarter"]
|
|
gameclock = ":".join([str(x) for x in divmod(game["clock"]["seconds"],60)])
|
|
status = game["status"]
|
|
if status["homeTeamPoints"]==status["awayTeamPoints"]:
|
|
bot.say(channel,"{}: Q{} {} Tied at {} ({} at {})".format(nick,quarter,gameclock,status["homeTeamPoints"],game["awayTeam"]["name"],game["homeTeam"]["name"]))
|
|
elif status["homeTeamPoints"]>status["awayTeamPoints"]:
|
|
bot.say(channel,"{}: Q{} {} {} {} - {} ({} at {})".format(nick,quarter,gameclock,game["homeTeam"]["name"],status["homeTeamPoints"],status["awayTeamPoints"],game["awayTeam"]["name"],game["homeTeam"]["name"]))
|
|
except Exception as e:
|
|
traceback.print_exc()
|
|
raise e
|