Skip to content

Instantly share code, notes, and snippets.

@LongJohnCoder
Created January 23, 2021 04:46
Show Gist options
  • Select an option

  • Save LongJohnCoder/edb382b91c4b2caab79535c03df33243 to your computer and use it in GitHub Desktop.

Select an option

Save LongJohnCoder/edb382b91c4b2caab79535c03df33243 to your computer and use it in GitHub Desktop.

Revisions

  1. @baderj baderj revised this gist Jan 5, 2015. 1 changed file with 27 additions and 8 deletions.
    35 changes: 27 additions & 8 deletions twitch_live_url.py
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,12 @@
    import json
    import re
    import argparse
    import random
    import m3u8

    USHER_API = 'http://usher.twitch.tv/select/{channel}.json' +\
    '?nauthsig={sig}&nauth={token}&allow_source=true'
    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):
    @@ -18,15 +21,31 @@ def get_token_and_signature(channel):

    def get_live_stream(channel):
    token, sig = get_token_and_signature(channel)
    url = USHER_API.format(channel=channel, sig=sig, token=token)
    r = random.randint(0,1E7)
    url = USHER_API.format(channel=channel, sig=sig, token=token, random=r)
    r = requests.get(url)
    txt = r.text
    for line in txt.split('\n'):
    if re.match('https?://.*', line):
    return line
    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()
    print( get_live_stream(args.channel_name) )
    m3u8_obj = get_live_stream(args.channel_name)
    print_video_urls(m3u8_obj)


  2. Johannes Bader revised this gist Jan 27, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion twitch_live_url.py
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,6 @@ 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']
  3. Johannes Bader created this gist Jan 24, 2014.
    33 changes: 33 additions & 0 deletions twitch_live_url.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    import requests
    import json
    import re
    import argparse

    USHER_API = 'http://usher.twitch.tv/select/{channel}.json' +\
    '?nauthsig={sig}&nauth={token}&allow_source=true'
    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)
    url = USHER_API.format(channel=channel, sig=sig, token=token)
    r = requests.get(url)
    txt = r.text
    for line in txt.split('\n'):
    if re.match('https?://.*', line):
    return line

    if __name__=="__main__":
    parser = argparse.ArgumentParser('get video url of twitch channel')
    parser.add_argument('channel_name')
    args = parser.parse_args()
    print( get_live_stream(args.channel_name) )