@@ -98,10 +98,11 @@ def __repr__(self):
9898class Episode :
9999 """ Defines an Episode. """
100100
101- def __init__ (self , uuid = None , nodeid = None , path = None , channel = None , program_title = None , title = None , description = None , cover = None , background = None ,
102- duration = None , season = None , season_uuid = None , number = None , rating = None , aired = None , expiry = None , stream = None ):
101+ def __init__ (self , uuid = None , video_type = None , nodeid = None , path = None , channel = None , program_title = None , title = None , description = None , cover = None ,
102+ background = None , duration = None , season = None , season_uuid = None , number = None , rating = None , aired = None , expiry = None , stream = None ):
103103 """
104104 :type uuid: str
105+ :type video_type: str
105106 :type nodeid: str
106107 :type path: str
107108 :type channel: str
@@ -120,6 +121,7 @@ def __init__(self, uuid=None, nodeid=None, path=None, channel=None, program_titl
120121 :type stream: string
121122 """
122123 self .uuid = uuid
124+ self .video_type = video_type
123125 self .nodeid = nodeid
124126 self .path = path
125127 self .channel = channel
@@ -401,6 +403,73 @@ def get_categories(self, channel):
401403
402404 return categories
403405
406+ def get_weather (self , channel ):
407+ """ Get a weather dictionary.
408+ :type channel: str
409+ :rtype dict
410+ """
411+ response = self ._get_url (self .SITE_APIS [channel ] + '/weather' , authentication = True )
412+ weather = json .loads (response )
413+ return weather
414+
415+ def get_ad_streams (self , channel , program , path , uuid , video_type , username ):
416+ """ Get a list of advertisement stream URLs to use for this video.
417+ :type channel: str
418+ :type path: str
419+ :rtype list
420+ """
421+ ad_streams = []
422+ ad_url = 'https://pubads.g.doubleclick.net/gampad/ads'
423+ weather = self .get_weather (channel )
424+ channel_info = dict (
425+ vier = dict (cmsid = '2493239' , network_id = '21797861328' ),
426+ vijf = dict (cmsid = '2493512' , network_id = '21797861328' ),
427+ zes = dict (cmsid = '2496240' , network_id = '21797861328' )
428+ )
429+ network_id = channel_info .get (channel ).get ('network_id' )
430+ from unicodedata import normalize
431+ program = normalize ('NFD' , program ).replace (' ' , '-' )
432+ program = re .sub (r'[^A-Za-z0-9-]+' , '' , program ).lower ()
433+ from hashlib import sha1
434+ ppid = sha1 (username .encode ('utf-8' )).hexdigest ()
435+ if program :
436+ iu_id = '/{}/{}/{}/{}' .format (network_id , channel , 'programmas' , program )
437+ else :
438+ iu_id = '/{}/{}/' .format (network_id , channel )
439+ params = dict (ad_rule = 1 ,
440+ cmsid = channel_info .get (channel ).get ('cmsid' ),
441+ correlator = int (round (time .time () * 1000 )),
442+ sbs_weather_cond = weather .get ('summary' ),
443+ sbs_weather_temp = weather .get ('temperature' ),
444+ description_url = path ,
445+ env = 'vp' ,
446+ gdfp_req = 1 ,
447+ impl = 's' ,
448+ iu = iu_id ,
449+ output = 'vast' ,
450+ pp = 'SBSNoDash' ,
451+ ppid = ppid ,
452+ sz = '640x360' ,
453+ unviewed_position_start = 1 ,
454+ url = path ,
455+ vid = uuid ,
456+ video_type = video_type )
457+
458+ xml = self ._get_url (ad_url , params )
459+ import xml .etree .ElementTree as ET
460+ tree = ET .fromstring (xml )
461+ for item in tree :
462+ if item .tag == 'Preroll' :
463+ url = item .find ('Ad' ).text
464+ xml = self ._get_url (url )
465+ tree = ET .fromstring (xml )
466+ for adv in tree .findall ('.//Ad' ):
467+ for stream in adv .findall ('.//MediaFile' ):
468+ if stream .get ('type' ) == 'video/mp4' and stream .get ('width' ) == '1920' :
469+ ad_streams .append (stream .text )
470+ break
471+ return ad_streams
472+
404473 @staticmethod
405474 def _extract_programs (html , channel ):
406475 """ Extract Programs from HTML code """
@@ -561,6 +630,7 @@ def _parse_episode_data(data, season_uuid=None):
561630
562631 episode = Episode (
563632 uuid = data .get ('videoUuid' ),
633+ video_type = data .get ('type' , {}),
564634 nodeid = data .get ('pageInfo' , {}).get ('nodeId' ),
565635 path = data .get ('link' ).lstrip ('/' ),
566636 channel = data .get ('pageInfo' , {}).get ('site' ),
0 commit comments