pastebin - collaborative debugging tool
kpaste.net RSS


trakt json python windows
Posted by Anonymous on Fri 19th Oct 2012 15:12
raw | new post
modification of post by Anonymous (view diff)

  1. import json
  2. import urllib2
  3. import base64
  4. import os
  5. import datetime
  6. import sys
  7.  
  8. #config
  9. #base64string =
  10. base64string =
  11. apikey=
  12. username =
  13. apipath='/user/calendar/shows.json/'
  14. tmppath=
  15.  
  16.  
  17. #for color crap on windows
  18. from colorama import init
  19. init()
  20. from colorama import Style
  21.  
  22. #funcs
  23.  
  24.  
  25.  
  26. def user_cal_shows():
  27.         request = urllib2.Request("https://api.trakt.tv/user/calendar/shows.json/" + apikey + "/" + username)
  28.         request.add_header("Authorization", "Basic %s" % base64string)
  29.         return json.load(urllib2.urlopen(request))
  30.  
  31. def outputrun():
  32.     for i in range(len(out)):
  33.         for b in range(len(out[i]['episodes'])):
  34.             in_watch = out[i]['episodes'][b]['show']['in_watchlist'] #True
  35.             #make sure the show is in the watch list
  36.             if in_watch == True:
  37.                 showname = out[i]['episodes'][b]['show']['title']  #Fringe
  38.                 ep_name = out[i]['episodes'][b]['episode']['title']  #In Absentia
  39.                 network = out[i]['episodes'][b]['show']['network']  #FOX
  40.                 airtime = out[i]['episodes'][b]['show']['air_time']  #9:00pm
  41.                 season = out[i]['episodes'][b]['episode']['season']  #5
  42.                 ep_num = out[i]['episodes'][b]['episode']['number']  #2
  43.                 airday = out[i]['episodes'][b]['show']['air_day']  #Friday
  44.                 runtime = out[i]['episodes'][b]['show']['runtime']  #60v    
  45.                 print showname + " airs at " + Style.BRIGHT + airtime + Style.NORMAL +" on "+ Style.BRIGHT + airday + Style.NORMAL
  46.                 print "\t " + str(season) + "." + str(ep_num) + " - " + ep_name + Style.BRIGHT +" " + network + Style.NORMAL
  47.                 print "\t watchlist: " + str(in_watch) + " - runtime: " + str(runtime) + "min"
  48.  
  49. def update_json():
  50.     out = user_cal_shows()
  51.     #overwrites any data there
  52.     with open(tmppath+"data","wb") as fp:
  53.         json.dump(out,fp)
  54.     return out
  55.  
  56.  
  57. #caching, sees if cache folder is there, if not make it
  58. #if it is, it gets the date from the cache
  59. if not os.path.exists(tmppath+"data"):
  60.     if not os.path.exists(tmppath):
  61.         os.makedirs(tmppath)
  62.     out = update_json()
  63. else:
  64.     #load from the file
  65.     with open(tmppath+"data", "rb") as fp:
  66.         out = json.load(fp)
  67.    #compare the date
  68.     if str(out[0]['date']) != str(datetime.date.today()):
  69.        #not the same, update list
  70.        out = update_json()
  71.     if len(sys.argv) > 1:
  72.         if str(sys.argv[1]) == "reset":
  73.            out = update_json()
  74. outputrun()

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at