[seznamzpravy] Improve and simplify (closes #14616)

This commit is contained in:
Sergey M․ 2018-01-27 23:36:44 +07:00
parent 27940ca09c
commit 3c3a07ee0b
No known key found for this signature in database
GPG Key ID: 2C393E0F18A9236D

View File

@ -1,6 +1,8 @@
# coding: utf-8 # coding: utf-8
from __future__ import unicode_literals from __future__ import unicode_literals
import re
from .common import InfoExtractor from .common import InfoExtractor
from ..compat import ( from ..compat import (
compat_parse_qs, compat_parse_qs,
@ -10,8 +12,8 @@ from ..compat import (
from ..utils import ( from ..utils import (
urljoin, urljoin,
int_or_none, int_or_none,
parse_codecs,
try_get, try_get,
update_url_query,
) )
@ -20,150 +22,149 @@ def _raw_id(src_url):
class SeznamZpravyIE(InfoExtractor): class SeznamZpravyIE(InfoExtractor):
_VALID_URL = r'https?://(?:www\.)?(?:seznam\.cz/zpravy|seznamzpravy\.cz)/iframe/player\?.*\bsrc=' _VALID_URL = r'https?://(?:www\.)?seznamzpravy\.cz/iframe/player\?.*\bsrc='
_TESTS = [{ _TESTS = [{
'url': r'https://www.seznamzpravy.cz/iframe/player?duration=241&serviceSlug=zpravy&src=https%3A%2F%2Fv39-a.sdn.szn.cz%2Fv_39%2Fvmd%2F5999c902ea707c67d8e267a9%3Ffl%3Dmdk%2C432f65a0%7C&itemType=video&autoPlay=false&title=Sv%C4%9Bt%20bez%20obalu%3A%20%C4%8Ce%C5%A1t%C3%AD%20voj%C3%A1ci%20na%20mis%C3%ADch%20(kr%C3%A1tk%C3%A1%20verze)&series=Sv%C4%9Bt%20bez%20obalu&serviceName=Seznam%20Zpr%C3%A1vy&poster=%2F%2Fd39-a.sdn.szn.cz%2Fd_39%2Fc_img_F_I%2FR5puJ.jpeg%3Ffl%3Dcro%2C0%2C0%2C1920%2C1080%7Cres%2C1200%2C%2C1%7Cjpg%2C80%2C%2C1&width=1920&height=1080&cutFrom=0&cutTo=0&splVersion=VOD&contentId=170889&contextId=35990&showAdvert=true&collocation=&autoplayPossible=true&embed=&isVideoTooShortForPreroll=false&isVideoTooLongForPostroll=true&videoCommentOpKey=&videoCommentId=&version=4.0.76&dotService=zpravy&gemiusPrismIdentifier=bVc1ZIb_Qax4W2v5xOPGpMeCP31kFfrTzj0SqPTLh_b.Z7&zoneIdPreroll=seznam.pack.videospot&skipOffsetPreroll=5&sectionPrefixPreroll=%2Fzpravy', 'url': 'https://www.seznamzpravy.cz/iframe/player?duration=241&serviceSlug=zpravy&src=https%3A%2F%2Fv39-a.sdn.szn.cz%2Fv_39%2Fvmd%2F5999c902ea707c67d8e267a9%3Ffl%3Dmdk%2C432f65a0%7C&itemType=video&autoPlay=false&title=Sv%C4%9Bt%20bez%20obalu%3A%20%C4%8Ce%C5%A1t%C3%AD%20voj%C3%A1ci%20na%20mis%C3%ADch%20(kr%C3%A1tk%C3%A1%20verze)&series=Sv%C4%9Bt%20bez%20obalu&serviceName=Seznam%20Zpr%C3%A1vy&poster=%2F%2Fd39-a.sdn.szn.cz%2Fd_39%2Fc_img_F_I%2FR5puJ.jpeg%3Ffl%3Dcro%2C0%2C0%2C1920%2C1080%7Cres%2C1200%2C%2C1%7Cjpg%2C80%2C%2C1&width=1920&height=1080&cutFrom=0&cutTo=0&splVersion=VOD&contentId=170889&contextId=35990&showAdvert=true&collocation=&autoplayPossible=true&embed=&isVideoTooShortForPreroll=false&isVideoTooLongForPostroll=true&videoCommentOpKey=&videoCommentId=&version=4.0.76&dotService=zpravy&gemiusPrismIdentifier=bVc1ZIb_Qax4W2v5xOPGpMeCP31kFfrTzj0SqPTLh_b.Z7&zoneIdPreroll=seznam.pack.videospot&skipOffsetPreroll=5&sectionPrefixPreroll=%2Fzpravy',
'params': {'skip_download': True}, # 'file_minsize': 1586 seems to get killed in test_download.py
'info_dict': { 'info_dict': {
'id': '170889', 'id': '170889',
'ext': 'mp4', 'ext': 'mp4',
'title': 'Svět bez obalu: Čeští vojáci na misích (krátká verze)', 'title': 'Svět bez obalu: Čeští vojáci na misích (krátká verze)',
} 'thumbnail': r're:^https?://.*\.jpe?g',
'duration': 241,
'series': 'Svět bez obalu',
},
'params': {
'skip_download': True,
},
}, {
# with Location key
'url': 'https://www.seznamzpravy.cz/iframe/player?duration=null&serviceSlug=zpravy&src=https%3A%2F%2Flive-a.sdn.szn.cz%2Fv_39%2F59e468fe454f8472a96af9fa%3Ffl%3Dmdk%2C5c1e2840%7C&itemType=livevod&autoPlay=false&title=P%C5%99edseda%20KDU-%C4%8CSL%20Pavel%20B%C4%9Blobr%C3%A1dek%20ve%20volebn%C3%AD%20V%C3%BDzv%C4%9B%20Seznamu&series=V%C3%BDzva&serviceName=Seznam%20Zpr%C3%A1vy&poster=%2F%2Fd39-a.sdn.szn.cz%2Fd_39%2Fc_img_G_J%2FjTBCs.jpeg%3Ffl%3Dcro%2C0%2C0%2C1280%2C720%7Cres%2C1200%2C%2C1%7Cjpg%2C80%2C%2C1&width=16&height=9&cutFrom=0&cutTo=0&splVersion=VOD&contentId=185688&contextId=38489&showAdvert=true&collocation=&hideFullScreen=false&hideSubtitles=false&embed=&isVideoTooShortForPreroll=false&isVideoTooShortForPreroll2=false&isVideoTooLongForPostroll=false&fakePostrollZoneID=seznam.clanky.zpravy.preroll&fakePrerollZoneID=seznam.clanky.zpravy.preroll&videoCommentId=&trim=default_16x9&noPrerollVideoLength=30&noPreroll2VideoLength=undefined&noMidrollVideoLength=0&noPostrollVideoLength=999999&autoplayPossible=true&version=5.0.41&dotService=zpravy&gemiusPrismIdentifier=zD3g7byfW5ekpXmxTVLaq5Srjw5i4hsYo0HY1aBwIe..27&zoneIdPreroll=seznam.pack.videospot&skipOffsetPreroll=5&sectionPrefixPreroll=%2Fzpravy%2Fvyzva&zoneIdPostroll=seznam.pack.videospot&skipOffsetPostroll=5&sectionPrefixPostroll=%2Fzpravy%2Fvyzva&regression=false',
'info_dict': {
'id': '185688',
'ext': 'mp4',
'title': 'Předseda KDU-ČSL Pavel Bělobrádek ve volební Výzvě Seznamu',
'thumbnail': r're:^https?://.*\.jpe?g',
'series': 'Výzva',
},
'params': {
'skip_download': True,
},
}] }]
@staticmethod
def _extract_urls(webpage):
return [
mobj.group('url') for mobj in re.finditer(
r'<iframe\b[^>]+\bsrc=(["\'])(?P<url>(?:https?:)?//(?:www\.)?seznamzpravy\.cz/iframe/player\?.*?)\1',
webpage)]
def _extract_sdn_formats(self, sdn_url, video_id): def _extract_sdn_formats(self, sdn_url, video_id):
sdn_data = self._download_json(sdn_url, video_id) sdn_data = self._download_json(sdn_url, video_id)
if sdn_data.get('Location'):
sdn_url = sdn_data['Location']
sdn_data = self._download_json(sdn_url, video_id)
formats = [] formats = []
mp4_formats = try_get(sdn_data, lambda x: x['data']['mp4'], dict) or {} mp4_formats = try_get(sdn_data, lambda x: x['data']['mp4'], dict) or {}
for fmt, fmtdata in mp4_formats.items(): for format_id, format_data in mp4_formats.items():
relative_url = fmtdata.get('url') relative_url = format_data.get('url')
if not relative_url: if not relative_url:
continue continue
try: try:
width, height = fmtdata.get('resolution') width, height = format_data.get('resolution')
except (TypeError, ValueError): except (TypeError, ValueError):
width, height = None, None width, height = None, None
formats.append({ f = {
'format_id': fmt, 'url': urljoin(sdn_url, relative_url),
'format_id': 'http-%s' % format_id,
'tbr': int_or_none(format_data.get('bandwidth'), scale=1000),
'width': int_or_none(width), 'width': int_or_none(width),
'height': int_or_none(height), 'height': int_or_none(height),
'url': urljoin(sdn_url, relative_url), }
'vcodec': fmtdata.get('codec'), f.update(parse_codecs(format_data.get('codec')))
'tbr': int_or_none(fmtdata.get('bandwidth'), scale=1000), formats.append(f)
'duration': int_or_none(fmtdata.get('duration'), scale=1000),
})
playlists = sdn_data.get('pls', {}) pls = sdn_data.get('pls', {})
dash_rel_url = try_get(playlists, lambda x: x['dash']['url'], compat_str)
def get_url(format_id):
return try_get(pls, lambda x: x[format_id]['url'], compat_str)
dash_rel_url = get_url('dash')
if dash_rel_url: if dash_rel_url:
formats.extend(self._extract_mpd_formats(urljoin(sdn_url, dash_rel_url), video_id, mpd_id='dash', fatal=False)) formats.extend(self._extract_mpd_formats(
urljoin(sdn_url, dash_rel_url), video_id, mpd_id='dash',
fatal=False))
hls_rel_url = try_get(playlists, lambda x: x['hls']['url'], compat_str) hls_rel_url = get_url('hls')
if hls_rel_url: if hls_rel_url:
formats.extend(self._extract_m3u8_formats(urljoin(sdn_url, hls_rel_url), video_id, ext='mp4', m3u8_id='hls', fatal=False)) formats.extend(self._extract_m3u8_formats(
urljoin(sdn_url, hls_rel_url), video_id, ext='mp4',
m3u8_id='hls', fatal=False))
self._sort_formats(formats) self._sort_formats(formats)
return formats return formats
def _real_extract(self, url): def _real_extract(self, url):
params = compat_parse_qs(compat_urllib_parse_urlparse(url).query) params = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
src = params['src'][0] src = params['src'][0]
title = params['title'][0]
video_id = params.get('contentId', [_raw_id(src)])[0] video_id = params.get('contentId', [_raw_id(src)])[0]
formats = self._extract_sdn_formats(src + 'spl2,2,VOD', video_id)
duration = int_or_none(params.get('duration', [None])[0])
series = params.get('series', [None])[0]
thumbnail = params.get('poster', [None])[0]
return { return {
'id': video_id, 'id': video_id,
'title': params['title'][0], 'title': title,
'formats': self._extract_sdn_formats(src + 'spl2,2,VOD', video_id), 'thumbnail': thumbnail,
'duration': duration,
'series': series,
'formats': formats,
} }
class SeznamZpravyArticleIE(InfoExtractor): class SeznamZpravyArticleIE(InfoExtractor):
_VALID_URL = r'https?://(?:www\.)?(?:seznam\.cz/zpravy|seznamzpravy\.cz)/clanek/(?:[-a-z0-9]+)-(?P<id>[0-9]+)' _VALID_URL = r'https?://(?:www\.)?(?:seznam\.cz/zpravy|seznamzpravy\.cz)/clanek/(?:[^/?#&]+)-(?P<id>\d+)'
_API_URL = 'https://apizpravy.seznam.cz/' _API_URL = 'https://apizpravy.seznam.cz/'
_TESTS = [{ _TESTS = [{
# two videos on one page, with SDN URL # two videos on one page, with SDN URL
'url': 'https://www.seznamzpravy.cz/clanek/jejich-svet-na-nas-utoci-je-lepsi-branit-se-na-jejich-pisecku-rika-reziser-a-major-v-zaloze-marhoul-35990', 'url': 'https://www.seznamzpravy.cz/clanek/jejich-svet-na-nas-utoci-je-lepsi-branit-se-na-jejich-pisecku-rika-reziser-a-major-v-zaloze-marhoul-35990',
'params': {'skip_download': True},
# ^ this is here instead of 'file_minsize': 1586, which does not work because
# test_download.py forces expected_minsize to at least 10k when test is running
'info_dict': { 'info_dict': {
'id': '170889', 'id': '35990',
'ext': 'mp4', 'title': 'md5:6011c877a36905f28f271fcd8dcdb0f2',
'title': 'Svět bez obalu: Čeští vojáci na misích (krátká verze)', 'description': 'md5:933f7b06fa337a814ba199d3596d27ba',
} },
'playlist_count': 2,
}, { }, {
# video with live stream URL # video with live stream URL
'url': 'https://www.seznam.cz/zpravy/clanek/znovu-do-vlady-s-ano-pavel-belobradek-ve-volebnim-specialu-seznamu-38489', 'url': 'https://www.seznam.cz/zpravy/clanek/znovu-do-vlady-s-ano-pavel-belobradek-ve-volebnim-specialu-seznamu-38489',
'info_dict': { 'info_dict': {
'id': '185688', 'id': '38489',
'ext': 'mp4', 'title': 'md5:8fa1afdc36fd378cf0eba2b74c5aca60',
'title': 'Předseda KDU-ČSL Pavel Bělobrádek ve volební Výzvě Seznamu', 'description': 'md5:428e7926a1a81986ec7eb23078004fb4',
} },
'playlist_count': 1,
}] }]
def _extract_caption(self, api_data, article_id):
title = api_data.get('title') or api_data.get('captionTitle')
caption = api_data.get('caption')
if not title or not caption:
return {}
if 'sdn' in caption.get('video', {}):
src_url = caption['video']['sdn']
elif 'liveStreamUrl' in caption:
src_url = self._download_json(caption['liveStreamUrl'], article_id)['Location']
else:
return {}
return {
'id': caption.get('uid'),
'title': caption.get('title'),
'src': src_url,
}
def _extract_content(self, api_data):
entries = []
for item in api_data.get('content', []):
media = item.get('properties', {}).get('media', {})
src_url = media.get('video', {}).get('sdn')
title = media.get('title')
if not src_url or not title:
continue
entries.append({
'id': media.get('uid'),
'title': title,
'src': src_url,
})
return entries
def _iframe_result(self, info_dict):
video_id = info_dict['id'] or _raw_id(info_dict['src'])
url = update_url_query('https://www.seznam.cz/zpravy/iframe/player', {
'src': info_dict['src'],
'title': info_dict['title'],
'contentId': video_id,
'serviceName': 'Seznam Zprávy',
})
return self.url_result(url, ie='SeznamZpravy', video_id=video_id, video_title=info_dict['title'])
def _real_extract(self, url): def _real_extract(self, url):
article_id = self._match_id(url) article_id = self._match_id(url)
api_data = self._download_json(self._API_URL + 'v1/documents/' + article_id, article_id)
caption = self._extract_caption(api_data, article_id) webpage = self._download_webpage(url, article_id)
content = self._extract_content(api_data)
if caption and not content: info = self._search_json_ld(webpage, article_id, default={})
return self._iframe_result(caption) print(info)
else:
if caption: title = info.get('title') or self._og_search_title(webpage, fatal=False)
content.insert(0, caption) description = info.get('description') or self._og_search_description(webpage)
return self.playlist_result(
[self._iframe_result(x) for x in content], return self.playlist_result([
playlist_id=article_id, self.url_result(url, ie=SeznamZpravyIE.ie_key())
playlist_title=api_data.get('title') or caption.get('title') for url in SeznamZpravyIE._extract_urls(webpage)],
) article_id, title, description)