@@ -97,10 +97,11 @@ def __repr__(self):
9797class Episode :
9898 """ Defines an Episode. """
9999
100- def __init__ (self , uuid = None , nodeid = None , path = None , channel = None , program_title = None , title = None , description = None , cover = None , background = None ,
101- duration = None , season = None , season_uuid = None , number = None , rating = None , aired = None , expiry = None , stream = None ):
100+ def __init__ (self , uuid = None , video_type = None , nodeid = None , path = None , channel = None , program_title = None , title = None , description = None , cover = None ,
101+ background = None , duration = None , season = None , season_uuid = None , number = None , rating = None , aired = None , expiry = None , stream = None ):
102102 """
103103 :type uuid: str
104+ :type video_type: str
104105 :type nodeid: str
105106 :type path: str
106107 :type channel: str
@@ -119,6 +120,7 @@ def __init__(self, uuid=None, nodeid=None, path=None, channel=None, program_titl
119120 :type stream: string
120121 """
121122 self .uuid = uuid
123+ self .video_type = video_type
122124 self .nodeid = nodeid
123125 self .path = path
124126 self .channel = channel
@@ -375,6 +377,69 @@ def get_categories(self, channel):
375377
376378 return categories
377379
380+ def get_weather (self , channel ):
381+ """ Get a weather dictionary.
382+ :type channel: str
383+ :rtype dict
384+ """
385+ response = self ._get_url (self .SITE_APIS [channel ] + '/weather' , authentication = True )
386+ weather = json .loads (response )
387+ return weather
388+
389+ def get_ad_streams (self , channel , program , path , uuid , video_type , username ):
390+ """ Get a list of advertisement stream URLs to use for this video.
391+ :type channel: str
392+ :type path: str
393+ :rtype list
394+ """
395+ ad_streams = []
396+ ad_url = 'https://pubads.g.doubleclick.net/gampad/ads'
397+ weather = self .get_weather (channel )
398+ channel_info = dict (
399+ vier = dict (cmsid = '2493239' , network_id = '21797861328' ),
400+ vijf = dict (cmsid = '2493512' , network_id = '21797861328' ),
401+ zes = dict (cmsid = '2496240' , network_id = '21797861328' )
402+ )
403+ network_id = channel_info .get (channel ).get ('network_id' )
404+ from unicodedata import normalize
405+ program = normalize ('NFD' , program ).replace (' ' , '-' )
406+ program = re .sub (r'[^A-Za-z0-9-]+' , '' , program ).lower ()
407+ from hashlib import sha1
408+ ppid = sha1 (username ).hexdigest ()
409+ if program :
410+ iu_id = '/{}/{}/{}/{}' .format (network_id , channel , 'programmas' , program )
411+ else :
412+ iu_id = '/{}/{}/' .format (network_id , channel )
413+ params = dict (ad_rule = 1 ,
414+ cmsid = channel_info .get (channel ).get ('cmsid' ),
415+ correlator = int (round (time .time () * 1000 )),
416+ sbs_weather_cond = weather .get ('summary' ),
417+ sbs_weather_temp = weather .get ('temperature' ),
418+ description_url = path ,
419+ env = 'vp' ,
420+ gdfp_req = 1 ,
421+ impl = 's' ,
422+ iu = iu_id ,
423+ output = 'vast' ,
424+ pp = 'SBSNoDash' ,
425+ ppid = ppid ,
426+ sz = '640x360' ,
427+ unviewed_position_start = 1 ,
428+ url = path ,
429+ vid = uuid ,
430+ video_type = video_type )
431+
432+ xml = self ._get_url (ad_url , params )
433+ import xml .etree .ElementTree as ET
434+ tree = ET .fromstring (xml )
435+ for item in tree :
436+ if item .tag == 'Preroll' :
437+ url = item .find ('Ad' ).text
438+ xml = self ._get_url (url )
439+ tree = ET .fromstring (xml )
440+ ad_streams = [item .text for item in tree .findall ('.//MediaFile[@delivery="streaming"]' ) if item .text .endswith ('.m3u8' )]
441+ return ad_streams
442+
378443 @staticmethod
379444 def _extract_programs (html , channel ):
380445 """ Extract Programs from HTML code """
@@ -535,6 +600,7 @@ def _parse_episode_data(data, season_uuid=None):
535600
536601 episode = Episode (
537602 uuid = data .get ('videoUuid' ),
603+ video_type = data .get ('type' , {}),
538604 nodeid = data .get ('pageInfo' , {}).get ('nodeId' ),
539605 path = data .get ('link' ).lstrip ('/' ),
540606 channel = data .get ('pageInfo' , {}).get ('site' ),
0 commit comments