Last active
August 29, 2015 14:12
-
-
Save Telling/e7488eadf6c4d7809431 to your computer and use it in GitHub Desktop.
Python script to show whom of those one follow on twitch.tv is live and capable of opening them with livestreamer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" twinfo will show you which of the channels you follow are currently live, | |
and allow you to input a number to open it with livestreamer[0]. | |
[0]: https://github.com/chrippa/livestreamer | |
""" | |
import grequests | |
import requests | |
import argparse | |
import os | |
class Twinfo: | |
def __init__(self, username): | |
self.username = username | |
self.create_url_list() | |
self.make_async_requests() | |
self.create_lookup_list() | |
self.live_channels() | |
self.open_livestreamer() | |
def create_url_list(self): | |
payload = {'limit': '100'} | |
url = 'https://api.twitch.tv/kraken/users/{}/follows/channels'.format( | |
self.username | |
) | |
r = requests.get(url, params=payload) | |
template = 'https://api.twitch.tv/kraken/streams/{}' | |
self.urls = [ | |
template.format(c['channel']['name']) for c in r.json()['follows'] | |
] | |
def make_async_requests(self): | |
rs = (grequests.get(u) for u in self.urls) | |
self.streams = grequests.map(rs) | |
def create_lookup_list(self): | |
live_list = [] | |
for stream in self.streams: | |
stream_json = stream.json()['stream'] | |
if stream_json: | |
live_list.append(stream_json['channel']['url']) | |
index_list = list(xrange(len(live_list))) | |
self.lookup_list = dict(zip(index_list, live_list)) | |
def live_channels(self): | |
headline = 'Live channels that {} follows:'.format(self.username) | |
headline_length = len(headline) | |
print '*' * headline_length | |
print headline | |
print '*' * headline_length | |
for index, stream in self.lookup_list.iteritems(): | |
print ' {0}: {1}'.format(index, stream[21:]) | |
print '-' * headline_length | |
def open_livestreamer(self): | |
index = raw_input('\nEnter channel number (or q to quit): ') | |
if index == 'q': | |
return | |
else: | |
print 'Opening in livestreamer...\n' | |
os.system('livestreamer {} best'.format( | |
self.lookup_list[int(index)]) | |
) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument('username', help="Your twitch.tv username") | |
args = parser.parse_args() | |
Twinfo(args.username) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment