Is There Any Way To Check if a Twitch Stream Is Live Using Python?
Asked Answered
F

11

12

I'm just wondering if there is any way to write a python script to check to see if a twitch.tv stream is live?

I'm not sure why my app engine tag was removed, but this would be using app engine.

Fine answered 21/8, 2012 at 22:47 Comment(3)
There is always a way, you just have to find it. Next time please show us what you've done so far and also mention that you want it running on GAE no just tagging it as GAE.Nuriel
I used to do that in my old posts (mention GAE in the title), and people got mad and started telling me to just tag it.Fine
Well, if the question is not directly related to the GAE (like this one) just leave it out.Nuriel
O
5

It looks like Twitch provides an API (documentation here) that provides a way to get that info. A very simple example of getting the feed would be:

import urllib2

url = 'http://api.justin.tv/api/stream/list.json?channel=FollowGrubby'
contents = urllib2.urlopen(url)

print contents.read()

This will dump all of the info, which you can then parse with a JSON library (XML looks to be available too). Looks like the value returns empty if the stream isn't live (haven't tested this much at all, nor have I read anything :) ). Hope this helps!

Orvieto answered 21/8, 2012 at 22:57 Comment(3)
Oh why thank you. I'm not particularly well versed with JSON, but I'll do some reading! Thanks again!Fine
The Justin.tv API was shut down in 2014, meaning this answer no longer works.Heracliteanism
Unfortunately, this answer is not valid any more. I've posted a working solution below.Bakehouse
B
11

I hated having to go through the process of making an api key and all those things just to check if a channel was live, so i tried to find a workaround:

As of june 2021 if you send a http get request to a url like https://www.twitch.tv/CHANNEL_NAME, in the response there will be a "isLiveBroadcast": true if the stream is live, and if the stream is not live, there will be nothing like that.

So i wrote this code as an example in nodejs:

const fetch = require('node-fetch');
const channelName = '39daph';

async function main(){
    let a = await fetch(`https://www.twitch.tv/${channelName}`);
    if( (await a.text()).includes('isLiveBroadcast') )
        console.log(`${channelName} is live`);
    else
        console.log(`${channelName} is not live`);
}

main();

here is also an example in python:

import requests
channelName = '39daph'

contents = requests.get('https://www.twitch.tv/' +channelName).content.decode('utf-8')

if 'isLiveBroadcast' in contents: 
    print(channelName + ' is live')
else:
    print(channelName + ' is not live')
Bounden answered 14/6, 2021 at 11:39 Comment(2)
This isn't PythonRam
ops, didn't read that, i'll add an example in pythonBounden
R
9

Since all answers are actually outdated as of 2020-05-02, i'll give it a shot. You now are required to register a developer application (I believe), and now you must use an endpoint that requires a user-id instead of a username (as they can change).

See https://dev.twitch.tv/docs/v5/reference/users

and https://dev.twitch.tv/docs/v5/reference/streams

First you'll need to Register an application

From that you'll need to get your Client-ID.

The one in this example is not a real

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

API_HEADERS = {
    'Client-ID' : 'tqanfnani3tygk9a9esl8conhnaz6wj',
    '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
                return True
            else:
                return False
    except Exception as e:
        print("Error checking user: ", e)
        return False
Ram answered 5/2, 2020 at 12:14 Comment(3)
Hey i just tried to use your code and I'm really thankful that I found urs because your the only one whos using the v5 API but I'm still confused what the URL would be because mine returns a no client-id specified could you give me an example URL?Tambac
The variable reqSession is undefined.Heracliteanism
Use this instead: req = requests.Session().get(url, headers=API_HEADERS)Limbert
O
5

It looks like Twitch provides an API (documentation here) that provides a way to get that info. A very simple example of getting the feed would be:

import urllib2

url = 'http://api.justin.tv/api/stream/list.json?channel=FollowGrubby'
contents = urllib2.urlopen(url)

print contents.read()

This will dump all of the info, which you can then parse with a JSON library (XML looks to be available too). Looks like the value returns empty if the stream isn't live (haven't tested this much at all, nor have I read anything :) ). Hope this helps!

Orvieto answered 21/8, 2012 at 22:57 Comment(3)
Oh why thank you. I'm not particularly well versed with JSON, but I'll do some reading! Thanks again!Fine
The Justin.tv API was shut down in 2014, meaning this answer no longer works.Heracliteanism
Unfortunately, this answer is not valid any more. I've posted a working solution below.Bakehouse
A
5

RocketDonkey's fine answer seems to be outdated by now, so I'm posting an updated answer for people like me who stumble across this SO-question with google. You can check the status of the user EXAMPLEUSER by parsing

https://api.twitch.tv/kraken/streams/EXAMPLEUSER

The entry "stream":null will tell you that the user if offline, if that user exists. Here is a small Python script which you can use on the commandline that will print 0 for user online, 1 for user offline and 2 for user not found.

#!/usr/bin/env python3

# checks whether a twitch.tv userstream is live

import argparse
from urllib.request import urlopen
from urllib.error import URLError
import json

def parse_args():
    """ parses commandline, returns args namespace object """
    desc = ('Check online status of twitch.tv user.\n'
            'Exit prints are 0: online, 1: offline, 2: not found, 3: error.')
    parser = argparse.ArgumentParser(description = desc,
             formatter_class = argparse.RawTextHelpFormatter)
    parser.add_argument('USER', nargs = 1, help = 'twitch.tv username')
    args = parser.parse_args()
    return args

def check_user(user):
    """ returns 0: online, 1: offline, 2: not found, 3: error """
    url = 'https://api.twitch.tv/kraken/streams/' + user
    try:
        info = json.loads(urlopen(url, timeout = 15).read().decode('utf-8'))
        if info['stream'] == None:
            status = 1
        else:
            status = 0
    except URLError as e:
        if e.reason == 'Not Found' or e.reason == 'Unprocessable Entity':
            status = 2
        else:
            status = 3
    return status

# main
try:
    user = parse_args().USER[0]
    print(check_user(user))
except KeyboardInterrupt:
    pass
Astoria answered 13/5, 2014 at 1:42 Comment(3)
Hello. Could you make a Python27 version of this script with urllib2? I'm currently this but I don't get what the change in the parse_args to get it to work on python27 pastebin.com/7vkuPZ9J I found this but don't really understand what do change #16878815 Any help would be appreciated! Edit Got it to work!Keyboard
On the theme of providing up-to-date info, the Twitch API documentation is now here: github.com/justintv/Twitch-APIUnbelieving
Even this is now out of time and one requires registering an application with Twitch such that you can submit a clientid as GET data.Polarize
B
5

📚 Explanation

Now, the Twitch API v5 is deprecated. The helix API is in place, where an OAuth Authorization Bearer AND client-id is needed. This is pretty annoying, so I went on a search for a viable workaround, and found one.

🌎 GraphQL

When inspecting Twitch's network requests, while not being logged in, I found out the anonymous API relies on GraphQL. GraphQL is a query language for APIs.

query {
  user(login: "USERNAME") {
    stream {
      id
    }
  }
}

In the graphql query above, we are querying a user by their login name. If they are streaming, the stream's id will be given. If not, None will be returned.

🐍 The Final Code

The finished python code, in a function, is below. The client-id is taken from Twitch's website. Twitch uses the client-id to fetch information for anonymous users. It will always work, without the need of getting your own client-id.

import requests
# ...
def checkIfUserIsStreaming(username):
  url = "https://gql.twitch.tv/gql"
  query = "query {\n  user(login: \""+username+"\") {\n    stream {\n      id\n    }\n  }\n}"
  return True if requests.request("POST", url, json={"query": query, "variables": {}}, headers={"client-id": "kimne78kx3ncx6brgo4mv6wki5h1ko"}).json()["data"]["user"]["stream"] else False
I've created a website where you can play with Twitch's GraphQL API. Refer to the GraphQL Docs for help on GraphQL syntax! There's also Twitch GraphQL API documentation on my playground.
Bakehouse answered 27/2, 2022 at 23:44 Comment(4)
Awesome solution. Thanks for that. Does the website have additional GraphQL API examples or just the one from above? I couldn't get it to load.Blarney
Sorry about the website being down, here's the new link. In the top right, if you click < Docs, then a schema explorer will open. You can use that to construct queries.Bakehouse
@Bakehouse the website and new link you posted are both down, I was wondering if you had a backup or maybe a schema we can download?Hereinto
@Hereinto Apologies, I hosted it on Replit, but Replit seems to have removed their free tier hosting plan. I've re-uploaded the website files to Github Pages: https://epiccodewizard.github.io/Twitch-GraphQL-Playground/.Bakehouse
L
3

Here is a more up to date answer using the latest version of the Twitch API (helix). (kraken is deprecated and you shouldn't use GQL since it's not documented for third party use).

It works but you should store the token and reuse the token rather than generate a new token every time you run the script.


import requests

client_id = ''
client_secret = ''
streamer_name = ''

body = {
    'client_id': client_id,
    'client_secret': client_secret,
    "grant_type": 'client_credentials'
}
r = requests.post('https://id.twitch.tv/oauth2/token', body)

#data output
keys = r.json();

print(keys)

headers = {
    'Client-ID': client_id,
    'Authorization': 'Bearer ' + keys['access_token']
}

print(headers)

stream = requests.get('https://api.twitch.tv/helix/streams?user_login=' + streamer_name, headers=headers)

stream_data = stream.json();

print(stream_data);

if len(stream_data['data']) == 1:
    print(streamer_name + ' is live: ' + stream_data['data'][0]['title'] + ' playing ' + stream_data['data'][0]['game_name']);
else:
    print(streamer_name + ' is not live');
Lavena answered 8/3, 2021 at 20:2 Comment(0)
G
2

Use the twitch api with your client_id as a parameter, then parse the json:

https://api.twitch.tv/kraken/streams/massansc?client_id=XXXXXXX

Twitch Client Id is explained here: https://dev.twitch.tv/docs#client-id, you need to register a developer application: https://www.twitch.tv/kraken/oauth2/clients/new

Example:

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
Gladis answered 24/11, 2016 at 23:36 Comment(1)
This uses the v3 API, which was discontinued in 2019. Twitch now responds with {"error":"Gone","status":410,"message":"v3 is a lie but v5 is still alive. See https://dev.twitch.tv/docs"}Heracliteanism
C
2

I'll try to shoot my shot, just in case someone still needs an answer to this, so here it goes

import requests
import time
from twitchAPI.twitch import Twitch

client_id = ""
client_secret = ""

twitch = Twitch(client_id, client_secret)
twitch.authenticate_app([])

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

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

def checkUser(user): #returns true if online, false if not
    userid = twitch.get_users(logins=[user])['data'][0]['id']
    url = TWITCH_STREAM_API_ENDPOINT_V5.format(userid)
    try:
        req = requests.Session().get(url, headers=API_HEADERS)
        jsondata = req.json()
        if 'stream' in jsondata:
            if jsondata['stream'] is not None: 
                return True
            else:
                return False
    except Exception as e:
        print("Error checking user: ", e)
        return False

print(checkUser('michaelreeves'))
Canna answered 20/10, 2020 at 10:12 Comment(0)
N
2

https://dev.twitch.tv/docs/api/reference#get-streams

import requests
# ================================================================
# your twitch client id
client_id = '' 
# your twitch secret       
client_secret = ''
# twitch username you want to check if it is streaming online
twitch_user = ''                           
# ================================================================
#getting auth token
url = 'https://id.twitch.tv/oauth2/token'
params = {
    'client_id':client_id,
    'client_secret':client_secret,
    'grant_type':'client_credentials'}
req = requests.post(url=url,params=params)
token = req.json()['access_token']
print(f'{token=}')
# ================================================================
#getting user data (user id for example)
url = f'https://api.twitch.tv/helix/users?login={twitch_user}'
headers = {
    'Authorization':f'Bearer {token}',
    'Client-Id':f'{client_id}'}
req = requests.get(url=url,headers=headers)
userdata = req.json()
userid = userdata['data'][0]['id']
print(f'{userid=}')
# ================================================================
#getting stream info (by user id for example)
url = f'https://api.twitch.tv/helix/streams?user_id={userid}'
headers = {
    'Authorization':f'Bearer {token}',
    'Client-Id':f'{client_id}'}
req = requests.get(url=url,headers=headers)
streaminfo = req.json()
print(f'{streaminfo=}')
# ================================================================
Newcomer answered 17/10, 2021 at 14:5 Comment(1)
You can do https://api.twitch.tv/helix/streams?user_login={twitch_user} instead and save an API call!Lavena
S
1

This solution doesn't require registering an application

import requests

HEADERS = { 'client-id' : 'kimne78kx3ncx6brgo4mv6wki5h1ko' }
GQL_QUERY = """
query($login: String) {
    user(login: $login) {
        stream {
            id
        }
    }
}
"""

def isLive(username):
    QUERY = {
        'query': GQL_QUERY,
        'variables': {
            'login': username
        }
    }

    response = requests.post('https://gql.twitch.tv/gql',
                             json=QUERY, headers=HEADERS)
    dict_response = response.json()
    return True if dict_response['data']['user']['stream'] is not None else False


if __name__ == '__main__':
    USERS = ['forsen', 'offineandy', 'dyrus']
    for user in USERS:
        IS_LIVE = isLive(user)
        print(f'User {user} live: {IS_LIVE}')
Saxophone answered 23/11, 2020 at 19:52 Comment(0)
F
0

Yes. You can use Twitch API call https://api.twitch.tv/kraken/streams/YOUR_CHANNEL_NAME and parse result to check if it's live.

The below function returns a streamID if the channel is live, else returns -1.

import urllib2, json, sys
TwitchChannel = 'A_Channel_Name'
def IsTwitchLive(): # return the stream Id is streaming else returns -1
    url = str('https://api.twitch.tv/kraken/streams/'+TwitchChannel)
    streamID = -1
    respose = urllib2.urlopen(url)
    html = respose.read()
    data = json.loads(html)
    try:
       streamID = data['stream']['_id']
    except:
       streamID = -1
    return int(streamID)
Familiarize answered 14/5, 2016 at 11:35 Comment(1)
This no longer works you now get an error saying Bad request, no client idReefer

© 2022 - 2025 — McMap. All rights reserved.