Fix for recent twitch API changes

This commit is contained in:
Twentysix 2016-03-23 15:25:25 +01:00
parent ba81bc78c9
commit 76723d5fef

View File

@ -70,12 +70,12 @@ class Streams:
async def twitch_alert(self, ctx, stream : str): async def twitch_alert(self, ctx, stream : str):
"""Adds/removes twitch alerts from the current channel""" """Adds/removes twitch alerts from the current channel"""
channel = ctx.message.channel channel = ctx.message.channel
check = await self.twitch_online(stream) check = await self.twitch_exists(stream)
if check == None: if check is False:
await self.bot.say("That stream doesn't exist.") await self.bot.say("That stream doesn't exist.")
return return
elif check == "error": elif check == "error":
await self.bot.say("Error.") await self.bot.say("Couldn't contact Twitch API. Try again later.")
return return
done = False done = False
@ -237,17 +237,14 @@ class Streams:
return "error" return "error"
async def twitch_online(self, stream): async def twitch_online(self, stream):
url = "https://api.twitch.tv/kraken/streams/" + stream url = "https://api.twitch.tv/kraken/streams?channel=" + stream
async with aiohttp.get(url) as r:
data = await r.json()
try: try:
if "stream" in data: async with aiohttp.get(url) as r:
if data["stream"] != None: data = await r.json()
return True if len(data["streams"]) > 0:
else: return True
return False else:
elif "error" in data: return False
return None
except: except:
return "error" return "error"
return "error" return "error"
@ -268,6 +265,18 @@ class Streams:
return "error" return "error"
return "error" return "error"
async def twitch_exists(self, stream):
url = "https://api.twitch.tv/channels/" + stream
try:
async with aiohttp.get(url) as r:
data = await r.json()
if "error" in data:
return False
else:
return True
except:
return "error"
async def stream_checker(self): async def stream_checker(self):
CHECK_DELAY = 60 CHECK_DELAY = 60