]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/francetv.py
[cleanup] Add more ruff rules (#10149)
[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
e4fbe5f8 122 if token_url := url_or_none(video.get('token')):
9749ac7f 123 tokenized_url = traverse_obj(self._download_json(
124 token_url, video_id, f'Downloading signed {format_id} manifest URL',
125 fatal=False, query={
126 'format': 'json',
127 'url': video_url,
128 }), ('url', {url_or_none}))
129 if tokenized_url:
130 video_url = tokenized_url
28fe35b4 131
bc03228a
S
132 ext = determine_ext(video_url)
133 if ext == 'f4m':
8faa338f 134 formats.extend(self._extract_f4m_formats(
ede624d1 135 video_url, video_id, f4m_id=format_id or ext, fatal=False))
bc03228a 136 elif ext == 'm3u8':
ede624d1 137 format_id = format_id or 'hls'
28fe35b4 138 fmts, subs = self._extract_m3u8_formats_and_subtitles(
ede624d1 139 video_url, video_id, 'mp4', m3u8_id=format_id, fatal=False)
140 for f in traverse_obj(fmts, lambda _, v: v['vcodec'] == 'none' and v.get('tbr') is None):
141 if mobj := re.match(rf'{format_id}-[Aa]udio-\w+-(?P<bitrate>\d+)', f['format_id']):
142 f.update({
143 'tbr': int_or_none(mobj.group('bitrate')),
144 'acodec': 'mp4a',
145 })
28fe35b4
F
146 formats.extend(fmts)
147 self._merge_subtitles(subs, target=subtitles)
8bdd16b4 148 elif ext == 'mpd':
28fe35b4 149 fmts, subs = self._extract_mpd_formats_and_subtitles(
ede624d1 150 video_url, video_id, mpd_id=format_id or 'dash', fatal=False)
28fe35b4
F
151 formats.extend(fmts)
152 self._merge_subtitles(subs, target=subtitles)
64892c0b
S
153 elif video_url.startswith('rtmp'):
154 formats.append({
155 'url': video_url,
ede624d1 156 'format_id': join_nonempty('rtmp', format_id),
64892c0b 157 'ext': 'flv',
64892c0b
S
158 })
159 else:
3c20208e
S
160 if self._is_valid_url(video_url, video_id, format_id):
161 formats.append({
162 'url': video_url,
163 'format_id': format_id,
164 })
8bdd16b4 165
28fe35b4
F
166 # XXX: what is video['captions']?
167
9749ac7f 168 if not formats and video_url:
169 urlh = self._request_webpage(
170 HEADRequest(video_url), video_id, 'Checking for geo-restriction',
171 fatal=False, expected_status=403)
172 if urlh and urlh.headers.get('x-errortype') == 'geo':
173 self.raise_geo_restricted(countries=self._GEO_COUNTRIES, metadata_available=True)
174
28fe35b4
F
175 for f in formats:
176 if f.get('acodec') != 'none' and f.get('language') in ('qtz', 'qad'):
177 f['language_preference'] = -10
add96eb9 178 f['format_note'] = 'audio description{}'.format(format_field(f, 'format_note', ', %s'))
28fe35b4
F
179
180 if spritesheets:
181 formats.append({
182 'format_id': 'spritesheets',
183 'format_note': 'storyboard',
184 'acodec': 'none',
185 'vcodec': 'none',
186 'ext': 'mhtml',
187 'protocol': 'mhtml',
9222c381 188 'url': 'about:invalid',
28fe35b4 189 'fragments': [{
b3edc806 190 'url': sheet,
28fe35b4 191 # XXX: not entirely accurate; each spritesheet seems to be
add96eb9 192 # a 10x10 grid of thumbnails corresponding to approximately
28fe35b4
F
193 # 2 seconds of the video; the last spritesheet may be shorter
194 'duration': 200,
add96eb9 195 } for sheet in traverse_obj(spritesheets, (..., {url_or_none}))],
28fe35b4
F
196 })
197
7e8d73c1
JMF
198 return {
199 'id': video_id,
71f28097 200 'title': join_nonempty(title, subtitle, delim=' - ').strip(),
28fe35b4
F
201 'thumbnail': image,
202 'duration': duration,
203 'timestamp': timestamp,
760f8121 204 'is_live': is_live,
7e8d73c1 205 'formats': formats,
5dadae07 206 'subtitles': subtitles,
71f28097
PG
207 'episode': subtitle if episode_number else None,
208 'series': title if episode_number else None,
209 'episode_number': int_or_none(episode_number),
210 'season_number': int_or_none(season_number),
ede624d1 211 '_format_sort_fields': ('res', 'tbr', 'proto'), # prioritize m3u8 over dash
7e8d73c1 212 }
648d25d4 213
99892e99 214 def _real_extract(self, url):
9749ac7f 215 url, smuggled_data = unsmuggle_url(url, {})
ede624d1 216 video_id = self._match_id(url)
217 hostname = smuggled_data.get('hostname') or 'www.france.tv'
99892e99 218
ede624d1 219 return self._extract_video(video_id, hostname=hostname)
99892e99 220
648d25d4 221
99892e99 222class FranceTVSiteIE(FranceTVBaseInfoExtractor):
4489d418 223 _VALID_URL = r'https?://(?:(?:www\.)?france\.tv|mobile\.france\.tv)/(?:[^/]+/)*(?P<id>[^/]+)\.html'
5d8afe69 224
6d1ded75
S
225 _TESTS = [{
226 'url': 'https://www.france.tv/france-2/13h15-le-dimanche/140921-les-mysteres-de-jesus.html',
227 'info_dict': {
9d74ea6d 228 'id': 'ec217ecc-0733-48cf-ac06-af1347b849d1',
6d1ded75
S
229 'ext': 'mp4',
230 'title': '13h15, le dimanche... - Les mystères de Jésus',
99892e99 231 'timestamp': 1502623500,
71f28097
PG
232 'duration': 2580,
233 'thumbnail': r're:^https?://.*\.jpg$',
99892e99 234 'upload_date': '20170813',
6d1ded75
S
235 },
236 'params': {
6d1ded75
S
237 'skip_download': True,
238 },
99892e99 239 'add_ie': [FranceTVIE.ie_key()],
71f28097 240 }, {
ede624d1 241 # geo-restricted
71f28097
PG
242 'url': 'https://www.france.tv/enfants/six-huit-ans/foot2rue/saison-1/3066387-duel-au-vieux-port.html',
243 'info_dict': {
244 'id': 'a9050959-eedd-4b4a-9b0d-de6eeaa73e44',
245 'ext': 'mp4',
246 'title': 'Foot2Rue - Duel au vieux port',
247 'episode': 'Duel au vieux port',
248 'series': 'Foot2Rue',
249 'episode_number': 1,
250 'season_number': 1,
251 'timestamp': 1642761360,
252 'upload_date': '20220121',
253 'season': 'Season 1',
254 'thumbnail': r're:^https?://.*\.jpg$',
255 'duration': 1441,
256 },
e4fbe5f8 257 }, {
258 # geo-restricted livestream (workflow == 'token-akamai')
259 'url': 'https://www.france.tv/france-4/direct.html',
260 'info_dict': {
261 'id': '9a6a7670-dde9-4264-adbc-55b89558594b',
262 'ext': 'mp4',
263 'title': r're:France 4 en direct .+',
264 'live_status': 'is_live',
265 },
266 'skip': 'geo-restricted livestream',
267 }, {
268 # livestream (workflow == 'dai')
269 'url': 'https://www.france.tv/france-2/direct.html',
270 'info_dict': {
271 'id': '006194ea-117d-4bcf-94a9-153d999c59ae',
272 'ext': 'mp4',
273 'title': r're:France 2 en direct .+',
274 'live_status': 'is_live',
275 },
276 'params': {'skip_download': 'livestream'},
6d1ded75
S
277 }, {
278 # france3
279 'url': 'https://www.france.tv/france-3/des-chiffres-et-des-lettres/139063-emission-du-mardi-9-mai-2017.html',
280 'only_matching': True,
281 }, {
282 # france4
283 'url': 'https://www.france.tv/france-4/hero-corp/saison-1/134151-apres-le-calme.html',
284 'only_matching': True,
285 }, {
286 # france5
287 'url': 'https://www.france.tv/france-5/c-a-dire/saison-10/137013-c-a-dire.html',
288 'only_matching': True,
289 }, {
290 # franceo
291 'url': 'https://www.france.tv/france-o/archipels/132249-mon-ancetre-l-esclave.html',
292 'only_matching': True,
6d1ded75
S
293 }, {
294 'url': 'https://www.france.tv/documentaires/histoire/136517-argentine-les-500-bebes-voles-de-la-dictature.html',
295 'only_matching': True,
296 }, {
297 'url': 'https://www.france.tv/jeux-et-divertissements/divertissements/133965-le-web-contre-attaque.html',
298 'only_matching': True,
12f01118
S
299 }, {
300 'url': 'https://mobile.france.tv/france-5/c-dans-l-air/137347-emission-du-vendredi-12-mai-2017.html',
301 'only_matching': True,
4489d418
S
302 }, {
303 'url': 'https://www.france.tv/142749-rouge-sang.html',
304 'only_matching': True,
49702e36
S
305 }, {
306 # france-3 live
307 'url': 'https://www.france.tv/france-3/direct.html',
308 'only_matching': True,
6d1ded75 309 }]
5d8afe69
PR
310
311 def _real_extract(self, url):
0a192fbe
S
312 display_id = self._match_id(url)
313
314 webpage = self._download_webpage(url, display_id)
315
6d1ded75 316 video_id = self._search_regex(
9d74ea6d 317 r'(?:data-main-video\s*=|videoId["\']?\s*[:=])\s*(["\'])(?P<id>(?:(?!\1).)+)\1',
6d1ded75
S
318 webpage, 'video id', default=None, group='id')
319
0a192fbe 320 if not video_id:
ede624d1 321 video_id = self._html_search_regex(
322 r'(?:href=|player\.setVideo\(\s*)"http://videos?\.francetv\.fr/video/([^@"]+@[^"]+)"',
323 webpage, 'video ID')
99892e99 324
ede624d1 325 return self._make_url_result(video_id, url=url)
6d1ded75
S
326
327
6d1ded75 328class FranceTVInfoIE(FranceTVBaseInfoExtractor):
3c1b4669 329 IE_NAME = 'francetvinfo.fr'
99892e99 330 _VALID_URL = r'https?://(?:www|mobile|france3-regions)\.francetvinfo\.fr/(?:[^/]+/)*(?P<id>[^/?#&.]+)'
5d8afe69 331
5c30b268 332 _TESTS = [{
13c30d1d 333 'url': 'https://www.francetvinfo.fr/replay-jt/france-3/soir-3/jt-grand-soir-3-jeudi-22-aout-2019_3561461.html',
3c1b4669 334 'info_dict': {
13c30d1d 335 'id': 'd12458ee-5062-48fe-bfdd-a30d6a01b793',
3c20208e 336 'ext': 'mp4',
3c1b4669 337 'title': 'Soir 3',
13c30d1d 338 'upload_date': '20190822',
ede624d1 339 'timestamp': 1566510730,
340 'thumbnail': r're:^https?://.*\.jpe?g$',
341 'duration': 1637,
c137cc0d
S
342 'subtitles': {
343 'fr': 'mincount:2',
344 },
648d25d4 345 },
3c20208e 346 'params': {
3c20208e
S
347 'skip_download': True,
348 },
99892e99 349 'add_ie': [FranceTVIE.ie_key()],
bbed5763 350 }, {
351 'note': 'Only an image exists in initial webpage instead of the video',
352 'url': 'https://www.francetvinfo.fr/sante/maladie/coronavirus/covid-19-en-inde-une-situation-catastrophique-a-new-dehli_4381095.html',
353 'info_dict': {
354 'id': '7d204c9e-a2d3-11eb-9e4c-000d3a23d482',
355 'ext': 'mp4',
ede624d1 356 'title': 'Covid-19 : une situation catastrophique à New Dehli - Édition du mercredi 21 avril 2021',
357 'thumbnail': r're:^https?://.*\.jpe?g$',
bbed5763 358 'duration': 76,
359 'timestamp': 1619028518,
360 'upload_date': '20210421',
361 },
362 'params': {
363 'skip_download': True,
364 },
365 'add_ie': [FranceTVIE.ie_key()],
5c30b268
PH
366 }, {
367 'url': 'http://www.francetvinfo.fr/elections/europeennes/direct-europeennes-regardez-le-debat-entre-les-candidats-a-la-presidence-de-la-commission_600639.html',
99892e99 368 'only_matching': True,
6f96e308
YCH
369 }, {
370 'url': 'http://www.francetvinfo.fr/economie/entreprises/les-entreprises-familiales-le-secret-de-la-reussite_933271.html',
99892e99 371 'only_matching': True,
db264e3c
S
372 }, {
373 'url': 'http://france3-regions.francetvinfo.fr/bretagne/cotes-d-armor/thalassa-echappee-breizh-ce-venredi-dans-les-cotes-d-armor-954961.html',
99892e99 374 'only_matching': True,
ad213a1d
YCH
375 }, {
376 # Dailymotion embed
377 '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',
378 'md5': 'ee7f1828f25a648addc90cb2687b1f12',
379 'info_dict': {
380 'id': 'x4iiko0',
381 'ext': 'mp4',
382 'title': 'NDDL, référendum, Brexit : Cécile Duflot répond à Patrick Cohen',
ede624d1 383 'description': 'md5:fdcb582c370756293a65cdfbc6ecd90e',
ad213a1d 384 'timestamp': 1467011958,
ad213a1d
YCH
385 'uploader': 'France Inter',
386 'uploader_id': 'x2q2ez',
ede624d1 387 'upload_date': '20160627',
388 'view_count': int,
389 'tags': ['Politique', 'France Inter', '27 juin 2016', 'Linvité de 8h20', 'Cécile Duflot', 'Patrick Cohen'],
390 'age_limit': 0,
391 'duration': 640,
392 'like_count': int,
393 'thumbnail': r're:https://[^/?#]+/v/[^/?#]+/x1080',
ad213a1d
YCH
394 },
395 'add_ie': ['Dailymotion'],
30b25d38
S
396 }, {
397 'url': 'http://france3-regions.francetvinfo.fr/limousin/emissions/jt-1213-limousin',
398 'only_matching': True,
41d1cca3 399 }, {
400 # "<figure id=" pattern (#28792)
401 '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',
402 'only_matching': True,
5c30b268 403 }]
648d25d4
JMF
404
405 def _real_extract(self, url):
99892e99
S
406 display_id = self._match_id(url)
407
408 webpage = self._download_webpage(url, display_id)
6f96e308 409
43aebb7d 410 dailymotion_urls = tuple(DailymotionIE._extract_embed_urls(url, webpage))
ad213a1d
YCH
411 if dailymotion_urls:
412 return self.playlist_result([
413 self.url_result(dailymotion_url, DailymotionIE.ie_key())
414 for dailymotion_url in dailymotion_urls])
6f96e308 415
876fed6b 416 video_id = self._search_regex(
417 (r'player\.load[^;]+src:\s*["\']([^"\']+)',
418 r'id-video=([^@]+@[^"]+)',
13c30d1d 419 r'<a[^>]+href="(?:https?:)?//videos\.francetv\.fr/video/([^@]+@[^"]+)"',
41d1cca3 420 r'(?:data-id|<figure[^<]+\bid)=["\']([\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'),
876fed6b 421 webpage, 'video id')
99892e99 422
9749ac7f 423 return self._make_url_result(video_id, url=url)