Guest User

Untitled

a guest
Jan 5th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. from urllib2 import urlopen
  2. from urllib import urlencode
  3. from json import loads
  4. from subprocess import Popen
  5. import click
  6.  
  7. @click.command()
  8. @click.option('-g', '--game', default='', help='Load streams for specific game')
  9. @click.option('-l', '--limit', type=click.IntRange(1,100), default=10, help='Number of streams to load')
  10. @click.option('-t', '--top', is_flag=True, help='Open top stream right away')
  11. @click.option('-q', '--quality', type=click.Choice(['worst', 'low', 'high', 'medium', 'high', 'source', 'best']), default='high', help='Stream quality')
  12. def main(game, limit, top, quality):
  13.     params = urlencode({'limit': limit, 'game': game})
  14.     streams = loads(urlopen("https://api.twitch.tv/kraken/streams?{}".format(params)).read())["streams"]
  15.     if top:
  16.         streamNum = 1
  17.     else:
  18.         click.echo("Top twitch.tv streams right now:")
  19.         click.echo("\n".join(map(makeStreamInfoString, enumerate(streams))))
  20.         click.confirm('Want to watch something?', abort=True)
  21.         streamNum = click.prompt('Type stream number to start livestreamer', type=click.IntRange(1, len(streams)))
  22.     Popen(['livestreamer', streams[int(streamNum) - 1]["channel"]["url"], quality])
  23.  
  24. def makeStreamInfoString(streamEnum):
  25.     (i, stream) = streamEnum
  26.     channelName = stream["channel"]["name"]
  27.     channelStatus = stream["channel"]["status"]
  28.     game = stream["game"]
  29.     viewers = str(stream["viewers"])
  30.     return u"[{:02d}] {} || {} viewers on {} || {}".format(
  31.         i + 1,
  32.         channelStatus,
  33.         click.style(viewers, bold=True),
  34.         click.style(channelName, bold=True),
  35.         click.style(game, bold=True)
  36.     )
  37.  
  38. if __name__ == '__main__':
  39.     main()
Advertisement
Add Comment
Please, Sign In to add comment