Advertisement
Guest User

addon.py

a guest
Sep 24th, 2015
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.42 KB | None | 0 0
  1. import xbmcaddon
  2. import xbmc
  3. import xbmcgui
  4. import xbmcplugin
  5. import sys
  6. import urllib
  7. import urllib2
  8. import urlparse
  9. import exceptions
  10. import json
  11.  
  12. # try:
  13. #   import StorageServer
  14. # except:
  15. #   import storageserverdummy as StorageServer
  16.  
  17. from datetime import timedelta
  18.  
  19. plugin_id = 'plugin.video.dplay'
  20.  
  21.  
  22. # thumbnails_cache_days = 0
  23. # thumbnails_cache = StorageServer.StorageServer(plugin_id, 24*int(thumbnails_cache_days))
  24.  
  25. addon = xbmcaddon.Addon(id=plugin_id)
  26. addonname = addon.getAddonInfo('name')
  27. addonicon = addon.getAddonInfo('icon')
  28.  
  29. this_plugin = int(sys.argv[1])
  30. plugin_url = sys.argv[0]
  31.  
  32. url_shows = 'http://it.dplay.com/api/v1/content/device/shows?realm=DPLAYIT&appVersion=2.0.0&platform=ANDROID' \
  33.            '&platformVersion=5.1.1&order=alphabetic&page={0}&limit=20&embed=genres%2Cchannels%2Chome_channel%2Cpackage'
  34. url_seasons = 'http://it.dplay.com/api/v1/content/device/shows/{0}/seasons?realm=DPLAYIT&appVersion=2.0.0' \
  35.              '&platform=ANDROID&platformVersion=5.1.1'
  36. url_episodes = 'http://it.dplay.com/api/v1/content/device/shows/{0}/seasons/{1}/videos?realm=DPLAYIT&appVersion=2.0.0' \
  37.               '&platform=ANDROID&platformVersion=5.1.1&page={2}&limit=20&' \
  38.               'embed=show%2Cchannels%2Chome_channel%2Cseason%2Cgenres%2Cvideo_cuepoints%2Cpackage'
  39. url_playback = 'https://secure.it.dplay.com/secure/api/v1/content/device/playback/{0}?realm=DPLAYIT' \
  40.               '&appVersion=2.0.0&platform=ANDROID&platformVersion=5.1.1'
  41. base_show_thumbnail_url = 'http://res.cloudinary.com/db79cecgq/image/upload/'
  42. user_agent = 'Dalvik/2.1.0 (Linux; U; Android 5.1.1; D6503 Build/23.4.A.0.546)'
  43.  
  44.  
  45. def params_url(query):
  46.     return plugin_url + '?' + urllib.urlencode(query)
  47.  
  48.  
  49. def get_duration(milliseconds):
  50.     return str(timedelta(milliseconds/1000.0))
  51.  
  52.  
  53. def get_cached_url(show):
  54.         crop = show['poster_image']['crop']
  55.         thumbnail_params = 'c_crop,h_'+str(crop['h'])+',w_'+str(crop['w'])+',x_'+str(crop['x'])+',y_'+str(crop['y'])+'/'
  56.         thumbnail_params += 'c_fill,h_246,w_368/'
  57.         return base_show_thumbnail_url + thumbnail_params + show['poster_image']['file']
  58.  
  59.  
  60. def all_shows():
  61.     i = 0
  62.     xbmcplugin.setContent(this_plugin, 'tvshows')
  63.     while True:
  64.         shows_url = url_shows.format(i)
  65.         try:
  66.             req = urllib2.Request(shows_url)
  67.             req.add_header('User-Agent', user_agent)
  68.             response = urllib2.urlopen(req)
  69.         except urllib2.HTTPError:
  70.                 break
  71.         data = json.loads(response.read())
  72.         i += 1
  73.         if len(data) == 0 or 'error' in data:
  74.             break
  75.         for show in data['data']:
  76.             if 'poster_image' in show:
  77.                 thumbnail_url = get_cached_url(show)
  78.             else:
  79.                 thumbnail_url = 'DefaultFolder.png'
  80.             label = show['title']+" - ("+str(show['episodes_available'])+" Episodi)"
  81.             litem = xbmcgui.ListItem(label, iconImage=thumbnail_url)
  82.             litem.setInfo('video', {
  83.                 'plot': show['description']
  84.             })
  85.             litem.setProperty('TotalEpisodes', str(show['episodes_available']))
  86.             litem.setProperty('TotalSeasons', str(show['seasons_available']))
  87.             xbmcplugin.addDirectoryItem(
  88.                 handle=this_plugin,
  89.                 url=params_url({'show_id': show['id']}),
  90.                 listitem=litem,
  91.                 isFolder=True
  92.             )
  93.     xbmcplugin.endOfDirectory(this_plugin)
  94.  
  95.  
  96. def get_seasons(show_id):
  97.     season_url = url_seasons.format(show_id)
  98.     xbmcplugin.setContent(this_plugin, 'episodes')
  99.     try:
  100.         req = urllib2.Request(season_url)
  101.         req.add_header('User-Agent', user_agent)
  102.         response = urllib2.urlopen(req)
  103.     except urllib2.HTTPError:
  104.         print('HTTPERROR')
  105.     data = json.loads(response.read())
  106.     if len(data) == 0:
  107.         return
  108.     for season in data['data']:
  109.         thumbnail_url = 'DefaultFolder.png'
  110.         label = season['name'] + " ({0} episodi su {1} disponibili)".format(
  111.             season['episodes_available'],
  112.             season['episodes_total']
  113.         )
  114.         litem = xbmcgui.ListItem(label,
  115.                                  iconImage=thumbnail_url
  116.                                  )
  117.         litem.setInfo('video', {
  118.             'plot': season['name']
  119.         })
  120.         xbmcplugin.addDirectoryItem(
  121.             handle=this_plugin,
  122.             url=params_url({
  123.                 'show_id': show_id,
  124.                 'season_number': season['season_number'],
  125.                 'season_id': season['id']
  126.             }),
  127.             listitem=litem,
  128.             isFolder=True
  129.         )
  130.     xbmcplugin.endOfDirectory(this_plugin)
  131.     return
  132.  
  133.  
  134. def get_episodes(show_id, season_id):
  135.     i = 0
  136.     xbmcplugin.setContent(this_plugin, 'episodes')
  137.     while True:
  138.         epUrl = url_episodes.format(show_id, season_id, i)
  139.         try:
  140.             req = urllib2.Request(epUrl)
  141.             req.add_header('User-Agent', user_agent)
  142.             response = urllib2.urlopen(req)
  143.         except urllib2.HTTPError:
  144.             break
  145.         try:
  146.             data = json.loads(response.read())
  147.         except exceptions.ValueError:
  148.             break
  149.         i += 1
  150.         print(response.getcode())
  151.         if response.getcode() != 200:
  152.             break
  153.         if len(data) == 0 or 'error' in data:
  154.             break
  155.         for episode in data['data']:
  156.             if episode['thumbnail_image'] is not None:
  157.                 crop = episode['thumbnail_image']['crop']
  158.                 thumbnail_params = 'c_crop,h_'+str(crop['h'])+',w_'+str(crop['w'])+',x_'\
  159.                                   + str(crop['x'])+',y_'+str(crop['y'])+'/'
  160.                 thumbnail_params += 'c_fill,h_246,w_368/'
  161.                 thumbnail_url = base_show_thumbnail_url + thumbnail_params + episode['thumbnail_image']['file']
  162.             else:
  163.                 thumbnail_url = 'DefaultVideo.png'
  164.             label = episode['title']
  165.             litem = xbmcgui.ListItem(label,
  166.                                      iconImage=thumbnail_url
  167.                                      )
  168.             litem.setInfo('video', {
  169.                 'plot': episode['description'],
  170.                 'duration': get_duration(episode['duration'])
  171.             })
  172.             litem.setProperty('IsPlayable', 'true')
  173.             xbmcplugin.addDirectoryItem(
  174.                 handle=this_plugin,
  175.                 url=params_url({
  176.                     'episode_id': episode['id']
  177.                 }),
  178.                 listitem=litem
  179.             )
  180.     xbmcplugin.endOfDirectory(this_plugin)
  181.  
  182.  
  183. def get_video_url(episode_id):
  184.     print('get_video_url')
  185.     playback_url = url_playback.format(episode_id)
  186.     try:
  187.         req = urllib2.Request(playback_url)
  188.         req.add_header('User-Agent', user_agent)
  189.         response = urllib2.urlopen(req)
  190.     except urllib2.HTTPError:
  191.         print("HTTPERROR")
  192.     data = json.loads(response.read())
  193.     url = data['data']['stream_url']
  194.     xbmcplugin.setResolvedUrl(this_plugin, True, xbmcgui.ListItem(path=url))
  195.     xbmc.executebuiltin('XBMC.PlayerControl(Play)')
  196.  
  197.  
  198. args = urlparse.parse_qs(sys.argv[2][1:])
  199. showid = args.get('show_id', None)
  200. seasonid = args.get('season_id', None)
  201. episodeid = args.get('episode_id', None)
  202.  
  203. if episodeid:
  204.     get_video_url(episodeid[0])
  205. elif seasonid:
  206.     get_episodes(showid[0], seasonid[0])
  207. elif showid:
  208.     get_seasons(showid[0])
  209. else:
  210.     all_shows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement