Advertisement
alexdunlop81

lifetime.py

Apr 24th, 2013
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.64 KB | None | 0 0
  1. import xbmc
  2. import xbmcgui
  3. import xbmcplugin
  4. import urllib
  5. import urllib2
  6. import httplib
  7. import sys
  8. import os
  9. import re
  10.  
  11. from BeautifulSoup import BeautifulStoneSoup
  12. from BeautifulSoup import BeautifulSoup
  13. import demjson
  14. import pyamf
  15. from pyamf import remoting, amf3, util
  16.  
  17. import resources.lib._common as common
  18.  
  19. import binascii
  20. import time
  21. import hmac
  22. try:
  23.     import hashlib.sha1 as sha1
  24. except:
  25.     import sha as sha1
  26.  
  27. pluginhandle = int(sys.argv[1])
  28. showlist= 'http://www.mylifetime.com/video'
  29.  
  30. def masterlist():
  31.     return shows(db=True)
  32.  
  33. def rootlist():
  34.     data = common.getURL(showlist)
  35.     tree=BeautifulSoup(data, convertEntities=BeautifulSoup.HTML_ENTITIES)
  36.     menu = tree.find('div',attrs={'id':'accordion','class':'view-content'}).findAll('h3')
  37.     for item in menu:
  38.         name = item.find('a',attrs={'href':'#'}).string
  39.         common.addDirectory(name, 'lifetime', 'shows', name )
  40.     common.addDirectory('Full Movies', 'lifetime', 'movies', '')
  41.     common.setView('seasons')
  42.  
  43. def shows(url=common.args.url,db=False):
  44.     data = common.getURL(showlist)
  45.     tree=BeautifulSoup(data, convertEntities=BeautifulSoup.HTML_ENTITIES)
  46.     menu = tree.find('div',attrs={'id':'accordion','class':'view-content'}).findAll('h3')
  47.     names = []
  48.     for item in menu:
  49.         name = item.find('a',attrs={'href':'#'}).string
  50.         names.append(name)
  51.     if db == False:
  52.         marker = names.index(url)
  53.     elif db == True:
  54.         marker = names.index('Current Shows')
  55.     menu = tree.find('div',attrs={'id':'accordion','class':'view-content'})
  56.     shows = menu.findAll('div',recursive=False)[marker].findAll('li')
  57.     dbshows = []
  58.     for show in shows:
  59.         showdata = show.findAll(attrs={'class':'field-content'})
  60.         name = showdata[0].a.string
  61.         showid = showdata[1].string
  62.         if db == False:
  63.             if 'Movies' in common.args.name:
  64.                 common.addDirectory(name, 'lifetime', 'showroot', showid)
  65.             else:
  66.                 common.addShow(name, 'lifetime', 'showroot', showid)
  67.         elif db == True:
  68.             dbshows.append((name, 'lifetime', 'showroot', showid))
  69.     if db == True:
  70.         return dbshows
  71.     else:
  72.         common.setView('tvshows')
  73.        
  74. def showroot(showid=common.args.url):
  75.     common.addDirectory('Episodes', 'lifetime', 'episodes', showid )
  76.     common.addDirectory('Clips', 'lifetime', 'clips', showid )
  77.     common.setView('seasons')
  78.  
  79. def episodes(showid=common.args.url):
  80.     url = 'http://www.mylifetime.com/d6/views/ajax'
  81.     url += '?show_code='+urllib.quote_plus(showid)
  82.     url += '&premium%5B%5D=0'
  83.     url += '&ajax_pager_field=0'
  84.     url += '&most_filter=mr'
  85.     url += '&view_name=video_watch_full_episodes_assoc'
  86.     url += '&view_display_id=block_1'
  87.     url += '&view_args='
  88.     url += '&view_path=watch-full-episodes-online'
  89.     url += '&view_base_path=null'
  90.     url += '&view_dom_id=video-watch-full-episodes-assoc-block-1'
  91.     url += '&pager_element=0'
  92.     processEpisodes(url)
  93.     #processEpisodes(url.replace('&premium%5B%5D=0','&premium%5B%5D=1'))
  94.     common.setView('episodes')
  95.        
  96. def movies(showid=common.args.url):
  97.     url = 'http://www.mylifetime.com/d6/views/ajax'
  98.     #url += '?js=1&page=1'
  99.     url += '?view_name=video_watch_full_movies_assoc'
  100.     url += '&view_display_id=block_1'
  101.     url += '&view_args='
  102.     url += '&view_path=watch-full-movies-online'
  103.     url += '&view_base_path=null'
  104.     url += '&view_dom_id=video-watch-full-movies-assoc-block-1'
  105.     url += '&pager_element=1'
  106.     processMovies(url)
  107.     common.setView('episodes')
  108.  
  109. def clips(showid=common.args.url):
  110.     url = 'http://www.mylifetime.com/d6/views/ajax'
  111.     url += '?js=1&page=1'
  112.     url += '&field_length_value_many_to_one%5B%5D=Clip'
  113.     url += '&show_code='+urllib.quote_plus(showid)
  114.     url += '&ajax_pager_field=0'
  115.     url += '&most_filter=mr'
  116.     url += '&view_name=video_homepage_video_list'
  117.     url += '&view_display_id=block_1'
  118.     url += '&view_args='
  119.     url += '&view_path=video'
  120.     url += '&view_base_path=null'
  121.     url += '&view_dom_id=video-homepage-video-list-block-1'
  122.     url += '&pager_element=0'
  123.     processEpisodes(url)
  124.     common.setView('episodes')
  125.  
  126. def processEpisodes(url):
  127.     data = common.getURL(url)
  128.     remove = re.compile('<script.*?script>', re.DOTALL)
  129.     data = re.sub(remove, '', data)
  130.     remove = re.compile('<\\!--.*?-->', re.DOTALL)
  131.     data = re.sub(remove, '', data)
  132.     htmldata = demjson.decode(data)['display']
  133.     remove = re.compile('"<div.*?div>"')
  134.     htmldata = re.sub(remove, '""', htmldata)
  135.     tree=BeautifulSoup(htmldata, convertEntities=BeautifulSoup.HTML_ENTITIES)
  136.     print tree.prettify()
  137.     episodes = tree.findAll('div',attrs={'class':re.compile('video-image-wrapper video')})
  138.     if len(episodes) == 0:
  139.         return False
  140.     for episode in episodes:
  141.         print episode.prettify()
  142.         url = episode.find('a')['href']
  143.         name = episode.find('img')['title']
  144.         thumb = episode.find('img')['src']
  145.         u = sys.argv[0]
  146.         u += '?url="'+urllib.quote_plus(url)+'"'
  147.         u += '&mode="lifetime"'
  148.         u += '&sitemode="playepisode"'
  149.         infoLabels={ "Title":name,
  150.                     "TVShowTitle":common.args.name}
  151.         common.addVideo(u,name,thumb,infoLabels=infoLabels)
  152.     return True
  153.  
  154. def processMovies(url):
  155.     data = common.getURL(url)
  156.     remove = re.compile('<script.*?script>', re.DOTALL)
  157.     data = re.sub(remove, '', data)
  158.     remove = re.compile('<\\!--.*?-->', re.DOTALL)
  159.     data = re.sub(remove, '', data)
  160.     htmldata = demjson.decode(data)['display']
  161.     remove = re.compile('"<div.*?div>"')
  162.     htmldata = re.sub(remove, '""', htmldata)
  163.     tree=BeautifulSoup(htmldata, convertEntities=BeautifulSoup.HTML_ENTITIES)
  164.     #print tree.prettify()
  165.     episodes = tree.findAll('div',attrs={'class':re.compile('video-image-wrapper video')})
  166.     if len(episodes) == 0:
  167.         return False
  168.     for episode in episodes:
  169.         print episode.prettify()
  170.         url = episode.find('a')['href']
  171.         try:    name = episode.find('b').string
  172.         except: name = episode.find('img')['title']
  173.         thumb = episode.find('img')['src']
  174.         u = sys.argv[0]
  175.         u += '?url="'+urllib.quote_plus(url)+'"'
  176.         u += '&mode="lifetime"'
  177.         u += '&sitemode="playepisode"'
  178.         infoLabels={ "Title":name,
  179.                     "TVShowTitle":common.args.name}
  180.         common.addVideo(u,name,thumb,infoLabels=infoLabels)
  181.     return True
  182.    
  183. def playepisode(url=common.args.url):
  184.     data = common.getURL(url)
  185.     path = url.split('mylifetime.com/')[1].split('?')[0]
  186.     jsonData = re.compile('Drupal\.settings\.video, (.+)\);').findall(data)[0]
  187.     json = demjson.decode(jsonData)
  188.     for item in json['cached_videos']:
  189.         if item['path'] == path:
  190.             smil_url = item['pdk_flash_url'].split('?')[0]
  191.     smil_url = json['cached_videos'][0]['pdk_flash_url'].split('?')[0]
  192.     #signed_url = sign_url(smil_url)
  193.     #link = common.getURL(signed_url)
  194.     #tree=BeautifulStoneSoup(link, convertEntities=BeautifulStoneSoup.HTML_ENTITIES)
  195.     #base = tree.find('meta')['base']
  196.     #videos = tree.findAll('video')
  197.     #        filename = video['src'].replace('.mp4','').replace('.flv','')
  198.     #swfUrl = 'http://www.mylifetime.com/shows/sites/all/libraries/pdk_player/pdk/swf/flvPlayer.swf'
  199.     #auth = filename.split('?')[1]
  200.     #filename = filename.split('?')[0]
  201.     #finalurl = 'base'+'?'+auth+' swfurl='+swfUrl+' swfvfy=true playpath='+filename
  202.     smil_url += '?manifest=m3u'
  203.     if (common.settings['enableproxy'] == 'true'):proxy = True
  204.     else:proxy = False
  205.     #url += '&manifest=m3u'
  206.     data = common.getURL(smil_url,proxy=proxy)
  207.     tree=BeautifulSoup(data, convertEntities=BeautifulSoup.HTML_ENTITIES)
  208.     hbitrate = -1
  209.     sbitrate = int(common.settings['quality']) * 1000
  210.     videos = tree.findAll('video')
  211.     for video in videos:
  212.         try:bitrate = int(video['system-bitrate'])
  213.         except:
  214.             try:bitrate = int(video['systembitrate'])
  215.             except: bitrate = 1
  216.         if bitrate > hbitrate and bitrate <= sbitrate:
  217.             hbitrate = bitrate
  218.             finalurl = video['src']
  219.  
  220.     #print tree.find('video')
  221.     #finalurl = tree.find('video')['src']
  222.     item = xbmcgui.ListItem(path=finalurl)
  223.     return xbmcplugin.setResolvedUrl(pluginhandle, True, item)
  224.  
  225. def sign_url(url):
  226.     hmac_key = 'crazyjava'
  227.     SEC_HEX = '733363723374' #'s3cr3t'
  228.     expiration = get_expiration()
  229.     path = url.split('http://link.theplatform.com/s/')[1]
  230.     sign_data = binascii.unhexlify('00'+expiration+binascii.hexlify(path).lower())
  231.     sig = hmac.new(hmac_key, sign_data, sha1)
  232.     sigHEX = sig.hexdigest()
  233.     signature = '00' + expiration + sigHEX + SEC_HEX
  234.     finalUrl = url+'?sig='+signature+'&format=SMIL&Tracking=true&Embedded=true&mbr=true'
  235.     return finalUrl
  236.  
  237. def get_expiration(auth_length = 600):
  238.     current_time = time.mktime(time.gmtime())+auth_length
  239.     expiration = ('%0.2X' % current_time).lower()
  240.     return expiration
  241.    
  242. ### OLD BRIGHTCOVE CODE
  243.    
  244. def playepisodeOLD(url=common.args.url):
  245.     swfUrl = 'http://admin.brightcove.com/viewer/us20110809.1526/federatedVideoUI/BrightcovePlayer.swf'
  246.    
  247.     data = common.getURL(url)
  248.     key = re.compile('"player_key":"(.+?)","').findall(data)[0]
  249.     try:content_id = re.compile('"defaultVideoID":"(.+?)","').findall(data)[0]
  250.     except:content_id = re.compile('"video_id":"(.+?)","').findall(data)[0]
  251.     exp_id = re.compile('"player_id":"(.+?)","').findall(data)[0]
  252.    
  253.     renditions = get_episode_info(key, content_id, url, exp_id)['programmedContent']['videoPlayer']['mediaDTO']['renditions']
  254.     rtmp = ''
  255.     hi_res = 0
  256.     selected_video = None
  257.     for video in renditions:
  258.         print video
  259.         if(int(video['size'])>hi_res):
  260.             selected_video = video
  261.             hi_res = int(video['size'])
  262.    
  263.     rtmp = selected_video['defaultURL']
  264.     refactor = re.compile('^(.+?ondemand)/&(.+?)(\?.+)$').findall(rtmp)[0]
  265.     mpvideo = re.compile('_(\d+)_').findall(refactor[1])[0]
  266.     publishId = re.compile('mp4:(\d+?)/').findall(refactor[1])
  267.     if(len(publishId)==0):
  268.         publishId = re.compile('^(\d+?)').findall(refactor[1])[0]
  269.     else:
  270.         publishId = publishId[0]
  271.    
  272.     rtmp = refactor[0]+refactor[2]+' playpath='+refactor[1]+refactor[2]+'&videoId='+mpvideo+'&lineUpId=&pubId='+publishId+'&playerId='+exp_id+'&affiliatedId='
  273.     rtmp += ' pageUrl='+url
  274.    
  275.     item = xbmcgui.ListItem(path=rtmp)
  276.     return xbmcplugin.setResolvedUrl(pluginhandle, True, item)
  277.  
  278. def get_episode_info(key, content_id, url, exp_id):
  279.     conn = httplib.HTTPConnection("c.brightcove.com")
  280.     envelope = build_amf_request(key, content_id, url, exp_id)
  281.     conn.request("POST", "/services/messagebroker/amf?playerKey="+key, str(remoting.encode(envelope).read()),{'content-type': 'application/x-amf'})
  282.     response = conn.getresponse().read()
  283.     response = remoting.decode(response).bodies[0][1].body
  284.     print response
  285.     return response
  286.  
  287. class ViewerExperienceRequest(object):
  288.     def __init__(self, URL, contentOverrides, experienceId, playerKey, TTLToken=''):
  289.         self.TTLToken = TTLToken
  290.         self.URL = URL
  291.         self.deliveryType = float(0)
  292.         self.contentOverrides = contentOverrides
  293.         self.experienceId = experienceId
  294.         self.playerKey = playerKey
  295.  
  296. class ContentOverride(object):
  297.     def __init__(self, contentId, contentType=0, target='videoPlayer'):
  298.         self.contentType = contentType
  299.         self.contentId = contentId
  300.         self.target = target
  301.         self.contentIds = None
  302.         self.contentRefId = None
  303.         self.contentRefIds = None
  304.         self.contentType = 0
  305.         self.featureId = float(0)
  306.         self.featuredRefId = None
  307.  
  308. def build_amf_request(key, content_id, url, exp_id):
  309.     print 'ContentId:'+content_id
  310.     print 'ExperienceId:'+exp_id
  311.     print 'URL:'+url
  312.     const = '686a10e2a34ec3ea6af8f2f1c41788804e0480cb'
  313.     pyamf.register_class(ViewerExperienceRequest, 'com.brightcove.experience.ViewerExperienceRequest')
  314.     pyamf.register_class(ContentOverride, 'com.brightcove.experience.ContentOverride')
  315.     content_override = ContentOverride(int(content_id))
  316.     viewer_exp_req = ViewerExperienceRequest(url, [content_override], int(exp_id), key)
  317.    
  318.     env = remoting.Envelope(amfVersion=3)
  319.     env.bodies.append(
  320.             (
  321.                 "/1",
  322.                 remoting.Request(
  323.                     target="com.brightcove.experience.ExperienceRuntimeFacade.getDataForExperience",
  324.                     body=[const, viewer_exp_req],
  325.                     envelope=env
  326.             )
  327.         )
  328.     )
  329.     return env
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement