import requests | |
import json | |
import re | |
import argparse | |
import random | |
import m3u8 | |
USHER_API = 'http://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 = 'http://api.twitch.tv/api/channels/{channel}/access_token' | |
def get_token_and_signature(channel): | |
url = TOKEN_API.format(channel=channel) | |
r = requests.get(url) | |
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) | |
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) | |
Doesn't work for me. URL's list is empty
Update: working, my bad
This script stopped working.
As far as I understand, twitch now requires oauth_token to get the access_token, this worked for me:
TOKEN_API
= 'https://api.twitch.tv/api/channels/{channel}/access_token?oauth_token={oauth_token}'
Ofcourse an oauth_token must be generated and assigned.
You can see my revision for a quick fix.
@GalPressman Thanks a lot for your fix. I didn't find a proper way to generate the oauth but from those 3rd party alike websites, generated one for IRC, but useful in this case.
@deng113jie You can read about it in the following link:
https://github.com/justintv/Twitch-API/blob/master/authentication.md#implicit-grant
According to some digging in their dev forums, it's possible that a client-id could be used instead of oauth_token, which is better since client-id is not secret and can be published inside the script. But this needs to be double checked.
https://api.twitch.tv/api/channels/[Channelname]/access_token?client_id=XXX [genereated on Connection-Site of twitch User Settings] works perfectly
Seems like twitch wants to keep track of applications using the api
So just to clarify things. Replace line 11 with:
TOKEN_API = 'http://api.twitch.tv/api/channels/{channel}/access_token?client_id=$CLIENT_ID'
Replace $CLIENT_ID with a client ID you get when you log into your twitch account, then settings -> connections and you have to register an application at the bottom. Then you get a client-id.
i got this error
Traceback (most recent call last):
File "/home/pi/etc/init.d/test.py", line 48, in
m3u8_obj = get_live_stream(args.channel_name)
File "/home/pi/etc/init.d/test.py", line 23, in get_live_stream
token, sig = get_token_and_signature(channel)
File "/home/pi/etc/init.d/test.py", line 18, in get_token_and_signature
sig = data['sig']
KeyError: 'sig'
Client key is inserted. Don´t know how to fix it
found my error. hashtag at the Beginning of the token was the failure
For any person who comes here in the future, I forked a fixed version of this script here:
https://gist.github.com/Josephvb10/a7ae56e6f9a9b4033b541c1b5c4458be
Don't forget to insert your client_id on line 11
I added the links yesterday and they have expired. How can i prevent this?
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'
You have a typo at
$allow_audio_only=true