[Streams] Better Twitch error handling

This commit is contained in:
Twentysix 2016-09-17 04:03:40 +02:00
parent 2203c7b261
commit 2dcb1c88d1

View File

@ -39,8 +39,8 @@ class Streams:
else: else:
await self.bot.say("Error.") await self.bot.say("Error.")
@commands.command() @commands.command(pass_context=True)
async def twitch(self, stream: str): async def twitch(self, ctx, stream: str):
"""Checks if twitch stream is online""" """Checks if twitch stream is online"""
stream = escape_mass_mentions(stream) stream = escape_mass_mentions(stream)
online = await self.twitch_online(stream) online = await self.twitch_online(stream)
@ -49,8 +49,12 @@ class Streams:
"is online!".format(stream)) "is online!".format(stream))
elif online is False: elif online is False:
await self.bot.say(stream + " is offline.") await self.bot.say(stream + " is offline.")
elif online is None: elif online == 404:
await self.bot.say("That stream doesn't exist.") await self.bot.say("That stream doesn't exist.")
elif online == 400:
await self.bot.say("Owner: Client-ID is invalid or not set. "
"See `{}streamset twitchtoken`"
"".format(ctx.prefix))
else: else:
await self.bot.say("Error.") await self.bot.say("Error.")
@ -80,10 +84,15 @@ class Streams:
"""Adds/removes twitch alerts from the current channel""" """Adds/removes twitch alerts from the current channel"""
stream = escape_mass_mentions(stream) stream = escape_mass_mentions(stream)
channel = ctx.message.channel channel = ctx.message.channel
check = await self.twitch_exists(stream) check = await self.twitch_online(stream)
if check is False: if check == 404:
await self.bot.say("That stream doesn't exist.") await self.bot.say("That stream doesn't exist.")
return return
elif check == 400:
await self.bot.say("Owner: Client-ID is invalid or not set. "
"See `{}streamset twitchtoken`"
"".format(ctx.prefix))
return
elif check == "error": elif check == "error":
await self.bot.say("Couldn't contact Twitch API. Try again later.") await self.bot.say("Couldn't contact Twitch API. Try again later.")
return return
@ -286,15 +295,21 @@ 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?channel=" + stream session = aiohttp.ClientSession()
url = "https://api.twitch.tv/kraken/streams/" + stream
header = {'Client-ID': self.settings.get("TWITCH_TOKEN", "")} header = {'Client-ID': self.settings.get("TWITCH_TOKEN", "")}
try: try:
async with aiohttp.get(url, headers=header) as r: async with session.get(url, headers=header) as r:
data = await r.json() data = await r.json()
if len(data["streams"]) > 0: await session.close()
return True if r.status == 400:
else: return 400
elif r.status == 404:
return 404
elif data["stream"] is None:
return False return False
elif data["stream"]:
return True
except: except:
return "error" return "error"
return "error" return "error"
@ -315,19 +330,6 @@ class Streams:
return "error" return "error"
return "error" return "error"
async def twitch_exists(self, stream):
url = "https://api.twitch.tv/channels/" + stream
header = {'Client-ID': self.settings.get("TWITCH_TOKEN", "")}
try:
async with aiohttp.get(url, headers=header) 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