francetv.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 _extract_video(self, video_id, catalogue=None):
  19. info = self._download_json(
  20. 'https://sivideo.webservices.francetelevisions.fr/tools/getInfosOeuvre/v2/',
  21. video_id, 'Downloading video JSON', query={
  22. 'idDiffusion': video_id,
  23. 'catalogue': catalogue or '',
  24. })
  25. if info.get('status') == 'NOK':
  26. raise ExtractorError(
  27. '%s returned error: %s' % (self.IE_NAME, info['message']),
  28. expected=True)
  29. allowed_countries = info['videos'][0].get('geoblocage')
  30. if allowed_countries:
  31. georestricted = True
  32. geo_info = self._download_json(
  33. 'http://geo.francetv.fr/ws/edgescape.json', video_id,
  34. 'Downloading geo restriction info')
  35. country = geo_info['reponse']['geo_info']['country_code']
  36. if country not in allowed_countries:
  37. raise ExtractorError(
  38. 'The video is not available from your location',
  39. expected=True)
  40. else:
  41. georestricted = False
  42. def sign(manifest_url, manifest_id):
  43. for host in ('hdfauthftv-a.akamaihd.net', 'hdfauth.francetv.fr'):
  44. signed_url = self._download_webpage(
  45. 'https://%s/esi/TA' % host, video_id,
  46. 'Downloading signed %s manifest URL' % manifest_id,
  47. fatal=False, query={
  48. 'url': manifest_url,
  49. })
  50. if (signed_url and isinstance(signed_url, compat_str) and
  51. re.search(r'^(?:https?:)?//', signed_url)):
  52. return signed_url
  53. return manifest_url
  54. formats = []
  55. for video in info['videos']:
  56. if video['statut'] != 'ONLINE':
  57. continue
  58. video_url = video['url']
  59. if not video_url:
  60. continue
  61. format_id = video['format']
  62. ext = determine_ext(video_url)
  63. if ext == 'f4m':
  64. if georestricted:
  65. # See https://github.com/rg3/youtube-dl/issues/3963
  66. # m3u8 urls work fine
  67. continue
  68. formats.extend(self._extract_f4m_formats(
  69. sign(video_url, format_id) + '&hdcore=3.7.0&plugin=aasp-3.7.0.39.44',
  70. video_id, f4m_id=format_id, fatal=False))
  71. elif ext == 'm3u8':
  72. formats.extend(self._extract_m3u8_formats(
  73. sign(video_url, format_id), video_id, 'mp4',
  74. entry_protocol='m3u8_native', m3u8_id=format_id,
  75. fatal=False))
  76. elif video_url.startswith('rtmp'):
  77. formats.append({
  78. 'url': video_url,
  79. 'format_id': 'rtmp-%s' % format_id,
  80. 'ext': 'flv',
  81. })
  82. else:
  83. if self._is_valid_url(video_url, video_id, format_id):
  84. formats.append({
  85. 'url': video_url,
  86. 'format_id': format_id,
  87. })
  88. self._sort_formats(formats)
  89. title = info['titre']
  90. subtitle = info.get('sous_titre')
  91. if subtitle:
  92. title += ' - %s' % subtitle
  93. title = title.strip()
  94. subtitles = {}
  95. subtitles_list = [{
  96. 'url': subformat['url'],
  97. 'ext': subformat.get('format'),
  98. } for subformat in info.get('subtitles', []) if subformat.get('url')]
  99. if subtitles_list:
  100. subtitles['fr'] = subtitles_list
  101. return {
  102. 'id': video_id,
  103. 'title': title,
  104. 'description': clean_html(info['synopsis']),
  105. 'thumbnail': compat_urlparse.urljoin('http://pluzz.francetv.fr', info['image']),
  106. 'duration': int_or_none(info.get('real_duration')) or parse_duration(info['duree']),
  107. 'timestamp': int_or_none(info['diffusion']['timestamp']),
  108. 'formats': formats,
  109. 'subtitles': subtitles,
  110. }
  111. class FranceTVIE(FranceTVBaseInfoExtractor):
  112. _VALID_URL = r'https?://(?:(?:www\.)?france\.tv|mobile\.france\.tv)/(?:[^/]+/)*(?P<id>[^/]+)\.html'
  113. _TESTS = [{
  114. 'url': 'https://www.france.tv/france-2/13h15-le-dimanche/140921-les-mysteres-de-jesus.html',
  115. 'info_dict': {
  116. 'id': '157550144',
  117. 'ext': 'mp4',
  118. 'title': '13h15, le dimanche... - Les mystères de Jésus',
  119. 'description': 'md5:75efe8d4c0a8205e5904498ffe1e1a42',
  120. 'timestamp': 1494156300,
  121. 'upload_date': '20170507',
  122. },
  123. 'params': {
  124. # m3u8 downloads
  125. 'skip_download': True,
  126. },
  127. }, {
  128. # france3
  129. 'url': 'https://www.france.tv/france-3/des-chiffres-et-des-lettres/139063-emission-du-mardi-9-mai-2017.html',
  130. 'only_matching': True,
  131. }, {
  132. # france4
  133. 'url': 'https://www.france.tv/france-4/hero-corp/saison-1/134151-apres-le-calme.html',
  134. 'only_matching': True,
  135. }, {
  136. # france5
  137. 'url': 'https://www.france.tv/france-5/c-a-dire/saison-10/137013-c-a-dire.html',
  138. 'only_matching': True,
  139. }, {
  140. # franceo
  141. 'url': 'https://www.france.tv/france-o/archipels/132249-mon-ancetre-l-esclave.html',
  142. 'only_matching': True,
  143. }, {
  144. # france2 live
  145. 'url': 'https://www.france.tv/france-2/direct.html',
  146. 'only_matching': True,
  147. }, {
  148. 'url': 'https://www.france.tv/documentaires/histoire/136517-argentine-les-500-bebes-voles-de-la-dictature.html',
  149. 'only_matching': True,
  150. }, {
  151. 'url': 'https://www.france.tv/jeux-et-divertissements/divertissements/133965-le-web-contre-attaque.html',
  152. 'only_matching': True,
  153. }, {
  154. 'url': 'https://mobile.france.tv/france-5/c-dans-l-air/137347-emission-du-vendredi-12-mai-2017.html',
  155. 'only_matching': True,
  156. }, {
  157. 'url': 'https://www.france.tv/142749-rouge-sang.html',
  158. 'only_matching': True,
  159. }]
  160. def _real_extract(self, url):
  161. display_id = self._match_id(url)
  162. webpage = self._download_webpage(url, display_id)
  163. catalogue = None
  164. video_id = self._search_regex(
  165. r'data-main-video=(["\'])(?P<id>(?:(?!\1).)+)\1',
  166. webpage, 'video id', default=None, group='id')
  167. if not video_id:
  168. video_id, catalogue = self._html_search_regex(
  169. r'(?:href=|player\.setVideo\(\s*)"http://videos?\.francetv\.fr/video/([^@]+@[^"]+)"',
  170. webpage, 'video ID').split('@')
  171. return self._extract_video(video_id, catalogue)
  172. class FranceTVEmbedIE(FranceTVBaseInfoExtractor):
  173. _VALID_URL = r'https?://embed\.francetv\.fr/*\?.*?\bue=(?P<id>[^&]+)'
  174. _TEST = {
  175. 'url': 'http://embed.francetv.fr/?ue=7fd581a2ccf59d2fc5719c5c13cf6961',
  176. 'info_dict': {
  177. 'id': 'NI_983319',
  178. 'ext': 'mp4',
  179. 'title': 'Le Pen Reims',
  180. 'upload_date': '20170505',
  181. 'timestamp': 1493981780,
  182. 'duration': 16,
  183. },
  184. }
  185. def _real_extract(self, url):
  186. video_id = self._match_id(url)
  187. video = self._download_json(
  188. 'http://api-embed.webservices.francetelevisions.fr/key/%s' % video_id,
  189. video_id)
  190. return self._extract_video(video['video_id'], video.get('catalog'))
  191. class FranceTVInfoIE(FranceTVBaseInfoExtractor):
  192. IE_NAME = 'francetvinfo.fr'
  193. _VALID_URL = r'https?://(?:www|mobile|france3-regions)\.francetvinfo\.fr/(?:[^/]+/)*(?P<title>[^/?#&.]+)'
  194. _TESTS = [{
  195. 'url': 'http://www.francetvinfo.fr/replay-jt/france-3/soir-3/jt-grand-soir-3-lundi-26-aout-2013_393427.html',
  196. 'info_dict': {
  197. 'id': '84981923',
  198. 'ext': 'mp4',
  199. 'title': 'Soir 3',
  200. 'upload_date': '20130826',
  201. 'timestamp': 1377548400,
  202. 'subtitles': {
  203. 'fr': 'mincount:2',
  204. },
  205. },
  206. 'params': {
  207. # m3u8 downloads
  208. 'skip_download': True,
  209. },
  210. }, {
  211. 'url': 'http://www.francetvinfo.fr/elections/europeennes/direct-europeennes-regardez-le-debat-entre-les-candidats-a-la-presidence-de-la-commission_600639.html',
  212. 'info_dict': {
  213. 'id': 'EV_20019',
  214. 'ext': 'mp4',
  215. 'title': 'Débat des candidats à la Commission européenne',
  216. 'description': 'Débat des candidats à la Commission européenne',
  217. },
  218. 'params': {
  219. 'skip_download': 'HLS (reqires ffmpeg)'
  220. },
  221. 'skip': 'Ce direct est terminé et sera disponible en rattrapage dans quelques minutes.',
  222. }, {
  223. 'url': 'http://www.francetvinfo.fr/economie/entreprises/les-entreprises-familiales-le-secret-de-la-reussite_933271.html',
  224. 'md5': 'f485bda6e185e7d15dbc69b72bae993e',
  225. 'info_dict': {
  226. 'id': 'NI_173343',
  227. 'ext': 'mp4',
  228. 'title': 'Les entreprises familiales : le secret de la réussite',
  229. 'thumbnail': r're:^https?://.*\.jpe?g$',
  230. 'timestamp': 1433273139,
  231. 'upload_date': '20150602',
  232. },
  233. 'params': {
  234. # m3u8 downloads
  235. 'skip_download': True,
  236. },
  237. }, {
  238. 'url': 'http://france3-regions.francetvinfo.fr/bretagne/cotes-d-armor/thalassa-echappee-breizh-ce-venredi-dans-les-cotes-d-armor-954961.html',
  239. 'md5': 'f485bda6e185e7d15dbc69b72bae993e',
  240. 'info_dict': {
  241. 'id': 'NI_657393',
  242. 'ext': 'mp4',
  243. 'title': 'Olivier Monthus, réalisateur de "Bretagne, le choix de l’Armor"',
  244. 'description': 'md5:a3264114c9d29aeca11ced113c37b16c',
  245. 'thumbnail': r're:^https?://.*\.jpe?g$',
  246. 'timestamp': 1458300695,
  247. 'upload_date': '20160318',
  248. },
  249. 'params': {
  250. 'skip_download': True,
  251. },
  252. }, {
  253. # Dailymotion embed
  254. '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',
  255. 'md5': 'ee7f1828f25a648addc90cb2687b1f12',
  256. 'info_dict': {
  257. 'id': 'x4iiko0',
  258. 'ext': 'mp4',
  259. 'title': 'NDDL, référendum, Brexit : Cécile Duflot répond à Patrick Cohen',
  260. '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',
  261. 'timestamp': 1467011958,
  262. 'upload_date': '20160627',
  263. 'uploader': 'France Inter',
  264. 'uploader_id': 'x2q2ez',
  265. },
  266. 'add_ie': ['Dailymotion'],
  267. }, {
  268. 'url': 'http://france3-regions.francetvinfo.fr/limousin/emissions/jt-1213-limousin',
  269. 'only_matching': True,
  270. }]
  271. def _real_extract(self, url):
  272. mobj = re.match(self._VALID_URL, url)
  273. page_title = mobj.group('title')
  274. webpage = self._download_webpage(url, page_title)
  275. dailymotion_urls = DailymotionIE._extract_urls(webpage)
  276. if dailymotion_urls:
  277. return self.playlist_result([
  278. self.url_result(dailymotion_url, DailymotionIE.ie_key())
  279. for dailymotion_url in dailymotion_urls])
  280. video_id, catalogue = self._search_regex(
  281. (r'id-video=([^@]+@[^"]+)',
  282. r'<a[^>]+href="(?:https?:)?//videos\.francetv\.fr/video/([^@]+@[^"]+)"'),
  283. webpage, 'video id').split('@')
  284. return self._extract_video(video_id, catalogue)
  285. class GenerationWhatIE(InfoExtractor):
  286. IE_NAME = 'france2.fr:generation-what'
  287. _VALID_URL = r'https?://generation-what\.francetv\.fr/[^/]+/video/(?P<id>[^/?#]+)'
  288. _TESTS = [{
  289. 'url': 'http://generation-what.francetv.fr/portrait/video/present-arms',
  290. 'info_dict': {
  291. 'id': 'wtvKYUG45iw',
  292. 'ext': 'mp4',
  293. 'title': 'Generation What - Garde à vous - FRA',
  294. 'uploader': 'Generation What',
  295. 'uploader_id': 'UCHH9p1eetWCgt4kXBYCb3_w',
  296. 'upload_date': '20160411',
  297. },
  298. }, {
  299. 'url': 'http://generation-what.francetv.fr/europe/video/present-arms',
  300. 'only_matching': True,
  301. }]
  302. def _real_extract(self, url):
  303. display_id = self._match_id(url)
  304. webpage = self._download_webpage(url, display_id)
  305. youtube_id = self._search_regex(
  306. r"window\.videoURL\s*=\s*'([0-9A-Za-z_-]{11})';",
  307. webpage, 'youtube id')
  308. return self.url_result(youtube_id, 'Youtube', youtube_id)
  309. class CultureboxIE(FranceTVBaseInfoExtractor):
  310. IE_NAME = 'culturebox.francetvinfo.fr'
  311. _VALID_URL = r'https?://(?:m\.)?culturebox\.francetvinfo\.fr/(?P<name>.*?)(\?|$)'
  312. _TEST = {
  313. 'url': 'http://culturebox.francetvinfo.fr/live/musique/musique-classique/le-livre-vermeil-de-montserrat-a-la-cathedrale-delne-214511',
  314. 'md5': '9b88dc156781c4dbebd4c3e066e0b1d6',
  315. 'info_dict': {
  316. 'id': 'EV_50111',
  317. 'ext': 'flv',
  318. 'title': "Le Livre Vermeil de Montserrat à la Cathédrale d'Elne",
  319. 'description': 'md5:f8a4ad202e8fe533e2c493cc12e739d9',
  320. 'upload_date': '20150320',
  321. 'timestamp': 1426892400,
  322. 'duration': 2760.9,
  323. },
  324. }
  325. def _real_extract(self, url):
  326. mobj = re.match(self._VALID_URL, url)
  327. name = mobj.group('name')
  328. webpage = self._download_webpage(url, name)
  329. if ">Ce live n'est plus disponible en replay<" in webpage:
  330. raise ExtractorError('Video %s is not available' % name, expected=True)
  331. video_id, catalogue = self._search_regex(
  332. r'["\'>]https?://videos\.francetv\.fr/video/([^@]+@.+?)["\'<]',
  333. webpage, 'video id').split('@')
  334. return self._extract_video(video_id, catalogue)