[dvtv] Fix extraction (closes #18514)
[youtube-dl] / youtube_dl / extractor / dvtv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     determine_ext,
9     ExtractorError,
10     int_or_none,
11     js_to_json,
12     mimetype2ext,
13     try_get,
14     unescapeHTML,
15     parse_iso8601,
16 )
17
18
19 class DVTVIE(InfoExtractor):
20     IE_NAME = 'dvtv'
21     IE_DESC = 'http://video.aktualne.cz/'
22
23     _VALID_URL = r'https?://video\.aktualne\.cz/(?:[^/]+/)+r~(?P<id>[0-9a-f]{32})'
24
25     _TESTS = [{
26         'url': 'http://video.aktualne.cz/dvtv/vondra-o-ceskem-stoleti-pri-pohledu-na-havla-mi-bylo-trapne/r~e5efe9ca855511e4833a0025900fea04/',
27         'md5': '67cb83e4a955d36e1b5d31993134a0c2',
28         'info_dict': {
29             'id': 'dc0768de855511e49e4b0025900fea04',
30             'ext': 'mp4',
31             'title': 'Vondra o Českém století: Při pohledu na Havla mi bylo trapně',
32             'duration': 1484,
33             'upload_date': '20141217',
34             'timestamp': 1418792400,
35         }
36     }, {
37         'url': 'http://video.aktualne.cz/dvtv/dvtv-16-12-2014-utok-talibanu-boj-o-kliniku-uprchlici/r~973eb3bc854e11e498be002590604f2e/',
38         'info_dict': {
39             'title': r're:^DVTV 16\. 12\. 2014: útok Talibanu, boj o kliniku, uprchlíci',
40             'id': '973eb3bc854e11e498be002590604f2e',
41         },
42         'playlist': [{
43             'md5': 'da7ca6be4935532241fa9520b3ad91e4',
44             'info_dict': {
45                 'id': 'b0b40906854d11e4bdad0025900fea04',
46                 'ext': 'mp4',
47                 'title': 'Drtinová Veselovský TV 16. 12. 2014: Témata dne',
48                 'description': 'md5:0916925dea8e30fe84222582280b47a0',
49                 'timestamp': 1418760010,
50                 'upload_date': '20141216',
51             }
52         }, {
53             'md5': '5f7652a08b05009c1292317b449ffea2',
54             'info_dict': {
55                 'id': '420ad9ec854a11e4bdad0025900fea04',
56                 'ext': 'mp4',
57                 'title': 'Školní masakr možná změní boj s Talibanem, říká novinářka',
58                 'description': 'md5:ff2f9f6de73c73d7cef4f756c1c1af42',
59                 'timestamp': 1418760010,
60                 'upload_date': '20141216',
61             }
62         }, {
63             'md5': '498eb9dfa97169f409126c617e2a3d64',
64             'info_dict': {
65                 'id': '95d35580846a11e4b6d20025900fea04',
66                 'ext': 'mp4',
67                 'title': 'Boj o kliniku: Veřejný zájem, nebo právo na majetek?',
68                 'description': 'md5:889fe610a70fee5511dc3326a089188e',
69                 'timestamp': 1418760010,
70                 'upload_date': '20141216',
71             }
72         }, {
73             'md5': 'b8dc6b744844032dab6ba3781a7274b9',
74             'info_dict': {
75                 'id': '6fe14d66853511e4833a0025900fea04',
76                 'ext': 'mp4',
77                 'title': 'Pánek: Odmítání syrských uprchlíků je ostudou české vlády',
78                 'description': 'md5:544f86de6d20c4815bea11bf2ac3004f',
79                 'timestamp': 1418760010,
80                 'upload_date': '20141216',
81             }
82         }],
83     }, {
84         'url': 'https://video.aktualne.cz/dvtv/zeman-si-jen-leci-mindraky-sobotku-nenavidi-a-babis-se-mu-te/r~960cdb3a365a11e7a83b0025900fea04/',
85         'md5': 'f8efe9656017da948369aa099788c8ea',
86         'info_dict': {
87             'id': '3c496fec365911e7a6500025900fea04',
88             'ext': 'mp4',
89             'title': 'Zeman si jen léčí mindráky, Sobotku nenávidí a Babiš se mu teď hodí, tvrdí Kmenta',
90             'duration': 1103,
91             'upload_date': '20170511',
92             'timestamp': 1494514200,
93         },
94         'params': {
95             'skip_download': True,
96         },
97     }, {
98         'url': 'http://video.aktualne.cz/v-cechach-poprve-zazni-zelenkova-zrestaurovana-mse/r~45b4b00483ec11e4883b002590604f2e/',
99         'only_matching': True,
100     }, {
101         # Test live stream video (liveStarter) parsing
102         'url': 'https://video.aktualne.cz/dvtv/zive-mistryne-sveta-eva-samkova-po-navratu-ze-sampionatu/r~182654c2288811e990fd0cc47ab5f122/',
103         'md5': '2e552e483f2414851ca50467054f9d5d',
104         'info_dict': {
105             'id': '8d116360288011e98c840cc47ab5f122',
106             'ext': 'mp4',
107             'title': 'Živě: Mistryně světa Eva Samková po návratu ze šampionátu',
108             'upload_date': '20190204',
109             'timestamp': 1549289591,
110         },
111         'params': {
112             # Video content is no longer available
113             'skip_download': True,
114         },
115     }]
116
117     def _parse_video_metadata(self, js, video_id, timestamp):
118
119         data = self._parse_json(js, video_id, transform_source=js_to_json)
120
121         live_starter = try_get(data, lambda x: x['plugins']['liveStarter'], dict)
122         if live_starter:
123             data.update(live_starter)
124
125         title = unescapeHTML(data['title'])
126
127         formats = []
128
129         for tracks in data.get('tracks', {}).values():
130             for video in tracks:
131                 video_url = video.get('src')
132                 if not video_url:
133                     continue
134                 video_type = video.get('type')
135                 ext = determine_ext(video_url, mimetype2ext(video_type))
136                 if video_type == 'application/vnd.apple.mpegurl' or ext == 'm3u8':
137                     formats.extend(self._extract_m3u8_formats(
138                         video_url, video_id, 'mp4', entry_protocol='m3u8_native',
139                         m3u8_id='hls', fatal=False))
140                 elif video_type == 'application/dash+xml' or ext == 'mpd':
141                     formats.extend(self._extract_mpd_formats(
142                         video_url, video_id, mpd_id='dash', fatal=False))
143                 else:
144                     label = video.get('label')
145                     height = self._search_regex(
146                         r'^(\d+)[pP]', label or '', 'height', default=None)
147                     format_id = ['http']
148                     for f in (ext, label):
149                         if f:
150                             format_id.append(f)
151                     formats.append({
152                         'url': video_url,
153                         'format_id': '-'.join(format_id),
154                         'height': int_or_none(height),
155                     })
156         self._sort_formats(formats)
157
158         return {
159             'id': data.get('mediaid') or video_id,
160             'title': title,
161             'description': data.get('description'),
162             'thumbnail': data.get('image'),
163             'duration': int_or_none(data.get('duration')),
164             'timestamp': int_or_none(timestamp),
165             'formats': formats
166         }
167
168     def _real_extract(self, url):
169         video_id = self._match_id(url)
170
171         webpage = self._download_webpage(url, video_id)
172
173         timestamp = parse_iso8601(self._html_search_meta(
174             'article:published_time', webpage, 'published time', default=None))
175
176         # playlist
177         items = re.findall(
178             r"(?s)playlist\.push\(({.+?})\);",
179             webpage)
180
181         if items:
182             return {
183                 '_type': 'playlist',
184                 'id': video_id,
185                 'title': self._og_search_title(webpage),
186                 'entries': [self._parse_video_metadata(i, video_id, timestamp) for i in items]
187             }
188
189         # single video
190         item = self._search_regex(
191             r'(?s)BBXPlayer.setup\((.+?)\);',
192             webpage, 'video', default=None)
193
194         if item:
195             # remove function calls (ex. htmldeentitize)
196             # TODO this should be fixed in a general way in the js_to_json
197             item = re.sub(r'\w+?\((.+)\)', r'\1', item)
198
199         if item:
200             return self._parse_video_metadata(item, video_id, timestamp)
201
202         raise ExtractorError('Could not find neither video nor playlist')