-
-
Save jedahan/c63a532af439ec859509 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
#------------------------------------------------- | |
# file: twitcher.py | |
# author: Florian Ehmke | |
# description: dmenu for twitch streams | |
#------------------------------------------------- | |
import argparse | |
import requests | |
from subprocess import Popen, PIPE, STDOUT | |
class Stream(object): | |
def __init__(self, name, status, viewers): | |
self.name = name | |
self.status = status | |
self.viewers = viewers | |
def display_string(self): | |
display_string = u"{} ({}) - {}\n".format(self.name, self.viewers, self.status).strip() | |
return display_string + "\n" | |
def create_arg_parser(): | |
parser = argparse.ArgumentParser(description= | |
'Open twitch stream through dmenu.') | |
parser.add_argument( | |
'-c', '--count', required=False, default=10, type=int, | |
help='How many (default: 10) streams should be displayed?') | |
parser.add_argument( | |
'-g', '--game', required=False, default="StarCraft II%3A Heart of the Swarm", type=str, | |
help='Which game (default: Starcraft II: Heart of the Swarm)?') | |
parser.add_argument( | |
'-q', '--quality', required=False, default="best", type=str, | |
help='Which quality (default: best)?') | |
parser.add_argument( | |
'-p', '--player', required=False, default="mpv", type=str, | |
help='Which player (default: mpv)?') | |
return vars(parser.parse_args()); | |
def main(): | |
args = create_arg_parser() | |
count = args['count'] | |
game = args['game'] | |
quality = args['quality'] | |
player = args['player'] | |
dmenu_command = ['dmenu', '-l', str(count), | |
'-nb', '#2D2D2D', '-nf', '#899CA1', | |
'-sb', '#2D2D2D', '-sf', '#C0C0C0', | |
'-fn', "-*-terminus-medium-*-*-*-16-*-*-*-*-*-*-*"] | |
url = "https://api.twitch.tv/kraken/streams?limit=100&game=" \ | |
"{}&limit={}".format(game.replace(' ','+'), count) | |
r = requests.get(url) | |
json_streams = r.json()["streams"] | |
streams = [] | |
for json_stream in json_streams: | |
channel = json_stream["channel"] | |
name = channel["name"] | |
status = channel["status"] | |
viewers = json_stream["viewers"] | |
streams.append(Stream(name, status, viewers)) | |
dmenu_str = "" | |
for stream in streams: | |
dmenu_str += stream.display_string() | |
dmenu_str.strip() | |
p = Popen(dmenu_command, stdout=PIPE, stdin=PIPE, stderr=STDOUT) | |
stream_selection = p.communicate(input=dmenu_str.encode('utf-8'))[0] | |
if stream_selection: | |
livestreamer_url = "twitch.tv/{} {} -p {}".format(stream_selection.decode('utf-8').split()[0], quality, player) | |
print(livestreamer_url) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @vimbaer and you both for this!
I forked it with some additionnal stuff. You can pass options for dmenu styling and the game param is now empty by default to show a global list of viewed streams (without game filtering).