]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/francetv.py
[ie/RideHome] Add extractor (#8875)
[yt-dlp.git] / yt_dlp / extractor / francetv.py
CommitLineData
ede624d1 1import re
9749ac7f 2import urllib.parse
3
5d8afe69 4from .common import InfoExtractor
71f28097 5from .dailymotion import DailymotionIE
9749ac7f 6from ..networking import HEADRequest
1cc79574 7from ..utils import (
71f28097 8 determine_ext,
9749ac7f 9 filter_dict,
28fe35b4 10 format_field,
71f28097
PG
11 int_or_none,
12 join_nonempty,
28fe35b4 13 parse_iso8601,
9749ac7f 14 smuggle_url,
15 unsmuggle_url,
16 url_or_none,
5d8afe69 17)
9749ac7f 18from ..utils.traversal import traverse_obj
5d8afe69
PR
19
20
648d25d4 21class FranceTVBaseInfoExtractor(InfoExtractor):
ede624d1 22 def _make_url_result(self, video_id, url=None):
23 video_id = video_id.split('@')[0] # for compat with old @catalog IDs
24 full_id = f'francetv:{video_id}'
9749ac7f 25 if url:
26 full_id = smuggle_url(full_id, {'hostname': urllib.parse.urlparse(url).hostname})
ede624d1 27 return self.url_result(full_id, FranceTVIE, video_id)
99892e99
S
28
29
30class FranceTVIE(InfoExtractor):
ede624d1 31 _VALID_URL = r'francetv:(?P<id>[^@#]+)'
9749ac7f 32 _GEO_COUNTRIES = ['FR']
33 _GEO_BYPASS = False
99892e99
S
34
35 _TESTS = [{
ede624d1 36 'url': 'francetv:ec217ecc-0733-48cf-ac06-af1347b849d1',
99892e99 37 'info_dict': {
ede624d1 38 'id': 'ec217ecc-0733-48cf-ac06-af1347b849d1',
99892e99
S
39 'ext': 'mp4',
40 'title': '13h15, le dimanche... - Les mystères de Jésus',
99892e99 41 'timestamp': 1502623500,
ede624d1 42 'duration': 2580,
43 'thumbnail': r're:^https?://.*\.jpg$',
99892e99
S
44 'upload_date': '20170813',
45 },
ede624d1 46 'params': {'skip_download': 'm3u8'},
99892e99
S
47 }, {
48 'url': 'francetv:162311093',
49 'only_matching': True,
50 }, {
51 'url': 'francetv:NI_1004933@Zouzous',
52 'only_matching': True,
53 }, {
54 'url': 'francetv:NI_983319@Info-web',
55 'only_matching': True,
56 }, {
57 'url': 'francetv:NI_983319',
58 'only_matching': True,
59 }, {
60 'url': 'francetv:NI_657393@Regions',
61 'only_matching': True,
760f8121
S
62 }, {
63 # france-3 live
64 'url': 'francetv:SIM_France3',
65 'only_matching': True,
99892e99
S
66 }]
67
ede624d1 68 def _extract_video(self, video_id, hostname=None):
760f8121 69 is_live = None
8bdd16b4 70 videos = []
28fe35b4
F
71 title = None
72 subtitle = None
71f28097
PG
73 episode_number = None
74 season_number = None
28fe35b4
F
75 image = None
76 duration = None
77 timestamp = None
78 spritesheets = None
79
ede624d1 80 # desktop+chrome returns dash; mobile+safari returns hls
81 for device_type, browser in [('desktop', 'chrome'), ('mobile', 'safari')]:
28fe35b4 82 dinfo = self._download_json(
ede624d1 83 f'https://k7.ftven.fr/videos/{video_id}', video_id,
84 f'Downloading {device_type} {browser} video JSON', query=filter_dict({
28fe35b4 85 'device_type': device_type,
ede624d1 86 'browser': browser,
9749ac7f 87 'domain': hostname,
88 }), fatal=False)
28fe35b4
F
89
90 if not dinfo:
8bdd16b4 91 continue
8bdd16b4 92
9749ac7f 93 video = traverse_obj(dinfo, ('video', {dict}))
28fe35b4
F
94 if video:
95 videos.append(video)
96 if duration is None:
97 duration = video.get('duration')
98 if is_live is None:
99 is_live = video.get('is_live')
100 if spritesheets is None:
101 spritesheets = video.get('spritesheets')
102
9749ac7f 103 meta = traverse_obj(dinfo, ('meta', {dict}))
28fe35b4
F
104 if meta:
105 if title is None:
106 title = meta.get('title')
71f28097
PG
107 # meta['pre_title'] contains season and episode number for series in format "S<ID> E<ID>"
108 season_number, episode_number = self._search_regex(
109 r'S(\d+)\s*E(\d+)', meta.get('pre_title'), 'episode info', group=(1, 2), default=(None, None))
28fe35b4
F
110 if subtitle is None:
111 subtitle = meta.get('additional_title')
112 if image is None:
113 image = meta.get('image_url')
114 if timestamp is None:
115 timestamp = parse_iso8601(meta.get('broadcasted_at'))
8bdd16b4 116
9749ac7f 117 formats, subtitles, video_url = [], {}, None
118 for video in traverse_obj(videos, lambda _, v: url_or_none(v['url'])):
119 video_url = video['url']
8bdd16b4 120 format_id = video.get('format')
28fe35b4 121
9749ac7f 122 token_url = url_or_none(video.get('token'))
123 if token_url and video.get('workflow') == 'token-akamai':
124 tokenized_url = traverse_obj(self._download_json(
125 token_url, video_id, f'Downloading signed {format_id} manifest URL',
126 fatal=False, query={
127 'format': 'json',
128 'url': video_url,
129 }), ('url', {url_or_none}))
130 if tokenized_url:
131 video_url = tokenized_url
28fe35b4 132
bc03228a
S
133 ext = determine_ext(video_url)
134 if ext == 'f4m':
8faa338f 135 formats.extend(self._extract_f4m_formats(
ede624d1 136 video_url, video_id, f4m_id=format_id or ext, fatal=False))
bc03228a 137 elif ext == 'm3u8':
ede624d1 138 format_id = format_id or 'hls'
28fe35b4 139 fmts, subs = self._extract_m3u8_formats_and_subtitles(
ede624d1 140 video_url, video_id, 'mp4', m3u8_id=format_id, fatal=False)
141 for f in traverse_obj(fmts, lambda _, v: v['vcodec'] == 'none' and v.get('tbr') is None):
142 if mobj := re.match(rf'{format_id}-[Aa]udio-\w+-(?P<bitrate>\d+)', f['format_id']):
143 f.update({
144 'tbr': int_or_none(mobj.group('bitrate')),
145 'acodec': 'mp4a',
146 })
28fe35b4
F
147 formats.extend(fmts)
148 self._merge_subtitles(subs, target=subtitles)
8bdd16b4 149 elif ext == 'mpd':
28fe35b4 150 fmts, subs = self._extract_mpd_formats_and_subtitles(
ede624d1 151 video_url, video_id, mpd_id=format_id or 'dash', fatal=False)
28fe35b4
F
152 formats.extend(fmts)
153 self._merge_subtitles(subs, target=subtitles)
64892c0b
S
154 elif video_url.startswith('rtmp'):
155 formats.append({
156 'url': video_url,
ede624d1 157 'format_id': join_nonempty('rtmp', format_id),
64892c0b 158 'ext': 'flv',
64892c0b
S
159 })
160 else:
3c20208e
S
161 if self._is_valid_url(video_url, video_id, format_id):
162 formats.append({
163 'url': video_url,
164 'format_id': format_id,
165 })
8bdd16b4 166
28fe35b4
F
167 # XXX: what is video['captions']?
168
9749ac7f 169 if not formats and video_url:
170 urlh = self._request_webpage(
171 HEADRequest(video_url), video_id, 'Checking for geo-restriction',
172 fatal=False, expected_status=403)
173 if urlh and urlh.headers.get('x-errortype') == 'geo':
174 self.raise_geo_restricted(countries=self._GEO_COUNTRIES, metadata_available=True)
175
28fe35b4
F
176 for f in formats:
177 if f.get('acodec') != 'none' and f.get('language') in ('qtz', 'qad'):
178 f['language_preference'] = -10
179 f['format_note'] = 'audio description%s' % format_field(f, 'format_note', ', %s')
180
181 if spritesheets:
182 formats.append({
183 'format_id': 'spritesheets',
184 'format_note': 'storyboard',
185 'acodec': 'none',
186 'vcodec': 'none',
187 'ext': 'mhtml',
188 'protocol': 'mhtml',
9222c381 189 'url': 'about:invalid',
28fe35b4 190 'fragments': [{
b3edc806 191 'url': sheet,
28fe35b4
F
192 # XXX: not entirely accurate; each spritesheet seems to be
193 # a 10×10 grid of thumbnails corresponding to approximately
194 # 2 seconds of the video; the last spritesheet may be shorter
195 'duration': 200,
ede624d1 196 } for sheet in traverse_obj(spritesheets, (..., {url_or_none}))]
28fe35b4
F
197 })
198
7e8d73c1
JMF
199 return {
200 'id': video_id,
71f28097 201 'title': join_nonempty(title, subtitle, delim=' - ').strip(),
28fe35b4
F
202 'thumbnail': image,
203 'duration': duration,
204 'timestamp': timestamp,
760f8121 205 'is_live': is_live,
7e8d73c1 206 'formats': formats,
5dadae07 207 'subtitles': subtitles,
71f28097
PG
208 'episode': subtitle if episode_number else None,
209 'series': title if episode_number else None,
210 'episode_number': int_or_none(episode_number),
211 'season_number': int_or_none(season_number),
ede624d1 212 '_format_sort_fields': ('res', 'tbr', 'proto'), # prioritize m3u8 over dash
7e8d73c1 213 }
648d25d4 214
99892e99 215 def _real_extract(self, url):
9749ac7f 216 url, smuggled_data = unsmuggle_url(url, {})
ede624d1 217 video_id = self._match_id(url)
218 hostname = smuggled_data.get('hostname') or 'www.france.tv'
99892e99 219
ede624d1 220 return self._extract_video(video_id, hostname=hostname)
99892e99 221
648d25d4 222
99892e99 223class FranceTVSiteIE(FranceTVBaseInfoExtractor):
4489d418 224 _VALID_URL = r'https?://(?:(?:www\.)?france\.tv|mobile\.france\.tv)/(?:[^/]+/)*(?P<id>[^/]+)\.html'
5d8afe69 225
6d1ded75
S
226 _TESTS = [{
227 'url': 'https://www.france.tv/france-2/13h15-le-dimanche/140921-les-mysteres-de-jesus.html',
228 'info_dict': {
9d74ea6d 229 'id': 'ec217ecc-0733-48cf-ac06-af1347b849d1',
6d1ded75
S
230 'ext': 'mp4',
231 'title': '13h15, le dimanche... - Les mystères de Jésus',
99892e99 232 'timestamp': 1502623500,
71f28097
PG
233 'duration': 2580,
234 'thumbnail': r're:^https?://.*\.jpg$',
99892e99 235 'upload_date': '20170813',
6d1ded75
S
236 },
237 'params': {
6d1ded75
S
238 'skip_download': True,
239 },
99892e99 240 'add_ie': [FranceTVIE.ie_key()],
71f28097 241 }, {
ede624d1 242 # geo-restricted
71f28097
PG
243 'url': 'https://www.france.tv/enfants/six-huit-ans/foot2rue/saison-1/3066387-duel-au-vieux-port.html',
244 'info_dict': {
245 'id': 'a9050959-eedd-4b4a-9b0d-de6eeaa73e44',
246 'ext': 'mp4',
247 'title': 'Foot2Rue - Duel au vieux port',
248 'episode': 'Duel au vieux port',
249 'series': 'Foot2Rue',
250 'episode_number': 1,
251 'season_number': 1,
252 'timestamp': 1642761360,
253 'upload_date': '20220121',
254 'season': 'Season 1',
255 'thumbnail': r're:^https?://.*\.jpg$',
256 'duration': 1441,
257 },
6d1ded75
S
258 }, {
259 # france3
260 'url': 'https://www.france.tv/france-3/des-chiffres-et-des-lettres/139063-emission-du-mardi-9-mai-2017.html',
261 'only_matching': True,
262 }, {
263 # france4
264 'url': 'https://www.france.tv/france-4/hero-corp/saison-1/134151-apres-le-calme.html',
265 'only_matching': True,
266 }, {
267 # france5
268 'url': 'https://www.france.tv/france-5/c-a-dire/saison-10/137013-c-a-dire.html',
269 'only_matching': True,
270 }, {
271 # franceo
272 'url': 'https://www.france.tv/france-o/archipels/132249-mon-ancetre-l-esclave.html',
273 'only_matching': True,
274 }, {
275 # france2 live
276 'url': 'https://www.france.tv/france-2/direct.html',
277 'only_matching': True,
278 }, {
279 'url': 'https://www.france.tv/documentaires/histoire/136517-argentine-les-500-bebes-voles-de-la-dictature.html',
280 'only_matching': True,
281 }, {
282 'url': 'https://www.france.tv/jeux-et-divertissements/divertissements/133965-le-web-contre-attaque.html',
283 'only_matching': True,
12f01118
S
284 }, {
285 'url': 'https://mobile.france.tv/france-5/c-dans-l-air/137347-emission-du-vendredi-12-mai-2017.html',
286 'only_matching': True,
4489d418
S
287 }, {
288 'url': 'https://www.france.tv/142749-rouge-sang.html',
289 'only_matching': True,
49702e36
S
290 }, {
291 # france-3 live
292 'url': 'https://www.france.tv/france-3/direct.html',
293 'only_matching': True,
6d1ded75 294 }]
5d8afe69
PR
295
296 def _real_extract(self, url):
0a192fbe
S
297 display_id = self._match_id(url)
298
299 webpage = self._download_webpage(url, display_id)
300
6d1ded75 301 video_id = self._search_regex(
9d74ea6d 302 r'(?:data-main-video\s*=|videoId["\']?\s*[:=])\s*(["\'])(?P<id>(?:(?!\1).)+)\1',
6d1ded75
S
303 webpage, 'video id', default=None, group='id')
304
0a192fbe 305 if not video_id:
ede624d1 306 video_id = self._html_search_regex(
307 r'(?:href=|player\.setVideo\(\s*)"http://videos?\.francetv\.fr/video/([^@"]+@[^"]+)"',
308 webpage, 'video ID')
99892e99 309
ede624d1 310 return self._make_url_result(video_id, url=url)
6d1ded75
S
311
312
6d1ded75 313class FranceTVInfoIE(FranceTVBaseInfoExtractor):
3c1b4669 314 IE_NAME = 'francetvinfo.fr'
99892e99 315 _VALID_URL = r'https?://(?:www|mobile|france3-regions)\.francetvinfo\.fr/(?:[^/]+/)*(?P<id>[^/?#&.]+)'
5d8afe69 316
5c30b268 317 _TESTS = [{
13c30d1d 318 'url': 'https://www.francetvinfo.fr/replay-jt/france-3/soir-3/jt-grand-soir-3-jeudi-22-aout-2019_3561461.html',
3c1b4669 319 'info_dict': {
13c30d1d 320 'id': 'd12458ee-5062-48fe-bfdd-a30d6a01b793',
3c20208e 321 'ext': 'mp4',
3c1b4669 322 'title': 'Soir 3',
13c30d1d 323 'upload_date': '20190822',
ede624d1 324 'timestamp': 1566510730,
325 'thumbnail': r're:^https?://.*\.jpe?g$',
326 'duration': 1637,
c137cc0d
S
327 'subtitles': {
328 'fr': 'mincount:2',
329 },
648d25d4 330 },
3c20208e 331 'params': {
3c20208e
S
332 'skip_download': True,
333 },
99892e99 334 'add_ie': [FranceTVIE.ie_key()],
bbed5763 335 }, {
336 'note': 'Only an image exists in initial webpage instead of the video',
337 'url': 'https://www.francetvinfo.fr/sante/maladie/coronavirus/covid-19-en-inde-une-situation-catastrophique-a-new-dehli_4381095.html',
338 'info_dict': {
339 'id': '7d204c9e-a2d3-11eb-9e4c-000d3a23d482',
340 'ext': 'mp4',
ede624d1 341 'title': 'Covid-19 : une situation catastrophique à New Dehli - Édition du mercredi 21 avril 2021',
342 'thumbnail': r're:^https?://.*\.jpe?g$',
bbed5763 343 'duration': 76,
344 'timestamp': 1619028518,
345 'upload_date': '20210421',
346 },
347 'params': {
348 'skip_download': True,
349 },
350 'add_ie': [FranceTVIE.ie_key()],
5c30b268
PH
351 }, {
352 'url': 'http://www.francetvinfo.fr/elections/europeennes/direct-europeennes-regardez-le-debat-entre-les-candidats-a-la-presidence-de-la-commission_600639.html',
99892e99 353 'only_matching': True,
6f96e308
YCH
354 }, {
355 'url': 'http://www.francetvinfo.fr/economie/entreprises/les-entreprises-familiales-le-secret-de-la-reussite_933271.html',
99892e99 356 'only_matching': True,
db264e3c
S
357 }, {
358 'url': 'http://france3-regions.francetvinfo.fr/bretagne/cotes-d-armor/thalassa-echappee-breizh-ce-venredi-dans-les-cotes-d-armor-954961.html',
99892e99 359 'only_matching': True,
ad213a1d
YCH
360 }, {
361 # Dailymotion embed
362 'url': 'http://www.francetvinfo.fr/politique/notre-dame-des-landes/video-sur-france-inter-cecile-duflot-denonce-le-regard-meprisant-de-patrick-cohen_1520091.html',
363 'md5': 'ee7f1828f25a648addc90cb2687b1f12',
364 'info_dict': {
365 'id': 'x4iiko0',
366 'ext': 'mp4',
367 'title': 'NDDL, référendum, Brexit : Cécile Duflot répond à Patrick Cohen',
ede624d1 368 'description': 'md5:fdcb582c370756293a65cdfbc6ecd90e',
ad213a1d 369 'timestamp': 1467011958,
ad213a1d
YCH
370 'uploader': 'France Inter',
371 'uploader_id': 'x2q2ez',
ede624d1 372 'upload_date': '20160627',
373 'view_count': int,
374 'tags': ['Politique', 'France Inter', '27 juin 2016', 'Linvité de 8h20', 'Cécile Duflot', 'Patrick Cohen'],
375 'age_limit': 0,
376 'duration': 640,
377 'like_count': int,
378 'thumbnail': r're:https://[^/?#]+/v/[^/?#]+/x1080',
ad213a1d
YCH
379 },
380 'add_ie': ['Dailymotion'],
30b25d38
S
381 }, {
382 'url': 'http://france3-regions.francetvinfo.fr/limousin/emissions/jt-1213-limousin',
383 'only_matching': True,
41d1cca3 384 }, {
385 # "<figure id=" pattern (#28792)
386 'url': 'https://www.francetvinfo.fr/culture/patrimoine/incendie-de-notre-dame-de-paris/notre-dame-de-paris-de-l-incendie-de-la-cathedrale-a-sa-reconstruction_4372291.html',
387 'only_matching': True,
5c30b268 388 }]
648d25d4
JMF
389
390 def _real_extract(self, url):
99892e99
S
391 display_id = self._match_id(url)
392
393 webpage = self._download_webpage(url, display_id)
6f96e308 394
43aebb7d 395 dailymotion_urls = tuple(DailymotionIE._extract_embed_urls(url, webpage))
ad213a1d
YCH
396 if dailymotion_urls:
397 return self.playlist_result([
398 self.url_result(dailymotion_url, DailymotionIE.ie_key())
399 for dailymotion_url in dailymotion_urls])
6f96e308 400
876fed6b 401 video_id = self._search_regex(
402 (r'player\.load[^;]+src:\s*["\']([^"\']+)',
403 r'id-video=([^@]+@[^"]+)',
13c30d1d 404 r'<a[^>]+href="(?:https?:)?//videos\.francetv\.fr/video/([^@]+@[^"]+)"',
41d1cca3 405 r'(?:data-id|<figure[^<]+\bid)=["\']([\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'),
876fed6b 406 webpage, 'video id')
99892e99 407
9749ac7f 408 return self._make_url_result(video_id, url=url)