Created
July 13, 2018 09:21
Revisions
-
foxbot created this gist
Jul 13, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,69 @@ import aiohttp import asyncio import discord import os CHANNEL_ID = 381870553235193857 class State: def __init__(self, token): self.discord = discord.http.HTTPClient() self.discord.token = token self.session = aiohttp.ClientSession() self.home = {} self.away = {} self.events = set() async def run(self): while True: await self.fetch_update() await asyncio.sleep(15) async def fetch_update(self): resp = await self.session.get('https://worldcup.sfg.io/matches/today') data = await resp.json() match = data[0] self.home = match['home_team'] self.away = match['away_team'] async def process_event(event): id = event['id'] if id in self.events: return self.events.add(id) print('handling event %s' % id) if event['type_of_event'] == 'goal': msg = '**%s** %s - %s **%s**\n*%s*' % ( self.home['code'], self.home['goals'], self.away['goals'], self.away['code'], event['player'] ) print('sending GOAL') await self.discord.send_message(CHANNEL_ID, msg) if not 'home_team_events' in match or not 'away_team_events' in match: return for event in match['home_team_events']: await process_event(event) for event in match['away_team_events']: await process_event(event) token = os.getenv('TOKEN') if not token: print('no token') os.abort() token = 'Bot %s' % token state = State(token) print('hi') loop = asyncio.get_event_loop() loop.run_until_complete(state.run()) loop.close() print('bye')