Skip to content

Instantly share code, notes, and snippets.

@JosephTico
Last active August 12, 2019 01:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JosephTico/a7ae56e6f9a9b4033b541c1b5c4458be to your computer and use it in GitHub Desktop.
Save JosephTico/a7ae56e6f9a9b4033b541c1b5c4458be to your computer and use it in GitHub Desktop.
import requests
import json
import re
import argparse
import random
import m3u8
USHER_API = 'https://usher.twitch.tv/api/channel/hls/{channel}.m3u8?player=twitchweb' +\
'&token={token}&sig={sig}&$allow_audio_only=true&allow_source=true' + \
'&type=any&p={random}'
TOKEN_API = 'https://api.twitch.tv/api/channels/{channel}/access_token?client_id=###########'
def get_token_and_signature(channel):
url = TOKEN_API.format(channel=channel)
r = requests.get(url, verify=False)
txt = r.text
data = json.loads(txt)
sig = data['sig']
token = data['token']
return token, sig
def get_live_stream(channel):
token, sig = get_token_and_signature(channel)
r = random.randint(0,1E7)
url = USHER_API.format(channel=channel, sig=sig, token=token, random=r)
r = requests.get(url, verify=False)
m3u8_obj = m3u8.loads(r.text)
return m3u8_obj
def print_video_urls(m3u8_obj):
print("Video URLs (sorted by quality):")
for p in m3u8_obj.playlists:
si = p.stream_info
bandwidth = si.bandwidth/(1024)
quality = p.media[0].name
resolution = si.resolution if si.resolution else "?"
uri = p.uri
#print(p.stream_info, p.media, p.uri[1])
txt = "\n{} kbit/s ({}), resolution={}".format(bandwidth, quality, resolution)
print(txt)
print(len(txt)*"-")
print(uri)
if __name__=="__main__":
parser = argparse.ArgumentParser('get video url of twitch channel')
parser.add_argument('channel_name')
args = parser.parse_args()
m3u8_obj = get_live_stream(args.channel_name)
print_video_urls(m3u8_obj)
@mk-pmb
Copy link

mk-pmb commented Nov 15, 2018

Would be nice if you could add a shebang to the top:

#!/usr/bin/python

However, (line numbers w/o shebang):

$ python twitch_live_url.py gronkh
Traceback (most recent call last):
  File "twitch_live_url.py", line 48, in <module>
    m3u8_obj = get_live_stream(args.channel_name)
  File "twitch_live_url.py", line 23, in get_live_stream
    token, sig = get_token_and_signature(channel)
  File "twitch_live_url.py", line 18, in get_token_and_signature
    sig = data['sig']
KeyError: 'sig'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment