Advertisement
hroafelme

Untitled

Jan 7th, 2015
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. """ Got it to work with this! """
  2.  
  3. import urllib2
  4. import json
  5.  
  6. url = 'https://api.twitch.tv/kraken/streams/sheevergaming'
  7. r = urllib2.urlopen(url)
  8. data = json.loads(r.read().decode(r.info().getparam('charset') or 'utf-8'))
  9.  
  10. if data['stream'] == None:
  11.     status = 1
  12. else:
  13.     status = 0
  14.  
  15. print status
  16.  
  17. ----
  18.    
  19.    #!/usr/bin/python
  20.    
  21.     # checks whether a twitch.tv userstream is live
  22.    
  23.     import argparse, urllib2
  24.     from urllib2 import Request
  25.     import json
  26.    
  27.     def parse_args():
  28.         """ parses commandline, returns args namespace object """
  29.         desc = ('Check online status of twitch.tv user.\n'
  30.                 'Exit prints are 0: online, 1: offline, 2: not found, 3: error.')
  31.         parser = argparse.ArgumentParser(description = desc,
  32.                  formatter_class = argparse.RawTextHelpFormatter)
  33.         parser.add_argument('USER', nargs = 1, help = 'twitch.tv username')
  34.         args = parser.parse_args()
  35.         return args
  36.    
  37.     def check_user(user):
  38.         """ returns 0: online, 1: offline, 2: not found, 3: error """
  39.         url = 'https://api.twitch.tv/kraken/streams/' + user
  40.         try:
  41.             info = json.loads(urllib2.urlopen(url, timeout = 15).read().decode('utf-8'))
  42.             if info['stream'] == None:
  43.                 status = 1
  44.             else:
  45.                 status = 0
  46.         except URLError as e:
  47.             if e.reason == 'Not Found' or e.reason == 'Unprocessable Entity':
  48.                 status = 2
  49.             else:
  50.                 status = 3
  51.         return status
  52.    
  53.     # main
  54.     try:
  55.         user = parse_args().USER[0]
  56.         print(check_user(user))
  57.     except KeyboardInterrupt:
  58.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement