Advertisement
alexdunlop81

syfy.py

May 2nd, 2013
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.97 KB | None | 0 0
  1. import xbmcplugin
  2. import xbmc
  3. import xbmcgui
  4. import urllib
  5. import urllib2
  6. import sys
  7. import os
  8. import re
  9.  
  10. from BeautifulSoup import BeautifulSoup
  11. from BeautifulSoup import BeautifulStoneSoup
  12. import demjson
  13. from pyamf.remoting.client import RemotingService
  14. import resources.lib._common as common
  15.  
  16. pluginhandle = int (sys.argv[1])
  17.  
  18. #BASE_URL = 'http://www.syfy.com/rewind/'
  19. BASE_URL = 'http://feed.theplatform.com/f/hQNl-B/sgM5DlyXAfwt/categories?form=json&fields=order,title,fullTitle,label,:smallBannerUrl,:largeBannerUrl&sort=order'
  20. BASE = 'http://www.syfy.com'
  21.  
  22. def masterlist():
  23.     return rootlist(db=True)
  24.  
  25. def rootlist(db=False):
  26.     xbmcplugin.setContent(int(sys.argv[1]), 'tvshows')
  27.     xbmcplugin.addSortMethod(pluginhandle, xbmcplugin.SORT_METHOD_LABEL)
  28.     data = common.getURL(BASE_URL)
  29.     shows = demjson.decode(data)['entries']
  30.     db_shows = []
  31.     for item in shows:
  32.         url = item['plcategory$fullTitle']
  33.         name = item['title']
  34.         if db==True:
  35.             db_shows.append((name,'syfy','showroot',url))
  36.         else:
  37.             common.addShow(name, 'syfy', 'showroot', url)
  38.     if db==True:
  39.         return db_shows
  40.     else:
  41.         common.setView('tvshows')
  42.        
  43. def showroot():
  44.     common.addDirectory('Full Episodes', 'syfy', 'episodes', common.args.url)
  45.     common.addDirectory('All Videos', 'syfy', 'allvideos', common.args.url)
  46.     common.setView('seasons')
  47.  
  48. def allvideos():
  49.     process('http://feed.theplatform.com/f/hQNl-B/2g1gkJT0urp6?')
  50.     common.setView('episodes')
  51.    
  52. def episodes():
  53.     process('http://feed.theplatform.com/f/hQNl-B/2g1gkJT0urp6?&byCustomValue={fullEpisode}{true}')
  54.     common.setView('episodes')
  55.    
  56. def process(urlBase, fullname = common.args.url):
  57.     #url = 'http://feed.theplatform.com/f/hQNl-B/2g1gkJT0urp6/'
  58.     url = urlBase
  59.     url += '&form=json'
  60.     #url += '&fields=guid,title,description,categories,content,defaultThumbnailUrl'
  61.     url += '&fileFields=duration,url,width,height'
  62.     url += '&count=true'
  63.     url += '&byCategories='+urllib.quote_plus(fullname)
  64.     #url += '&byCustomValue={fullEpisode}{true}'
  65.     data = common.getURL(url)
  66.     episodes = demjson.decode(data)['entries']
  67.     for episode in episodes:
  68.         name = episode['title']
  69.         showname= episode['media$categories'][1]['media$name'].split('/')[1]
  70.         try:
  71.             seasonEpisode = episode['pl1$subtitle'].replace('Episode','').strip()
  72.             season = int(seasonEpisode[:1])
  73.             episodeNum = int(seasonEpisode[1:])
  74.         except:
  75.             season = 0
  76.             episodeNum = 0
  77.         description = episode['description']
  78.         thumb= episode['plmedia$defaultThumbnailUrl']
  79.         duration=str(int(episode['media$content'][0]['plfile$duration']))
  80.         airDate = common.formatDate(epoch=episode['pubDate']/1000)
  81.         if season <> 0 and episodeNum <> 0:
  82.             displayname = '%sx%s - %s' % (str(season),str(episodeNum),name)
  83.         else:
  84.             displayname = name
  85.         url=episode['media$content'][0]['plfile$url']
  86.         u = sys.argv[0]
  87.         u += '?url="'+urllib.quote_plus(url)+'"'
  88.         u += '&mode="syfy"'
  89.         u += '&sitemode="play"'
  90.         infoLabels={ "Title":name,
  91.                      "Season":season,
  92.                      "Episode":episodeNum,
  93.                      "Plot":description,
  94.                      "premiered":airDate,
  95.                      "Duration":duration,
  96.                      "TVShowTitle":showname
  97.                      }
  98.         common.addVideo(u,displayname,thumb,infoLabels=infoLabels)
  99.  
  100. #Get SMIL url and play video
  101. def play():
  102.     smilurl=common.args.url
  103.     #+'&manifest=m3u'
  104.     swfUrl = 'http://www.syfy.com/_utils/video/codebase/pdk/swf/flvPlayer.swf'
  105.     if (common.settings['enableproxy'] == 'true'):proxy = True
  106.     else:proxy = False
  107.     data = common.getURL(smilurl,proxy=proxy)
  108.     tree=BeautifulSoup(data, convertEntities=BeautifulSoup.HTML_ENTITIES)
  109.     print tree.prettify()
  110.     rtmpbase = tree.find('meta')
  111.     if rtmpbase:
  112.         rtmpbase = rtmpbase['base']
  113.         items=tree.find('switch').findAll('video')
  114.         hbitrate = -1
  115.         sbitrate = int(common.settings['quality']) * 1024
  116.         for item in items:
  117.             bitrate = int(item['system-bitrate'])
  118.             if bitrate > hbitrate and bitrate <= sbitrate:
  119.                 hbitrate = bitrate
  120.                 playpath = item['src']
  121.                 if '.mp4' in playpath:
  122.                     playpath = 'mp4:'+playpath
  123.                 else:
  124.                     playpath = playpath.replace('.flv','')
  125.                 finalurl = rtmpbase+' playpath='+playpath + " swfurl=" + swfUrl + " swfvfy=true"
  126.     else:
  127.         #open m3u
  128.         data = common.getURL(smilurl+'&manifest=m3u',proxy=proxy)
  129.         tree=BeautifulSoup(data, convertEntities=BeautifulSoup.HTML_ENTITIES)
  130.         print tree.prettify()
  131.         items=tree.find('seq').findAll('video')
  132.         item=items[0]
  133.         hbitrate = -1
  134.         sbitrate = int(common.settings['quality']) * 1024
  135.         #for item in items:
  136.         #    bitrate = int(item['system-bitrate'])
  137.         #    if bitrate > hbitrate and bitrate <= sbitrate:
  138.         #        hbitrate = bitrate
  139.         m3u8url = item['src']
  140.         origfilename=m3u8url.split('/')[-1]
  141.         data = common.getURL(m3u8url,proxy=proxy)
  142.        # lines=data.splitlines()
  143.         #print "D",data
  144.         #bitrate on url isn't used
  145.         #.split('b__=')[0]+'b__='+common.settings['quality']
  146.         #print data
  147.         items=re.compile('BANDWIDTH=(\d*).*\n(.*)(\n)').findall(data)
  148.         #print "%^&^",items
  149.         for item in items:
  150.             #print line
  151.  
  152.             bitrate = int(item[0])
  153.             if bitrate > hbitrate and bitrate <= sbitrate:
  154.                 hbitrate = bitrate
  155.                # print "BR",bitrate
  156.                 filename = item[1]
  157.         finalurl=m3u8url.replace(origfilename,filename)
  158.     item = xbmcgui.ListItem(path=finalurl)
  159.     xbmcplugin.setResolvedUrl(pluginhandle, True, item)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement