def stream_live_check(stream):
url = "https://api.twitch.tv/kraken/streams/{}".format(stream.lower())
try:
contents = json.loads(urllib.request.urlopen(url).read().decode("utf-8"))
if contents["stream"] == None:
status = "offline"
bot_message = "{} is offline.".format(stream)
else:
#print(contents)
name = contents["stream"]["channel"]["name"]
title = contents["stream"]["channel"]["status"]
game = contents["stream"]["channel"]["game"]
viewers = contents["stream"]["viewers"]
bot_message = "{0} is online.\n{0}'s title is: {1} \n{0} is playing {2} \nThere are {3} viewers \n".format(name,title,game,viewers)
except urllib.error.URLError as e:
if e.reason == "Not found" or e.reason == "Unprocessable Entity":
bot_message = "That stream doesn't exist."
else:
bot_message = "There was an error proccessing your request."
return bot_message
评论列表
文章目录