I got a script that request data from twitch API that should check if a streamer is in live or he’s not.
The script where less substantial at its origin, but then error comes up and as you can see there are a lot of issue that i try to catch like for example connection errors.
def request_data(id):
#HTTP REQUEST
link = 'https://api.twitch.tv/kraken/streams/' + id
accept = 'application/vnd.twitchtv.v5+json'
client_id = '##################'
r1 = r2 = r3 = data = None
print('join request_data')
print('join request_data first try')
try:
r1 = requests.get(link, headers={'Client-ID':client_id, 'Accept': accept})
except Exception as error:
print('(1)Error: '+ str(error))
return data
time.sleep(1) #sec
if r1.status_code == 200:
print('entro in request_data r1.status_code == 200')
try:
r2 = r1.text
except:
print('(2)Error: '+ str(error))
return data
if 'stream' in r2:
print('join request_data "stream" in r2')
try:
r3 = r1.json()['stream']
except Exception as error:
print('(3)Error: '+ str(error))
return data
print('join in request_data second try')
if r3 != None:
print('join in request_data r3 != None')
try:
data = r3['broadcast_platform']
except Exception as error:
print('(4)Error: '+ str(error))
print('end of request_data')
return data
The code works fine, when someone goes live a get the notification, but randomly (as it seems to me) the script stop working and when I try to stop it using Ctrl + c the script won’t stop and i have to use Ctrl + z to stop it.
When I open the file where the output is writted the last line is always different…
“join request_data” writted by print(‘join request_data’) row 65
“join request_data second try” writted by print(‘join request_data second try’) row 89
As you can see there are a lot of try except one for each statement(or instruction idk how to say it)
and none of them seems to catch errors, cause I cannot find them searchin into the output file.
Script also here if you’re more comfortable reading it there: https://del.dog/odopiduqab
Python version 3.6