[seznamzpravy] Add extractor (closes #14102)
[youtube-dl] / youtube_dl / extractor / seznamzpravy.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import (
6     compat_parse_qs,
7     compat_str,
8     compat_urllib_parse_urlparse,
9 )
10 from ..utils import (
11     urljoin,
12     int_or_none,
13     try_get,
14     update_url_query,
15 )
16
17
18 def _raw_id(src_url):
19     return compat_urllib_parse_urlparse(src_url).path.split('/')[-1]
20
21
22 class SeznamZpravyIE(InfoExtractor):
23     _VALID_URL = r'https?://(?:www\.)?(?:seznam\.cz/zpravy|seznamzpravy\.cz)/iframe/player\?.*\bsrc='
24     _TESTS = [{
25         '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',
26         'params': {'skip_download': True},  # 'file_minsize': 1586 seems to get killed in test_download.py
27         'info_dict': {
28             'id': '170889',
29             'ext': 'mp4',
30             'title': 'Svět bez obalu: Čeští vojáci na misích (krátká verze)',
31         }
32     }]
33
34     def _extract_sdn_formats(self, sdn_url, video_id):
35         sdn_data = self._download_json(sdn_url, video_id)
36         formats = []
37         mp4_formats = try_get(sdn_data, lambda x: x['data']['mp4'], dict) or {}
38         for fmt, fmtdata in mp4_formats.items():
39             relative_url = fmtdata.get('url')
40             if not relative_url:
41                 continue
42
43             try:
44                 width, height = fmtdata.get('resolution')
45             except (TypeError, ValueError):
46                 width, height = None, None
47
48             formats.append({
49                 'format_id': fmt,
50                 'width': int_or_none(width),
51                 'height': int_or_none(height),
52                 'url': urljoin(sdn_url, relative_url),
53                 'vcodec': fmtdata.get('codec'),
54                 'tbr': int_or_none(fmtdata.get('bandwidth'), scale=1000),
55                 'duration': int_or_none(fmtdata.get('duration'), scale=1000),
56             })
57
58         playlists = sdn_data.get('pls', {})
59         dash_rel_url = try_get(playlists, lambda x: x['dash']['url'], compat_str)
60         if dash_rel_url:
61             formats.extend(self._extract_mpd_formats(urljoin(sdn_url, dash_rel_url), video_id, mpd_id='dash', fatal=False))
62
63         hls_rel_url = try_get(playlists, lambda x: x['hls']['url'], compat_str)
64         if hls_rel_url:
65             formats.extend(self._extract_m3u8_formats(urljoin(sdn_url, hls_rel_url), video_id, ext='mp4', m3u8_id='hls', fatal=False))
66
67         self._sort_formats(formats)
68         return formats
69
70     def _real_extract(self, url):
71         params = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
72         src = params['src'][0]
73         video_id = params.get('contentId', [_raw_id(src)])[0]
74
75         return {
76             'id': video_id,
77             'title': params['title'][0],
78             'formats': self._extract_sdn_formats(src + 'spl2,2,VOD', video_id),
79         }
80
81
82 class SeznamZpravyArticleIE(InfoExtractor):
83     _VALID_URL = r'https?://(?:www\.)?(?:seznam\.cz/zpravy|seznamzpravy\.cz)/clanek/(?:[-a-z0-9]+)-(?P<id>[0-9]+)'
84     _API_URL = 'https://apizpravy.seznam.cz/'
85
86     _TESTS = [{
87         # two videos on one page, with SDN URL
88         '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',
89         'params': {'skip_download': True},
90         # ^ this is here instead of 'file_minsize': 1586, which does not work because
91         #   test_download.py forces expected_minsize to at least 10k when test is running
92         'info_dict': {
93             'id': '170889',
94             'ext': 'mp4',
95             'title': 'Svět bez obalu: Čeští vojáci na misích (krátká verze)',
96         }
97     }, {
98         # video with live stream URL
99         'url': 'https://www.seznam.cz/zpravy/clanek/znovu-do-vlady-s-ano-pavel-belobradek-ve-volebnim-specialu-seznamu-38489',
100         'info_dict': {
101             'id': '185688',
102             'ext': 'mp4',
103             'title': 'Předseda KDU-ČSL Pavel Bělobrádek ve volební Výzvě Seznamu',
104         }
105     }]
106
107     def _extract_caption(self, api_data, article_id):
108         title = api_data.get('title') or api_data.get('captionTitle')
109         caption = api_data.get('caption')
110         if not title or not caption:
111             return {}
112
113         if 'sdn' in caption.get('video', {}):
114             src_url = caption['video']['sdn']
115         elif 'liveStreamUrl' in caption:
116             src_url = self._download_json(caption['liveStreamUrl'], article_id)['Location']
117         else:
118             return {}
119
120         return {
121             'id': caption.get('uid'),
122             'title': caption.get('title'),
123             'src': src_url,
124         }
125
126     def _extract_content(self, api_data):
127         entries = []
128         for item in api_data.get('content', []):
129             media = item.get('properties', {}).get('media', {})
130             src_url = media.get('video', {}).get('sdn')
131             title = media.get('title')
132             if not src_url or not title:
133                 continue
134
135             entries.append({
136                 'id': media.get('uid'),
137                 'title': title,
138                 'src': src_url,
139             })
140
141         return entries
142
143     def _iframe_result(self, info_dict):
144         video_id = info_dict['id'] or _raw_id(info_dict['src'])
145         url = update_url_query('https://www.seznam.cz/zpravy/iframe/player', {
146             'src': info_dict['src'],
147             'title': info_dict['title'],
148             'contentId': video_id,
149             'serviceName': 'Seznam Zprávy',
150         })
151         return self.url_result(url, ie='SeznamZpravy', video_id=video_id, video_title=info_dict['title'])
152
153     def _real_extract(self, url):
154         article_id = self._match_id(url)
155         api_data = self._download_json(self._API_URL + 'v1/documents/' + article_id, article_id)
156
157         caption = self._extract_caption(api_data, article_id)
158         content = self._extract_content(api_data)
159
160         if caption and not content:
161             return self._iframe_result(caption)
162         else:
163             if caption:
164                 content.insert(0, caption)
165             return self.playlist_result(
166                 [self._iframe_result(x) for x in content],
167                 playlist_id=article_id,
168                 playlist_title=api_data.get('title') or caption.get('title')
169             )