francetv.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. def _real_extract(self, url):
  347. display_id = self._match_id(url)
  348. webpage = self._download_webpage(url, display_id)
  349. dailymotion_urls = DailymotionIE._extract_urls(webpage)
  350. if dailymotion_urls:
  351. return self.playlist_result([
  352. self.url_result(dailymotion_url, DailymotionIE.ie_key())
  353. for dailymotion_url in dailymotion_urls])
  354. video_id = self._search_regex(
  355. (r'player\.load[^;]+src:\s*["\']([^"\']+)',
  356. r'id-video=([^@]+@[^"]+)',
  357. r'<a[^>]+href="(?:https?:)?//videos\.francetv\.fr/video/([^@]+@[^"]+)"',
  358. r'data-id=["\']([\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'),
  359. webpage, 'video id')
  360. return self._make_url_result(video_id)
  361. class FranceTVInfoSportIE(FranceTVBaseInfoExtractor):
  362. IE_NAME = 'sport.francetvinfo.fr'
  363. _VALID_URL = r'https?://sport\.francetvinfo\.fr/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  364. _TESTS = [{
  365. 'url': 'https://sport.francetvinfo.fr/les-jeux-olympiques/retour-sur-les-meilleurs-moments-de-pyeongchang-2018',
  366. 'info_dict': {
  367. 'id': '6e49080e-3f45-11e8-b459-000d3a2439ea',
  368. 'ext': 'mp4',
  369. 'title': 'Retour sur les meilleurs moments de Pyeongchang 2018',
  370. 'timestamp': 1523639962,
  371. 'upload_date': '20180413',
  372. },
  373. 'params': {
  374. 'skip_download': True,
  375. },
  376. 'add_ie': [FranceTVIE.ie_key()],
  377. }]
  378. def _real_extract(self, url):
  379. display_id = self._match_id(url)
  380. webpage = self._download_webpage(url, display_id)
  381. video_id = self._search_regex(r'data-video="([^"]+)"', webpage, 'video_id')
  382. return self._make_url_result(video_id, 'Sport-web')
  383. class GenerationWhatIE(InfoExtractor):
  384. IE_NAME = 'france2.fr:generation-what'
  385. _VALID_URL = r'https?://generation-what\.francetv\.fr/[^/]+/video/(?P<id>[^/?#&]+)'
  386. _TESTS = [{
  387. 'url': 'http://generation-what.francetv.fr/portrait/video/present-arms',
  388. 'info_dict': {
  389. 'id': 'wtvKYUG45iw',
  390. 'ext': 'mp4',
  391. 'title': 'Generation What - Garde à vous - FRA',
  392. 'uploader': 'Generation What',
  393. 'uploader_id': 'UCHH9p1eetWCgt4kXBYCb3_w',
  394. 'upload_date': '20160411',
  395. },
  396. 'params': {
  397. 'skip_download': True,
  398. },
  399. 'add_ie': ['Youtube'],
  400. }, {
  401. 'url': 'http://generation-what.francetv.fr/europe/video/present-arms',
  402. 'only_matching': True,
  403. }]
  404. def _real_extract(self, url):
  405. display_id = self._match_id(url)
  406. webpage = self._download_webpage(url, display_id)
  407. youtube_id = self._search_regex(
  408. r"window\.videoURL\s*=\s*'([0-9A-Za-z_-]{11})';",
  409. webpage, 'youtube id')
  410. return self.url_result(youtube_id, ie='Youtube', video_id=youtube_id)
  411. class CultureboxIE(FranceTVBaseInfoExtractor):
  412. _VALID_URL = r'https?://(?:m\.)?culturebox\.francetvinfo\.fr/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  413. _TESTS = [{
  414. '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',
  415. 'info_dict': {
  416. 'id': 'EV_134885',
  417. 'ext': 'mp4',
  418. 'title': 'Cantates BWV 4, 106 et 131 de Bach par Raphaël Pichon 5/7',
  419. 'description': 'md5:19c44af004b88219f4daa50fa9a351d4',
  420. 'upload_date': '20180206',
  421. 'timestamp': 1517945220,
  422. 'duration': 5981,
  423. },
  424. 'params': {
  425. 'skip_download': True,
  426. },
  427. 'add_ie': [FranceTVIE.ie_key()],
  428. }]
  429. def _real_extract(self, url):
  430. display_id = self._match_id(url)
  431. webpage = self._download_webpage(url, display_id)
  432. if ">Ce live n'est plus disponible en replay<" in webpage:
  433. raise ExtractorError(
  434. 'Video %s is not available' % display_id, expected=True)
  435. video_id, catalogue = self._search_regex(
  436. r'["\'>]https?://videos\.francetv\.fr/video/([^@]+@.+?)["\'<]',
  437. webpage, 'video id').split('@')
  438. return self._make_url_result(video_id, catalogue)
  439. class FranceTVJeunesseIE(FranceTVBaseInfoExtractor):
  440. _VALID_URL = r'(?P<url>https?://(?:www\.)?(?:zouzous|ludo)\.fr/heros/(?P<id>[^/?#&]+))'
  441. _TESTS = [{
  442. 'url': 'https://www.zouzous.fr/heros/simon',
  443. 'info_dict': {
  444. 'id': 'simon',
  445. },
  446. 'playlist_count': 9,
  447. }, {
  448. 'url': 'https://www.ludo.fr/heros/ninjago',
  449. 'info_dict': {
  450. 'id': 'ninjago',
  451. },
  452. 'playlist_count': 10,
  453. }, {
  454. 'url': 'https://www.zouzous.fr/heros/simon?abc',
  455. 'only_matching': True,
  456. }]
  457. def _real_extract(self, url):
  458. mobj = re.match(self._VALID_URL, url)
  459. playlist_id = mobj.group('id')
  460. playlist = self._download_json(
  461. '%s/%s' % (mobj.group('url'), 'playlist'), playlist_id)
  462. if not playlist.get('count'):
  463. raise ExtractorError(
  464. '%s is not available' % playlist_id, expected=True)
  465. entries = []
  466. for item in playlist['items']:
  467. identity = item.get('identity')
  468. if identity and isinstance(identity, compat_str):
  469. entries.append(self._make_url_result(identity))
  470. return self.playlist_result(entries, playlist_id)