Script that request data from twitch API crashes without errors | Python 3.6 over Ubuntu

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

I’m not sure what you have going on in your script, but hopefully this helps. Both V5 and New API examples.

import requests

chanid = "23161357" # LIRIK's channel ID
userlogin = "joshog"

client_id = "################################"

def request_datav5(id):
    headers = {"Client-ID": client_id, "Accept": "application/vnd.twitchtv.v5+json"}
    url = "https://api.twitch.tv/kraken/streams/" + chanid
    r = requests.get(url, headers=headers).json()
    return r
    # If a bad channel ID response will be:
    # {'error': 'Internal Server Error', 'status': 500, 'message': ''}

    # If streamer is offline response will be:
    # {'stream': None}

print("\r\nAPI V5 \r\n")
print(request_datav5(chanid))

def request_dataNewAPI(userlogin):
    headers = {"Client-ID": client_id, "Accept": "application/vnd.twitchtv.v5+json"}
    url = "https://api.twitch.tv/helix/streams?user_login=" + userlogin
    r = requests.get(url, headers=headers).json()
    return r
    # If a bad user login name or offline response will be:
    # {'data': [], 'pagination': {}}

print ("\r\nNew API \r\n")
print(request_dataNewAPI(userlogin))

Another thing you can look into is Webhooks:
https://dev.twitch.tv/docs/api/webhooks-reference/#topic-stream-changed

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.