@@ -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,65 @@ 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 ):
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+ if program :
408+ iu_id = '/{}/{}/{}/{}' .format (network_id , channel , 'programmas' , program )
409+ else :
410+ iu_id = '/{}/{}/' .format (network_id , channel )
411+ params = dict (ad_rule = 1 ,
412+ cmsid = channel_info .get (channel ).get ('cmsid' ),
413+ correlator = int (round (time .time () * 1000 )),
414+ sbs_weather_cond = weather .get ('summary' ),
415+ sbs_weather_temp = weather .get ('temperature' ),
416+ description_url = path ,
417+ env = 'vp' ,
418+ gdfp_req = 1 ,
419+ impl = 's' ,
420+ iu = iu_id ,
421+ output = 'vast' ,
422+ sz = '640x360' ,
423+ unviewed_position_start = 1 ,
424+ url = path ,
425+ vid = uuid ,
426+ video_type = video_type )
427+
428+ xml = self ._get_url (ad_url , params )
429+ import xml .etree .ElementTree as ET
430+ tree = ET .fromstring (xml )
431+ for item in tree :
432+ if item .tag == 'Preroll' :
433+ url = item .find ('Ad' ).text
434+ xml = self ._get_url (url )
435+ tree = ET .fromstring (xml )
436+ ad_streams = [item .text for item in tree .findall ('.//MediaFile[@delivery="streaming"]' ) if item .text .endswith ('.m3u8' )]
437+ return ad_streams
438+
378439 @staticmethod
379440 def _extract_programs (html , channel ):
380441 """ Extract Programs from HTML code """
@@ -535,6 +596,7 @@ def _parse_episode_data(data, season_uuid=None):
535596
536597 episode = Episode (
537598 uuid = data .get ('videoUuid' ),
599+ video_type = data .get ('type' , {}),
538600 nodeid = data .get ('pageInfo' , {}).get ('nodeId' ),
539601 path = data .get ('link' ).lstrip ('/' ),
540602 channel = data .get ('pageInfo' , {}).get ('site' ),
0 commit comments