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