0

I'm looking for a solution to switch scenes in OBS automatically when going live/offline. I found this Code from an earlier question but it doesn't work for me. It just returns "None". Tanks!

import requests

TWITCH_STREAM_API_ENDPOINT_V5 = "https://api.twitch.tv/kraken/streams/bikestreaming"

API_HEADERS = {
    'Client-ID' : 'myID',
    'Accept' : 'application/vnd.twitchtv.v5+json',
}

reqSession = requests.Session()

def checkUser(userID): #returns true if online, false if not
    url = TWITCH_STREAM_API_ENDPOINT_V5.format(userID)

    try:
        req = reqSession.get(url, headers=API_HEADERS)
        jsondata = req.json()
        if 'stream' in jsondata:
            if jsondata['stream'] is not None: #stream is online
                #print('online')
                return True
            else:
                return False
                #print('offline')
    except Exception as e:
        print("Error checking user: ", e)
        return False

print(checkUser("bikestreaming"))

Felix
  • 1
  • 2

1 Answers1

0

Here is a much easier and simpler way of doing it that will be easier to debug:

import requests
import json

def is_live_stream(streamer_name, client_id):

    twitch_api_stream_url = "https://api.twitch.tv/kraken/streams/" \
                    + streamer_name + "?client_id=" + client_id

    streamer_html = requests.get(twitch_api_stream_url)
    streamer = json.loads(streamer_html.content)

    return streamer["stream"] is not None

# Call with is_live_stream("bikestreaming", your-client-id)

As before, true if streamer is live, false if streamer is not live. Code with try catch statements are hard to debug. This link explains twitch Client ID's: https://dev.twitch.tv/docs#client-id

OctopuSS7
  • 465
  • 2
  • 9
  • 1
    Thanks for the fas Answar. I'm not relay into programming so I'm sorry for being so dumb :). I tried calling with print(is_live_stream("bikestreaming","id")) and got some errors: Traceback (most recent call last): File "F:/Dateien/pycharm/obsSwitcher/main.py", line 15, in print(is_live_stream("bikestreaming", "id")) File "F:/Dateien/pycharm/obsSwitcher/main.py", line 12, in is_live_stream return streamer["stream"] is not None KeyError: 'stream' – Felix Mar 07 '21 at 12:23
  • Ok, so if you're not into programming, there are a few things I need to add to that answer. I'll update it in a few minutes. – OctopuSS7 Mar 07 '21 at 12:26
  • First, try creating new key, then try just re-running it, as there might have been a corrupted json data packet. – OctopuSS7 Mar 07 '21 at 12:49
  • You mean Client-ID? I tried but there are still coming this errors. – Felix Mar 07 '21 at 12:59
  • Same errors? Not really sure in that case, as the errors indicate that there is no 'stream' attribute. I think twitch have updated their api since I last did this. You'll have to look at the documentation. – OctopuSS7 Mar 07 '21 at 13:23
  • I now just tried "return streamer". No error appeared but "{'error': 'Bad Request', 'status': 400, 'message': 'Invalid client id specified'} " was printed. – Felix Mar 07 '21 at 13:29
  • Because you need to add a client id to make any request to the twitch API – OctopuSS7 Mar 07 '21 at 14:03