Skip to content

Instantly share code, notes, and snippets.

@MLLeKander
Last active September 16, 2016 18:00
Show Gist options
  • Save MLLeKander/776dd1607165bb970ca2 to your computer and use it in GitHub Desktop.
Save MLLeKander/776dd1607165bb970ca2 to your computer and use it in GitHub Desktop.
Script to list Twitch streams
#!/usr/bin/env python2
import urllib2, json
from multiprocessing.dummy import Pool as ThreadPool
def auth_urlopen(url):
request = urllib2.Request(url)
request.add_header('Client-ID','8vbhkriv9mcwqfhar3zqfuckvcptbnq')
request.add_header('Accept','application/vnd.twitchtv.v2+json')
return urllib2.urlopen(request)
def followed_streams(username):
followed_response = json.load(auth_urlopen('https://api.twitch.tv/kraken/users/%s/follows/channels?api_version=2'%username))
followed_channels = [a['channel']['name'] for a in followed_response['follows']]
return ('Followed Streams', json.load(auth_urlopen('http://api.twitch.tv/kraken/streams?on_site=1&limit=5&channel=%s'%','.join(followed_channels))))
def read_twitch(game):
(short_name, game_name) = game
if short_name == 'followed':
return followed_streams(game_name)
return (short_name, json.load(auth_urlopen('http://api.twitch.tv/kraken/streams?on_site=1&game=%s&limit=3'%game_name)))
games = [
('Hearthstone','Hearthstone%3A+Heroes+of+Warcraft'),
('StarcraftII','StarCraft+II'),
('followed','OMGTallMonster'),
]
pool = ThreadPool(4)
for (short_name,data) in pool.map(read_twitch, games):
print short_name
for stream in data['streams']:
try:
channel = stream['channel']
print '%7d %10s : %s'%(stream['viewers'],channel['name'],channel['status'].replace("\n","\t").replace("\r",""))
except:
print 'Problem processing %s.' % channel['name']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment