francetv.py 18 KB

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