Play advertisements

This commit is contained in:
mediaminister 2020-11-12 14:23:16 +01:00
parent 591cd91c09
commit 82e1dc28d4
3 changed files with 123 additions and 32 deletions

View File

@ -193,11 +193,21 @@ def show_listing(title_items, category=None, sort=None, content=None, cache=True
xbmcplugin.endOfDirectory(routing.handle, succeeded, cacheToDisc=cache) xbmcplugin.endOfDirectory(routing.handle, succeeded, cacheToDisc=cache)
def play(stream, stream_type=STREAM_HLS, license_key=None, title=None, art_dict=None, info_dict=None, prop_dict=None, stream_dict=None): def play(stream, stream_type=STREAM_HLS, license_key=None, title=None, art_dict=None, info_dict=None, prop_dict=None, stream_dict=None, ads_list=None):
"""Play the given stream""" """Play the given stream"""
from resources.lib.addon import routing from resources.lib.addon import routing
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
playlist.clear()
play_item = xbmcgui.ListItem(label=title, path=stream) if ads_list:
ads_list.append(stream)
stream = ads_list
if not isinstance(stream, list):
stream = [stream]
for i, url in enumerate(stream):
play_item = xbmcgui.ListItem(label=title, path=url)
if art_dict: if art_dict:
play_item.setArt(art_dict) play_item.setArt(art_dict)
if info_dict: if info_dict:
@ -207,6 +217,7 @@ def play(stream, stream_type=STREAM_HLS, license_key=None, title=None, art_dict=
if stream_dict: if stream_dict:
play_item.addStreamInfo('video', stream_dict) play_item.addStreamInfo('video', stream_dict)
if not url.endswith('.mp4'):
# Setup Inputstream Adaptive # Setup Inputstream Adaptive
if kodi_version_major() >= 19: if kodi_version_major() >= 19:
play_item.setProperty('inputstream', 'inputstream.adaptive') play_item.setProperty('inputstream', 'inputstream.adaptive')
@ -229,7 +240,12 @@ def play(stream, stream_type=STREAM_HLS, license_key=None, title=None, art_dict=
play_item.setContentLookup(False) play_item.setContentLookup(False)
xbmcplugin.setResolvedUrl(routing.handle, True, listitem=play_item) if i == 0:
first_item = play_item
playlist.add(url=url, listitem=play_item, index=i)
xbmcplugin.setResolvedUrl(routing.handle, True, listitem=first_item)
def get_search_string(heading='', message=''): def get_search_string(heading='', message=''):

View File

@ -48,6 +48,10 @@ class Player:
kodiutils.ok_dialog(message=kodiutils.localize(30712)) kodiutils.ok_dialog(message=kodiutils.localize(30712))
return return
# Get advertisements
ad_streams = self._api.get_ad_streams(episode.channel, episode.program_title, path, episode.uuid, episode.video_type, kodiutils.get_setting('username'))
_LOGGER.info('Advertisements: %s', ad_streams)
if episode.stream: if episode.stream:
# We already have a resolved stream. Nice! # We already have a resolved stream. Nice!
# We don't need credentials for these streams. # We don't need credentials for these streams.
@ -78,7 +82,8 @@ class Player:
license_key, license_key,
info_dict=titleitem.info_dict, info_dict=titleitem.info_dict,
art_dict=titleitem.art_dict, art_dict=titleitem.art_dict,
prop_dict=titleitem.prop_dict) prop_dict=titleitem.prop_dict,
ads_list=ad_streams)
def play(self, uuid): def play(self, uuid):
""" Play the requested item. """ Play the requested item.

View File

@ -98,10 +98,11 @@ class Season:
class Episode: class Episode:
""" Defines an Episode. """ """ Defines an Episode. """
def __init__(self, uuid=None, nodeid=None, path=None, channel=None, program_title=None, title=None, description=None, cover=None, background=None, def __init__(self, uuid=None, video_type=None, nodeid=None, path=None, channel=None, program_title=None, title=None, description=None, cover=None,
duration=None, season=None, season_uuid=None, number=None, rating=None, aired=None, expiry=None, stream=None): background=None, duration=None, season=None, season_uuid=None, number=None, rating=None, aired=None, expiry=None, stream=None):
""" """
:type uuid: str :type uuid: str
:type video_type: str
:type nodeid: str :type nodeid: str
:type path: str :type path: str
:type channel: str :type channel: str
@ -120,6 +121,7 @@ class Episode:
:type stream: string :type stream: string
""" """
self.uuid = uuid self.uuid = uuid
self.video_type = video_type
self.nodeid = nodeid self.nodeid = nodeid
self.path = path self.path = path
self.channel = channel self.channel = channel
@ -401,6 +403,73 @@ class ContentApi:
return categories return categories
def get_weather(self, channel):
""" Get a weather dictionary.
:type channel: str
:rtype dict
"""
response = self._get_url(self.SITE_APIS[channel] + '/weather', authentication=True)
weather = json.loads(response)
return weather
def get_ad_streams(self, channel, program, path, uuid, video_type, username):
""" Get a list of advertisement stream URLs to use for this video.
:type channel: str
:type path: str
:rtype list
"""
ad_streams = []
ad_url = 'https://pubads.g.doubleclick.net/gampad/ads'
weather = self.get_weather(channel)
channel_info = dict(
vier=dict(cmsid='2493239', network_id='21797861328'),
vijf=dict(cmsid='2493512', network_id='21797861328'),
zes=dict(cmsid='2496240', network_id='21797861328')
)
network_id = channel_info.get(channel).get('network_id')
from unicodedata import normalize
program = normalize('NFD', program).replace(' ', '-')
program = re.sub(r'[^A-Za-z0-9-]+', '', program).lower()
from hashlib import sha1
ppid = sha1(username.encode('utf-8')).hexdigest()
if program:
iu_id = '/{}/{}/{}/{}'.format(network_id, channel, 'programmas', program)
else:
iu_id = '/{}/{}/'.format(network_id, channel)
params = dict(ad_rule=1,
cmsid=channel_info.get(channel).get('cmsid'),
correlator=int(round(time.time() * 1000)),
sbs_weather_cond=weather.get('summary'),
sbs_weather_temp=weather.get('temperature'),
description_url=path,
env='vp',
gdfp_req=1,
impl='s',
iu=iu_id,
output='vast',
pp='SBSNoDash',
ppid=ppid,
sz='640x360',
unviewed_position_start=1,
url=path,
vid=uuid,
video_type=video_type)
xml = self._get_url(ad_url, params)
import xml.etree.ElementTree as ET
tree = ET.fromstring(xml)
for item in tree:
if item.tag == 'Preroll':
url = item.find('Ad').text
xml = self._get_url(url)
tree = ET.fromstring(xml)
for adv in tree.findall('.//Ad'):
for stream in adv.findall('.//MediaFile'):
if stream.get('type') == 'video/mp4' and stream.get('width') == '1920':
ad_streams.append(stream.text)
break
return ad_streams
@staticmethod @staticmethod
def _extract_programs(html, channel): def _extract_programs(html, channel):
""" Extract Programs from HTML code """ """ Extract Programs from HTML code """
@ -561,6 +630,7 @@ class ContentApi:
episode = Episode( episode = Episode(
uuid=data.get('videoUuid'), uuid=data.get('videoUuid'),
video_type=data.get('type', {}),
nodeid=data.get('pageInfo', {}).get('nodeId'), nodeid=data.get('pageInfo', {}).get('nodeId'),
path=data.get('link').lstrip('/'), path=data.get('link').lstrip('/'),
channel=data.get('pageInfo', {}).get('site'), channel=data.get('pageInfo', {}).get('site'),