Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback for live streams when web scraping fails #168

Merged
merged 1 commit into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions resources/lib/vrtplayer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,23 @@
name='Eén',
studio='Een',
live_stream='https://www.vrt.be/vrtnu/kanalen/een/',

live_stream_id='vualto_een',
),
'canvas': dict(
id='1H',
type='tv',
name='Canvas',
studio='Canvas',
live_stream='https://www.vrt.be/vrtnu/kanalen/canvas/',
live_stream_id='vualto_canvas',
),
'ketnet': dict(
id='O9',
type='tv',
name='Ketnet',
studio='Ketnet',
live_stream='https://www.vrt.be/vrtnu/kanalen/ketnet/',
live_stream_id='vualto_ketnet',
),
'ketnet-jr': dict(
id='1H',
Expand All @@ -59,6 +61,7 @@
type='radio+tv',
name='Sporza',
studio='Sporza',
live_stream_id='vualto_sporza',
),
'vrtnxt': dict(
id='',
Expand Down Expand Up @@ -90,20 +93,22 @@
name='Studio Brussel',
studio='Studio Brussel',
# live_stream='https://stubru.be/live',
live_stream='https://live-radio-cf-vrt.akamaized.net/groupb/live/0f394a26-c87d-475e-8590-e9c6e79b28d9/live.isml/.mpd',
live_stream_id='vualto_stubru',
),
'mnm': dict(
id='55',
type='radio+tv',
name='MNM',
studio='MNM',
# live_stream='https://mnm.be/kijk/live',
live_stream='https://live-radio-cf-vrt.akamaized.net/groupa/live/bac277a1-306d-44a0-8e2e-e5b9c07fa270/live.isml/.mpd',
live_stream_id='vualto_mnm',
),
'vrtnws': dict(
id='13',
type='radio+tv',
name='VRT NWS',
studio='VRT NWS',
live_stream_id='vualto_nieuws',
# live_stream_id='vualto_journaal',
),
}
38 changes: 27 additions & 11 deletions resources/lib/vrtplayer/streamservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,38 @@ def _get_license_key(self, key_url, key_type='R', key_headers=None, key_value=No

return '%s|%s|%s|' % (key_url, header, key_value)

def _get_api_data(self, video_url):
def _get_api_data(self, video_url, video_id=None):
html_page = requests.get(video_url, proxies=self._proxies).text
strainer = SoupStrainer('div', {'class': 'cq-dd-vrtvideo'})
soup = BeautifulSoup(html_page, 'html.parser', parse_only=strainer)
video_data = soup.find(lambda tag: tag.name == 'div' and tag.get('class') == ['vrtvideo']).attrs
is_live_stream = False
xvrttoken = None
try:
video_data = soup.find(lambda tag: tag.name == 'div' and tag.get('class') == ['vrtvideo']).attrs
except Exception:
# Web scraping failed
video_data = None

# Web scraping failed, fall back to using video_id only
if not video_data and video_id:
return apidata.ApiData(self._CLIENT, self._VUALTO_API_URL, video_id, '', None, True)

# Store required data attributes
client = video_data.get('data-client')
media_api_url = video_data.get('data-mediaapiurl')

video_id = video_data.get('data-videoid')
if video_id is not None:
xvrttoken = self.token_resolver.get_xvrttoken()
else:
video_id = video_data.get('data-livestream')
# Is this a livestream instead ?
if video_id is None:
is_live_stream = True
xvrttoken = None
video_id = video_data.get('data-livestream')
else:
is_live_stream = False
xvrttoken = self.token_resolver.get_xvrttoken()

publication_id = video_data.get('data-publicationid', '')
if publication_id:
publication_id += requests.utils.quote('$')

return apidata.ApiData(client, media_api_url, video_id, publication_id, xvrttoken, is_live_stream)

def _get_video_json(self, api_data):
Expand Down Expand Up @@ -145,17 +157,21 @@ def _fix_virtualsubclip(stream_dict, duration):

def get_stream(self, video, retry=False, api_data=None):
video_url = video.get('video_url')
self._kodi_wrapper.log_notice('video_url ' + video_url)
video_id = video.get('video_id')
publication_id = video.get('publication_id')
# Prepare api_data for on demand streams by video_id and publication_id
if video_id and publication_id and not retry:
xvrttoken = self.token_resolver.get_xvrttoken()
api_data = apidata.ApiData(self._CLIENT, self._VUALTO_API_URL, video_id, publication_id + requests.utils.quote('$'), xvrttoken, False)
# Support .mpd streams directly
# Prepare api_data for livestreams by video_id, e.g. vualto_strubru, vualto_mnm
elif video_id and not video_url:
api_data = apidata.ApiData(self._CLIENT, self._VUALTO_API_URL, video_id, '', None, True)
# Support .mpd streams directly (only works on Kodi 18+ with inputstream.adaptive)
elif video_url.endswith('.mpd'):
return streamurls.StreamURLS(video_url, use_inputstream_adaptive=True)
# Webscrape api_data from html video_url
else:
api_data = api_data or self._get_api_data(video_url)
api_data = api_data or self._get_api_data(video_url, video_id)

vudrm_token = None
video_json = self._get_video_json(api_data)
Expand Down
9 changes: 8 additions & 1 deletion resources/lib/vrtplayer/vrtplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,16 @@ def show_livestream_items(self):
thumbnail = 'DefaultAddonMusic.png'
fanart = 'DefaultAddonPVRClient.png'
plot = self._kodi_wrapper.get_localized_string(32102) % CHANNELS[channel].get('name')

url_dict = dict(action=actions.PLAY)
if CHANNELS[channel].get('live_stream'):
url_dict['video_url'] = CHANNELS[channel].get('live_stream')
if CHANNELS[channel].get('live_stream_id'):
url_dict['video_id'] = CHANNELS[channel].get('live_stream_id')

livestream_items.append(helperobjects.TitleItem(
title=self._kodi_wrapper.get_localized_string(32101) % CHANNELS[channel].get('name'),
url_dict=dict(action=actions.PLAY, video_url=CHANNELS[channel].get('live_stream')),
url_dict=url_dict,
is_playable=True,
art_dict=dict(thumb=thumbnail, icon='DefaultAddonPVRClient.png', fanart=fanart),
video_dict=dict(
Expand Down