francetv.py 17 KB

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