logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

francetv.py (20534B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_str,
  7. compat_urlparse,
  8. )
  9. from ..utils import (
  10. clean_html,
  11. determine_ext,
  12. ExtractorError,
  13. int_or_none,
  14. parse_duration,
  15. try_get,
  16. url_or_none,
  17. urljoin,
  18. )
  19. from .dailymotion import DailymotionIE
  20. class FranceTVBaseInfoExtractor(InfoExtractor):
  21. def _make_url_result(self, video_or_full_id, catalog=None):
  22. full_id = 'francetv:%s' % video_or_full_id
  23. if '@' not in video_or_full_id and catalog:
  24. full_id += '@%s' % catalog
  25. return self.url_result(
  26. full_id, ie=FranceTVIE.ie_key(),
  27. video_id=video_or_full_id.split('@')[0])
  28. class FranceTVIE(InfoExtractor):
  29. _VALID_URL = r'''(?x)
  30. (?:
  31. https?://
  32. sivideo\.webservices\.francetelevisions\.fr/tools/getInfosOeuvre/v2/\?
  33. .*?\bidDiffusion=[^&]+|
  34. (?:
  35. https?://videos\.francetv\.fr/video/|
  36. francetv:
  37. )
  38. (?P<id>[^@]+)(?:@(?P<catalog>.+))?
  39. )
  40. '''
  41. _TESTS = [{
  42. # without catalog
  43. 'url': 'https://sivideo.webservices.francetelevisions.fr/tools/getInfosOeuvre/v2/?idDiffusion=162311093&callback=_jsonp_loader_callback_request_0',
  44. 'md5': 'c2248a8de38c4e65ea8fae7b5df2d84f',
  45. 'info_dict': {
  46. 'id': '162311093',
  47. 'ext': 'mp4',
  48. 'title': '13h15, le dimanche... - Les mystères de Jésus',
  49. 'description': 'md5:75efe8d4c0a8205e5904498ffe1e1a42',
  50. 'timestamp': 1502623500,
  51. 'upload_date': '20170813',
  52. },
  53. }, {
  54. # with catalog
  55. 'url': 'https://sivideo.webservices.francetelevisions.fr/tools/getInfosOeuvre/v2/?idDiffusion=NI_1004933&catalogue=Zouzous&callback=_jsonp_loader_callback_request_4',
  56. 'only_matching': True,
  57. }, {
  58. 'url': 'http://videos.francetv.fr/video/NI_657393@Regions',
  59. 'only_matching': True,
  60. }, {
  61. 'url': 'francetv:162311093',
  62. 'only_matching': True,
  63. }, {
  64. 'url': 'francetv:NI_1004933@Zouzous',
  65. 'only_matching': True,
  66. }, {
  67. 'url': 'francetv:NI_983319@Info-web',
  68. 'only_matching': True,
  69. }, {
  70. 'url': 'francetv:NI_983319',
  71. 'only_matching': True,
  72. }, {
  73. 'url': 'francetv:NI_657393@Regions',
  74. 'only_matching': True,
  75. }, {
  76. # france-3 live
  77. 'url': 'francetv:SIM_France3',
  78. 'only_matching': True,
  79. }]
  80. def _extract_video(self, video_id, catalogue=None):
  81. # Videos are identified by idDiffusion so catalogue part is optional.
  82. # However when provided, some extra formats may be returned so we pass
  83. # it if available.
  84. info = self._download_json(
  85. 'https://sivideo.webservices.francetelevisions.fr/tools/getInfosOeuvre/v2/',
  86. video_id, 'Downloading video JSON', query={
  87. 'idDiffusion': video_id,
  88. 'catalogue': catalogue or '',
  89. })
  90. if info.get('status') == 'NOK':
  91. raise ExtractorError(
  92. '%s returned error: %s' % (self.IE_NAME, info['message']),
  93. expected=True)
  94. allowed_countries = info['videos'][0].get('geoblocage')
  95. if allowed_countries:
  96. georestricted = True
  97. geo_info = self._download_json(
  98. 'http://geo.francetv.fr/ws/edgescape.json', video_id,
  99. 'Downloading geo restriction info')
  100. country = geo_info['reponse']['geo_info']['country_code']
  101. if country not in allowed_countries:
  102. raise ExtractorError(
  103. 'The video is not available from your location',
  104. expected=True)
  105. else:
  106. georestricted = False
  107. def sign(manifest_url, manifest_id):
  108. for host in ('hdfauthftv-a.akamaihd.net', 'hdfauth.francetv.fr'):
  109. signed_url = url_or_none(self._download_webpage(
  110. 'https://%s/esi/TA' % host, video_id,
  111. 'Downloading signed %s manifest URL' % manifest_id,
  112. fatal=False, query={
  113. 'url': manifest_url,
  114. }))
  115. if signed_url:
  116. return signed_url
  117. return manifest_url
  118. is_live = None
  119. videos = []
  120. for video in (info.get('videos') or []):
  121. if video.get('statut') != 'ONLINE':
  122. continue
  123. if not video.get('url'):
  124. continue
  125. videos.append(video)
  126. if not videos:
  127. for device_type in ['desktop', 'mobile']:
  128. fallback_info = self._download_json(
  129. 'https://player.webservices.francetelevisions.fr/v1/videos/%s' % video_id,
  130. video_id, 'Downloading fallback %s video JSON' % device_type, query={
  131. 'device_type': device_type,
  132. 'browser': 'chrome',
  133. }, fatal=False)
  134. if fallback_info and fallback_info.get('video'):
  135. videos.append(fallback_info['video'])
  136. formats = []
  137. for video in videos:
  138. video_url = video.get('url')
  139. if not video_url:
  140. continue
  141. if is_live is None:
  142. is_live = (try_get(
  143. video, lambda x: x['plages_ouverture'][0]['direct'], bool) is True
  144. or video.get('is_live') is True
  145. or '/live.francetv.fr/' in video_url)
  146. format_id = video.get('format')
  147. ext = determine_ext(video_url)
  148. if ext == 'f4m':
  149. if georestricted:
  150. # See https://github.com/ytdl-org/youtube-dl/issues/3963
  151. # m3u8 urls work fine
  152. continue
  153. formats.extend(self._extract_f4m_formats(
  154. sign(video_url, format_id) + '&hdcore=3.7.0&plugin=aasp-3.7.0.39.44',
  155. video_id, f4m_id=format_id, fatal=False))
  156. elif ext == 'm3u8':
  157. formats.extend(self._extract_m3u8_formats(
  158. sign(video_url, format_id), video_id, 'mp4',
  159. entry_protocol='m3u8_native', m3u8_id=format_id,
  160. fatal=False))
  161. elif ext == 'mpd':
  162. formats.extend(self._extract_mpd_formats(
  163. sign(video_url, format_id), video_id, mpd_id=format_id, fatal=False))
  164. elif video_url.startswith('rtmp'):
  165. formats.append({
  166. 'url': video_url,
  167. 'format_id': 'rtmp-%s' % format_id,
  168. 'ext': 'flv',
  169. })
  170. else:
  171. if self._is_valid_url(video_url, video_id, format_id):
  172. formats.append({
  173. 'url': video_url,
  174. 'format_id': format_id,
  175. })
  176. self._sort_formats(formats)
  177. title = info['titre']
  178. subtitle = info.get('sous_titre')
  179. if subtitle:
  180. title += ' - %s' % subtitle
  181. title = title.strip()
  182. subtitles = {}
  183. subtitles_list = [{
  184. 'url': subformat['url'],
  185. 'ext': subformat.get('format'),
  186. } for subformat in info.get('subtitles', []) if subformat.get('url')]
  187. if subtitles_list:
  188. subtitles['fr'] = subtitles_list
  189. return {
  190. 'id': video_id,
  191. 'title': self._live_title(title) if is_live else title,
  192. 'description': clean_html(info.get('synopsis')),
  193. 'thumbnail': urljoin('https://sivideo.webservices.francetelevisions.fr', info.get('image')),
  194. 'duration': int_or_none(info.get('real_duration')) or parse_duration(info.get('duree')),
  195. 'timestamp': int_or_none(try_get(info, lambda x: x['diffusion']['timestamp'])),
  196. 'is_live': is_live,
  197. 'formats': formats,
  198. 'subtitles': subtitles,
  199. }
  200. def _real_extract(self, url):
  201. mobj = re.match(self._VALID_URL, url)
  202. video_id = mobj.group('id')
  203. catalog = mobj.group('catalog')
  204. if not video_id:
  205. qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
  206. video_id = qs.get('idDiffusion', [None])[0]
  207. catalog = qs.get('catalogue', [None])[0]
  208. if not video_id:
  209. raise ExtractorError('Invalid URL', expected=True)
  210. return self._extract_video(video_id, catalog)
  211. class FranceTVSiteIE(FranceTVBaseInfoExtractor):
  212. _VALID_URL = r'https?://(?:(?:www\.)?france\.tv|mobile\.france\.tv)/(?:[^/]+/)*(?P<id>[^/]+)\.html'
  213. _TESTS = [{
  214. 'url': 'https://www.france.tv/france-2/13h15-le-dimanche/140921-les-mysteres-de-jesus.html',
  215. 'info_dict': {
  216. 'id': 'ec217ecc-0733-48cf-ac06-af1347b849d1',
  217. 'ext': 'mp4',
  218. 'title': '13h15, le dimanche... - Les mystères de Jésus',
  219. 'description': 'md5:75efe8d4c0a8205e5904498ffe1e1a42',
  220. 'timestamp': 1502623500,
  221. 'upload_date': '20170813',
  222. },
  223. 'params': {
  224. 'skip_download': True,
  225. },
  226. 'add_ie': [FranceTVIE.ie_key()],
  227. }, {
  228. # france3
  229. 'url': 'https://www.france.tv/france-3/des-chiffres-et-des-lettres/139063-emission-du-mardi-9-mai-2017.html',
  230. 'only_matching': True,
  231. }, {
  232. # france4
  233. 'url': 'https://www.france.tv/france-4/hero-corp/saison-1/134151-apres-le-calme.html',
  234. 'only_matching': True,
  235. }, {
  236. # france5
  237. 'url': 'https://www.france.tv/france-5/c-a-dire/saison-10/137013-c-a-dire.html',
  238. 'only_matching': True,
  239. }, {
  240. # franceo
  241. 'url': 'https://www.france.tv/france-o/archipels/132249-mon-ancetre-l-esclave.html',
  242. 'only_matching': True,
  243. }, {
  244. # france2 live
  245. 'url': 'https://www.france.tv/france-2/direct.html',
  246. 'only_matching': True,
  247. }, {
  248. 'url': 'https://www.france.tv/documentaires/histoire/136517-argentine-les-500-bebes-voles-de-la-dictature.html',
  249. 'only_matching': True,
  250. }, {
  251. 'url': 'https://www.france.tv/jeux-et-divertissements/divertissements/133965-le-web-contre-attaque.html',
  252. 'only_matching': True,
  253. }, {
  254. 'url': 'https://mobile.france.tv/france-5/c-dans-l-air/137347-emission-du-vendredi-12-mai-2017.html',
  255. 'only_matching': True,
  256. }, {
  257. 'url': 'https://www.france.tv/142749-rouge-sang.html',
  258. 'only_matching': True,
  259. }, {
  260. # france-3 live
  261. 'url': 'https://www.france.tv/france-3/direct.html',
  262. 'only_matching': True,
  263. }]
  264. def _real_extract(self, url):
  265. display_id = self._match_id(url)
  266. webpage = self._download_webpage(url, display_id)
  267. catalogue = None
  268. video_id = self._search_regex(
  269. r'(?:data-main-video\s*=|videoId["\']?\s*[:=])\s*(["\'])(?P<id>(?:(?!\1).)+)\1',
  270. webpage, 'video id', default=None, group='id')
  271. if not video_id:
  272. video_id, catalogue = self._html_search_regex(
  273. r'(?:href=|player\.setVideo\(\s*)"http://videos?\.francetv\.fr/video/([^@]+@[^"]+)"',
  274. webpage, 'video ID').split('@')
  275. return self._make_url_result(video_id, catalogue)
  276. class FranceTVEmbedIE(FranceTVBaseInfoExtractor):
  277. _VALID_URL = r'https?://embed\.francetv\.fr/*\?.*?\bue=(?P<id>[^&]+)'
  278. _TESTS = [{
  279. 'url': 'http://embed.francetv.fr/?ue=7fd581a2ccf59d2fc5719c5c13cf6961',
  280. 'info_dict': {
  281. 'id': 'NI_983319',
  282. 'ext': 'mp4',
  283. 'title': 'Le Pen Reims',
  284. 'upload_date': '20170505',
  285. 'timestamp': 1493981780,
  286. 'duration': 16,
  287. },
  288. 'params': {
  289. 'skip_download': True,
  290. },
  291. 'add_ie': [FranceTVIE.ie_key()],
  292. }]
  293. def _real_extract(self, url):
  294. video_id = self._match_id(url)
  295. video = self._download_json(
  296. 'http://api-embed.webservices.francetelevisions.fr/key/%s' % video_id,
  297. video_id)
  298. return self._make_url_result(video['video_id'], video.get('catalog'))
  299. class FranceTVInfoIE(FranceTVBaseInfoExtractor):
  300. IE_NAME = 'francetvinfo.fr'
  301. _VALID_URL = r'https?://(?:www|mobile|france3-regions)\.francetvinfo\.fr/(?:[^/]+/)*(?P<id>[^/?#&.]+)'
  302. _TESTS = [{
  303. 'url': 'http://www.francetvinfo.fr/replay-jt/france-3/soir-3/jt-grand-soir-3-lundi-26-aout-2013_393427.html',
  304. 'info_dict': {
  305. 'id': '84981923',
  306. 'ext': 'mp4',
  307. 'title': 'Soir 3',
  308. 'upload_date': '20130826',
  309. 'timestamp': 1377548400,
  310. 'subtitles': {
  311. 'fr': 'mincount:2',
  312. },
  313. },
  314. 'params': {
  315. 'skip_download': True,
  316. },
  317. 'add_ie': [FranceTVIE.ie_key()],
  318. }, {
  319. 'url': 'http://www.francetvinfo.fr/elections/europeennes/direct-europeennes-regardez-le-debat-entre-les-candidats-a-la-presidence-de-la-commission_600639.html',
  320. 'only_matching': True,
  321. }, {
  322. 'url': 'http://www.francetvinfo.fr/economie/entreprises/les-entreprises-familiales-le-secret-de-la-reussite_933271.html',
  323. 'only_matching': True,
  324. }, {
  325. 'url': 'http://france3-regions.francetvinfo.fr/bretagne/cotes-d-armor/thalassa-echappee-breizh-ce-venredi-dans-les-cotes-d-armor-954961.html',
  326. 'only_matching': True,
  327. }, {
  328. # Dailymotion embed
  329. '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',
  330. 'md5': 'ee7f1828f25a648addc90cb2687b1f12',
  331. 'info_dict': {
  332. 'id': 'x4iiko0',
  333. 'ext': 'mp4',
  334. 'title': 'NDDL, référendum, Brexit : Cécile Duflot répond à Patrick Cohen',
  335. 'description': 'Au lendemain de la victoire du "oui" au référendum sur l\'aéroport de Notre-Dame-des-Landes, l\'ancienne ministre écologiste est l\'invitée de Patrick Cohen. Plus d\'info : https://www.franceinter.fr/emissions/le-7-9/le-7-9-27-juin-2016',
  336. 'timestamp': 1467011958,
  337. 'upload_date': '20160627',
  338. 'uploader': 'France Inter',
  339. 'uploader_id': 'x2q2ez',
  340. },
  341. 'add_ie': ['Dailymotion'],
  342. }, {
  343. 'url': 'http://france3-regions.francetvinfo.fr/limousin/emissions/jt-1213-limousin',
  344. 'only_matching': True,
  345. }, {
  346. # "<figure id=" pattern (#28792)
  347. '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',
  348. 'only_matching': True,
  349. }]
  350. def _real_extract(self, url):
  351. display_id = self._match_id(url)
  352. webpage = self._download_webpage(url, display_id)
  353. dailymotion_urls = DailymotionIE._extract_urls(webpage)
  354. if dailymotion_urls:
  355. return self.playlist_result([
  356. self.url_result(dailymotion_url, DailymotionIE.ie_key())
  357. for dailymotion_url in dailymotion_urls])
  358. video_id = self._search_regex(
  359. (r'player\.load[^;]+src:\s*["\']([^"\']+)',
  360. r'id-video=([^@]+@[^"]+)',
  361. r'<a[^>]+href="(?:https?:)?//videos\.francetv\.fr/video/([^@]+@[^"]+)"',
  362. r'(?:data-id|<figure[^<]+\bid)=["\']([\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'),
  363. webpage, 'video id')
  364. return self._make_url_result(video_id)
  365. class FranceTVInfoSportIE(FranceTVBaseInfoExtractor):
  366. IE_NAME = 'sport.francetvinfo.fr'
  367. _VALID_URL = r'https?://sport\.francetvinfo\.fr/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  368. _TESTS = [{
  369. 'url': 'https://sport.francetvinfo.fr/les-jeux-olympiques/retour-sur-les-meilleurs-moments-de-pyeongchang-2018',
  370. 'info_dict': {
  371. 'id': '6e49080e-3f45-11e8-b459-000d3a2439ea',
  372. 'ext': 'mp4',
  373. 'title': 'Retour sur les meilleurs moments de Pyeongchang 2018',
  374. 'timestamp': 1523639962,
  375. 'upload_date': '20180413',
  376. },
  377. 'params': {
  378. 'skip_download': True,
  379. },
  380. 'add_ie': [FranceTVIE.ie_key()],
  381. }]
  382. def _real_extract(self, url):
  383. display_id = self._match_id(url)
  384. webpage = self._download_webpage(url, display_id)
  385. video_id = self._search_regex(r'data-video="([^"]+)"', webpage, 'video_id')
  386. return self._make_url_result(video_id, 'Sport-web')
  387. class GenerationWhatIE(InfoExtractor):
  388. IE_NAME = 'france2.fr:generation-what'
  389. _VALID_URL = r'https?://generation-what\.francetv\.fr/[^/]+/video/(?P<id>[^/?#&]+)'
  390. _TESTS = [{
  391. 'url': 'http://generation-what.francetv.fr/portrait/video/present-arms',
  392. 'info_dict': {
  393. 'id': 'wtvKYUG45iw',
  394. 'ext': 'mp4',
  395. 'title': 'Generation What - Garde à vous - FRA',
  396. 'uploader': 'Generation What',
  397. 'uploader_id': 'UCHH9p1eetWCgt4kXBYCb3_w',
  398. 'upload_date': '20160411',
  399. },
  400. 'params': {
  401. 'skip_download': True,
  402. },
  403. 'add_ie': ['Youtube'],
  404. }, {
  405. 'url': 'http://generation-what.francetv.fr/europe/video/present-arms',
  406. 'only_matching': True,
  407. }]
  408. def _real_extract(self, url):
  409. display_id = self._match_id(url)
  410. webpage = self._download_webpage(url, display_id)
  411. youtube_id = self._search_regex(
  412. r"window\.videoURL\s*=\s*'([0-9A-Za-z_-]{11})';",
  413. webpage, 'youtube id')
  414. return self.url_result(youtube_id, ie='Youtube', video_id=youtube_id)
  415. class CultureboxIE(FranceTVBaseInfoExtractor):
  416. _VALID_URL = r'https?://(?:m\.)?culturebox\.francetvinfo\.fr/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  417. _TESTS = [{
  418. 'url': 'https://culturebox.francetvinfo.fr/opera-classique/musique-classique/c-est-baroque/concerts/cantates-bwv-4-106-et-131-de-bach-par-raphael-pichon-57-268689',
  419. 'info_dict': {
  420. 'id': 'EV_134885',
  421. 'ext': 'mp4',
  422. 'title': 'Cantates BWV 4, 106 et 131 de Bach par Raphaël Pichon 5/7',
  423. 'description': 'md5:19c44af004b88219f4daa50fa9a351d4',
  424. 'upload_date': '20180206',
  425. 'timestamp': 1517945220,
  426. 'duration': 5981,
  427. },
  428. 'params': {
  429. 'skip_download': True,
  430. },
  431. 'add_ie': [FranceTVIE.ie_key()],
  432. }]
  433. def _real_extract(self, url):
  434. display_id = self._match_id(url)
  435. webpage = self._download_webpage(url, display_id)
  436. if ">Ce live n'est plus disponible en replay<" in webpage:
  437. raise ExtractorError(
  438. 'Video %s is not available' % display_id, expected=True)
  439. video_id, catalogue = self._search_regex(
  440. r'["\'>]https?://videos\.francetv\.fr/video/([^@]+@.+?)["\'<]',
  441. webpage, 'video id').split('@')
  442. return self._make_url_result(video_id, catalogue)
  443. class FranceTVJeunesseIE(FranceTVBaseInfoExtractor):
  444. _VALID_URL = r'(?P<url>https?://(?:www\.)?(?:zouzous|ludo)\.fr/heros/(?P<id>[^/?#&]+))'
  445. _TESTS = [{
  446. 'url': 'https://www.zouzous.fr/heros/simon',
  447. 'info_dict': {
  448. 'id': 'simon',
  449. },
  450. 'playlist_count': 9,
  451. }, {
  452. 'url': 'https://www.ludo.fr/heros/ninjago',
  453. 'info_dict': {
  454. 'id': 'ninjago',
  455. },
  456. 'playlist_count': 10,
  457. }, {
  458. 'url': 'https://www.zouzous.fr/heros/simon?abc',
  459. 'only_matching': True,
  460. }]
  461. def _real_extract(self, url):
  462. mobj = re.match(self._VALID_URL, url)
  463. playlist_id = mobj.group('id')
  464. playlist = self._download_json(
  465. '%s/%s' % (mobj.group('url'), 'playlist'), playlist_id)
  466. if not playlist.get('count'):
  467. raise ExtractorError(
  468. '%s is not available' % playlist_id, expected=True)
  469. entries = []
  470. for item in playlist['items']:
  471. identity = item.get('identity')
  472. if identity and isinstance(identity, compat_str):
  473. entries.append(self._make_url_result(identity))
  474. return self.playlist_result(entries, playlist_id)