You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

473 lines
19 KiB

12 years ago
10 years ago
12 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_parse_qs,
  7. compat_str,
  8. compat_urllib_parse_urlparse,
  9. )
  10. from ..utils import (
  11. ExtractorError,
  12. find_xpath_attr,
  13. get_element_by_attribute,
  14. int_or_none,
  15. NO_DEFAULT,
  16. qualities,
  17. try_get,
  18. unified_strdate,
  19. )
  20. # There are different sources of video in arte.tv, the extraction process
  21. # is different for each one. The videos usually expire in 7 days, so we can't
  22. # add tests.
  23. class ArteTvIE(InfoExtractor):
  24. _VALID_URL = r'https?://videos\.arte\.tv/(?P<lang>fr|de|en|es)/.*-(?P<id>.*?)\.html'
  25. IE_NAME = 'arte.tv'
  26. def _real_extract(self, url):
  27. mobj = re.match(self._VALID_URL, url)
  28. lang = mobj.group('lang')
  29. video_id = mobj.group('id')
  30. ref_xml_url = url.replace('/videos/', '/do_delegate/videos/')
  31. ref_xml_url = ref_xml_url.replace('.html', ',view,asPlayerXml.xml')
  32. ref_xml_doc = self._download_xml(
  33. ref_xml_url, video_id, note='Downloading metadata')
  34. config_node = find_xpath_attr(ref_xml_doc, './/video', 'lang', lang)
  35. config_xml_url = config_node.attrib['ref']
  36. config = self._download_xml(
  37. config_xml_url, video_id, note='Downloading configuration')
  38. formats = [{
  39. 'format_id': q.attrib['quality'],
  40. # The playpath starts at 'mp4:', if we don't manually
  41. # split the url, rtmpdump will incorrectly parse them
  42. 'url': q.text.split('mp4:', 1)[0],
  43. 'play_path': 'mp4:' + q.text.split('mp4:', 1)[1],
  44. 'ext': 'flv',
  45. 'quality': 2 if q.attrib['quality'] == 'hd' else 1,
  46. } for q in config.findall('./urls/url')]
  47. self._sort_formats(formats)
  48. title = config.find('.//name').text
  49. thumbnail = config.find('.//firstThumbnailUrl').text
  50. return {
  51. 'id': video_id,
  52. 'title': title,
  53. 'thumbnail': thumbnail,
  54. 'formats': formats,
  55. }
  56. class ArteTVBaseIE(InfoExtractor):
  57. @classmethod
  58. def _extract_url_info(cls, url):
  59. mobj = re.match(cls._VALID_URL, url)
  60. lang = mobj.group('lang')
  61. query = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
  62. if 'vid' in query:
  63. video_id = query['vid'][0]
  64. else:
  65. # This is not a real id, it can be for example AJT for the news
  66. # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
  67. video_id = mobj.group('id')
  68. return video_id, lang
  69. def _extract_from_json_url(self, json_url, video_id, lang, title=None):
  70. info = self._download_json(json_url, video_id)
  71. player_info = info['videoJsonPlayer']
  72. vsr = try_get(player_info, lambda x: x['VSR'], dict)
  73. if not vsr:
  74. error = None
  75. if try_get(player_info, lambda x: x['custom_msg']['type']) == 'error':
  76. error = try_get(
  77. player_info, lambda x: x['custom_msg']['msg'], compat_str)
  78. if not error:
  79. error = 'Video %s is not available' % player_info.get('VID') or video_id
  80. raise ExtractorError(error, expected=True)
  81. upload_date_str = player_info.get('shootingDate')
  82. if not upload_date_str:
  83. upload_date_str = (player_info.get('VRA') or player_info.get('VDA') or '').split(' ')[0]
  84. title = (player_info.get('VTI') or title or player_info['VID']).strip()
  85. subtitle = player_info.get('VSU', '').strip()
  86. if subtitle:
  87. title += ' - %s' % subtitle
  88. info_dict = {
  89. 'id': player_info['VID'],
  90. 'title': title,
  91. 'description': player_info.get('VDE'),
  92. 'upload_date': unified_strdate(upload_date_str),
  93. 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
  94. }
  95. qfunc = qualities(['HQ', 'MQ', 'EQ', 'SQ'])
  96. LANGS = {
  97. 'fr': 'F',
  98. 'de': 'A',
  99. 'en': 'E[ANG]',
  100. 'es': 'E[ESP]',
  101. }
  102. langcode = LANGS.get(lang, lang)
  103. formats = []
  104. for format_id, format_dict in vsr.items():
  105. f = dict(format_dict)
  106. versionCode = f.get('versionCode')
  107. l = re.escape(langcode)
  108. # Language preference from most to least priority
  109. # Reference: section 5.6.3 of
  110. # http://www.arte.tv/sites/en/corporate/files/complete-technical-guidelines-arte-geie-v1-05.pdf
  111. PREFERENCES = (
  112. # original version in requested language, without subtitles
  113. r'VO{0}$'.format(l),
  114. # original version in requested language, with partial subtitles in requested language
  115. r'VO{0}-ST{0}$'.format(l),
  116. # original version in requested language, with subtitles for the deaf and hard-of-hearing in requested language
  117. r'VO{0}-STM{0}$'.format(l),
  118. # non-original (dubbed) version in requested language, without subtitles
  119. r'V{0}$'.format(l),
  120. # non-original (dubbed) version in requested language, with subtitles partial subtitles in requested language
  121. r'V{0}-ST{0}$'.format(l),
  122. # non-original (dubbed) version in requested language, with subtitles for the deaf and hard-of-hearing in requested language
  123. r'V{0}-STM{0}$'.format(l),
  124. # original version in requested language, with partial subtitles in different language
  125. r'VO{0}-ST(?!{0}).+?$'.format(l),
  126. # original version in requested language, with subtitles for the deaf and hard-of-hearing in different language
  127. r'VO{0}-STM(?!{0}).+?$'.format(l),
  128. # original version in different language, with partial subtitles in requested language
  129. r'VO(?:(?!{0}).+?)?-ST{0}$'.format(l),
  130. # original version in different language, with subtitles for the deaf and hard-of-hearing in requested language
  131. r'VO(?:(?!{0}).+?)?-STM{0}$'.format(l),
  132. # original version in different language, without subtitles
  133. r'VO(?:(?!{0}))?$'.format(l),
  134. # original version in different language, with partial subtitles in different language
  135. r'VO(?:(?!{0}).+?)?-ST(?!{0}).+?$'.format(l),
  136. # original version in different language, with subtitles for the deaf and hard-of-hearing in different language
  137. r'VO(?:(?!{0}).+?)?-STM(?!{0}).+?$'.format(l),
  138. )
  139. for pref, p in enumerate(PREFERENCES):
  140. if re.match(p, versionCode):
  141. lang_pref = len(PREFERENCES) - pref
  142. break
  143. else:
  144. lang_pref = -1
  145. format = {
  146. 'format_id': format_id,
  147. 'preference': -10 if f.get('videoFormat') == 'M3U8' else None,
  148. 'language_preference': lang_pref,
  149. 'format_note': '%s, %s' % (f.get('versionCode'), f.get('versionLibelle')),
  150. 'width': int_or_none(f.get('width')),
  151. 'height': int_or_none(f.get('height')),
  152. 'tbr': int_or_none(f.get('bitrate')),
  153. 'quality': qfunc(f.get('quality')),
  154. }
  155. if f.get('mediaType') == 'rtmp':
  156. format['url'] = f['streamer']
  157. format['play_path'] = 'mp4:' + f['url']
  158. format['ext'] = 'flv'
  159. else:
  160. format['url'] = f['url']
  161. formats.append(format)
  162. self._check_formats(formats, video_id)
  163. self._sort_formats(formats)
  164. info_dict['formats'] = formats
  165. return info_dict
  166. class ArteTVPlus7IE(ArteTVBaseIE):
  167. IE_NAME = 'arte.tv:+7'
  168. _VALID_URL = r'https?://(?:(?:www|sites)\.)?arte\.tv/(?:[^/]+/)?(?P<lang>fr|de|en|es)/(?:videos/)?(?:[^/]+/)*(?P<id>[^/?#&]+)'
  169. _TESTS = [{
  170. 'url': 'http://www.arte.tv/guide/de/sendungen/XEN/xenius/?vid=055918-015_PLUS7-D',
  171. 'only_matching': True,
  172. }, {
  173. 'url': 'http://sites.arte.tv/karambolage/de/video/karambolage-22',
  174. 'only_matching': True,
  175. }, {
  176. 'url': 'http://www.arte.tv/de/videos/048696-000-A/der-kluge-bauch-unser-zweites-gehirn',
  177. 'only_matching': True,
  178. }]
  179. @classmethod
  180. def suitable(cls, url):
  181. return False if ArteTVPlaylistIE.suitable(url) else super(ArteTVPlus7IE, cls).suitable(url)
  182. def _real_extract(self, url):
  183. video_id, lang = self._extract_url_info(url)
  184. webpage = self._download_webpage(url, video_id)
  185. return self._extract_from_webpage(webpage, video_id, lang)
  186. def _extract_from_webpage(self, webpage, video_id, lang):
  187. patterns_templates = (r'arte_vp_url=["\'](.*?%s.*?)["\']', r'data-url=["\']([^"]+%s[^"]+)["\']')
  188. ids = (video_id, '')
  189. # some pages contain multiple videos (like
  190. # http://www.arte.tv/guide/de/sendungen/XEN/xenius/?vid=055918-015_PLUS7-D),
  191. # so we first try to look for json URLs that contain the video id from
  192. # the 'vid' parameter.
  193. patterns = [t % re.escape(_id) for _id in ids for t in patterns_templates]
  194. json_url = self._html_search_regex(
  195. patterns, webpage, 'json vp url', default=None)
  196. if not json_url:
  197. def find_iframe_url(webpage, default=NO_DEFAULT):
  198. return self._html_search_regex(
  199. r'<iframe[^>]+src=(["\'])(?P<url>.+\bjson_url=.+?)\1',
  200. webpage, 'iframe url', group='url', default=default)
  201. iframe_url = find_iframe_url(webpage, None)
  202. if not iframe_url:
  203. embed_url = self._html_search_regex(
  204. r'arte_vp_url_oembed=\'([^\']+?)\'', webpage, 'embed url', default=None)
  205. if embed_url:
  206. player = self._download_json(
  207. embed_url, video_id, 'Downloading player page')
  208. iframe_url = find_iframe_url(player['html'])
  209. # en and es URLs produce react-based pages with different layout (e.g.
  210. # http://www.arte.tv/guide/en/053330-002-A/carnival-italy?zone=world)
  211. if not iframe_url:
  212. program = self._search_regex(
  213. r'program\s*:\s*({.+?["\']embed_html["\'].+?}),?\s*\n',
  214. webpage, 'program', default=None)
  215. if program:
  216. embed_html = self._parse_json(program, video_id)
  217. if embed_html:
  218. iframe_url = find_iframe_url(embed_html['embed_html'])
  219. if iframe_url:
  220. json_url = compat_parse_qs(
  221. compat_urllib_parse_urlparse(iframe_url).query)['json_url'][0]
  222. if json_url:
  223. title = self._search_regex(
  224. r'<h3[^>]+title=(["\'])(?P<title>.+?)\1',
  225. webpage, 'title', default=None, group='title')
  226. return self._extract_from_json_url(json_url, video_id, lang, title=title)
  227. # Different kind of embed URL (e.g.
  228. # http://www.arte.tv/magazine/trepalium/fr/episode-0406-replay-trepalium)
  229. entries = [
  230. self.url_result(url)
  231. for _, url in re.findall(r'<iframe[^>]+src=(["\'])(?P<url>.+?)\1', webpage)]
  232. return self.playlist_result(entries)
  233. # It also uses the arte_vp_url url from the webpage to extract the information
  234. class ArteTVCreativeIE(ArteTVPlus7IE):
  235. IE_NAME = 'arte.tv:creative'
  236. _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de|en|es)/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  237. _TESTS = [{
  238. 'url': 'http://creative.arte.tv/fr/episode/osmosis-episode-1',
  239. 'info_dict': {
  240. 'id': '057405-001-A',
  241. 'ext': 'mp4',
  242. 'title': 'OSMOSIS - N\'AYEZ PLUS PEUR D\'AIMER (1)',
  243. 'upload_date': '20150716',
  244. },
  245. }, {
  246. 'url': 'http://creative.arte.tv/fr/Monty-Python-Reunion',
  247. 'playlist_count': 11,
  248. 'add_ie': ['Youtube'],
  249. }, {
  250. 'url': 'http://creative.arte.tv/de/episode/agentur-amateur-4-der-erste-kunde',
  251. 'only_matching': True,
  252. }]
  253. class ArteTVInfoIE(ArteTVPlus7IE):
  254. IE_NAME = 'arte.tv:info'
  255. _VALID_URL = r'https?://info\.arte\.tv/(?P<lang>fr|de|en|es)/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  256. _TESTS = [{
  257. 'url': 'http://info.arte.tv/fr/service-civique-un-cache-misere',
  258. 'info_dict': {
  259. 'id': '067528-000-A',
  260. 'ext': 'mp4',
  261. 'title': 'Service civique, un cache misère ?',
  262. 'upload_date': '20160403',
  263. },
  264. }]
  265. class ArteTVFutureIE(ArteTVPlus7IE):
  266. IE_NAME = 'arte.tv:future'
  267. _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  268. _TESTS = [{
  269. 'url': 'http://future.arte.tv/fr/info-sciences/les-ecrevisses-aussi-sont-anxieuses',
  270. 'info_dict': {
  271. 'id': '050940-028-A',
  272. 'ext': 'mp4',
  273. 'title': 'Les écrevisses aussi peuvent être anxieuses',
  274. 'upload_date': '20140902',
  275. },
  276. }, {
  277. 'url': 'http://future.arte.tv/fr/la-science-est-elle-responsable',
  278. 'only_matching': True,
  279. }]
  280. class ArteTVDDCIE(ArteTVPlus7IE):
  281. IE_NAME = 'arte.tv:ddc'
  282. _VALID_URL = r'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>[^/?#&]+)'
  283. _TESTS = []
  284. def _real_extract(self, url):
  285. video_id, lang = self._extract_url_info(url)
  286. if lang == 'folge':
  287. lang = 'de'
  288. elif lang == 'emission':
  289. lang = 'fr'
  290. webpage = self._download_webpage(url, video_id)
  291. scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
  292. script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
  293. javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
  294. json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
  295. return self._extract_from_json_url(json_url, video_id, lang)
  296. class ArteTVConcertIE(ArteTVPlus7IE):
  297. IE_NAME = 'arte.tv:concert'
  298. _VALID_URL = r'https?://concert\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  299. _TESTS = [{
  300. 'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde',
  301. 'md5': '9ea035b7bd69696b67aa2ccaaa218161',
  302. 'info_dict': {
  303. 'id': '186',
  304. 'ext': 'mp4',
  305. 'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"',
  306. 'upload_date': '20140128',
  307. 'description': 'md5:486eb08f991552ade77439fe6d82c305',
  308. },
  309. }]
  310. class ArteTVCinemaIE(ArteTVPlus7IE):
  311. IE_NAME = 'arte.tv:cinema'
  312. _VALID_URL = r'https?://cinema\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>.+)'
  313. _TESTS = [{
  314. 'url': 'http://cinema.arte.tv/fr/article/les-ailes-du-desir-de-julia-reck',
  315. 'md5': 'a5b9dd5575a11d93daf0e3f404f45438',
  316. 'info_dict': {
  317. 'id': '062494-000-A',
  318. 'ext': 'mp4',
  319. 'title': 'Film lauréat du concours web - "Les ailes du désir" de Julia Reck',
  320. 'upload_date': '20150807',
  321. },
  322. }]
  323. class ArteTVMagazineIE(ArteTVPlus7IE):
  324. IE_NAME = 'arte.tv:magazine'
  325. _VALID_URL = r'https?://(?:www\.)?arte\.tv/magazine/[^/]+/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  326. _TESTS = [{
  327. # Embedded via <iframe src="http://www.arte.tv/arte_vp/index.php?json_url=..."
  328. 'url': 'http://www.arte.tv/magazine/trepalium/fr/entretien-avec-le-realisateur-vincent-lannoo-trepalium',
  329. 'md5': '2a9369bcccf847d1c741e51416299f25',
  330. 'info_dict': {
  331. 'id': '065965-000-A',
  332. 'ext': 'mp4',
  333. 'title': 'Trepalium - Extrait Ep.01',
  334. 'upload_date': '20160121',
  335. },
  336. }, {
  337. # Embedded via <iframe src="http://www.arte.tv/guide/fr/embed/054813-004-A/medium"
  338. 'url': 'http://www.arte.tv/magazine/trepalium/fr/episode-0406-replay-trepalium',
  339. 'md5': 'fedc64fc7a946110fe311634e79782ca',
  340. 'info_dict': {
  341. 'id': '054813-004_PLUS7-F',
  342. 'ext': 'mp4',
  343. 'title': 'Trepalium (4/6)',
  344. 'description': 'md5:10057003c34d54e95350be4f9b05cb40',
  345. 'upload_date': '20160218',
  346. },
  347. }, {
  348. 'url': 'http://www.arte.tv/magazine/metropolis/de/frank-woeste-german-paris-metropolis',
  349. 'only_matching': True,
  350. }]
  351. class ArteTVEmbedIE(ArteTVPlus7IE):
  352. IE_NAME = 'arte.tv:embed'
  353. _VALID_URL = r'''(?x)
  354. http://www\.arte\.tv
  355. /(?:playerv2/embed|arte_vp/index)\.php\?json_url=
  356. (?P<json_url>
  357. http://arte\.tv/papi/tvguide/videos/stream/player/
  358. (?P<lang>[^/]+)/(?P<id>[^/]+)[^&]*
  359. )
  360. '''
  361. _TESTS = []
  362. def _real_extract(self, url):
  363. mobj = re.match(self._VALID_URL, url)
  364. video_id = mobj.group('id')
  365. lang = mobj.group('lang')
  366. json_url = mobj.group('json_url')
  367. return self._extract_from_json_url(json_url, video_id, lang)
  368. class TheOperaPlatformIE(ArteTVPlus7IE):
  369. IE_NAME = 'theoperaplatform'
  370. _VALID_URL = r'https?://(?:www\.)?theoperaplatform\.eu/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  371. _TESTS = [{
  372. 'url': 'http://www.theoperaplatform.eu/de/opera/verdi-otello',
  373. 'md5': '970655901fa2e82e04c00b955e9afe7b',
  374. 'info_dict': {
  375. 'id': '060338-009-A',
  376. 'ext': 'mp4',
  377. 'title': 'Verdi - OTELLO',
  378. 'upload_date': '20160927',
  379. },
  380. }]
  381. class ArteTVPlaylistIE(ArteTVBaseIE):
  382. IE_NAME = 'arte.tv:playlist'
  383. _VALID_URL = r'https?://(?:www\.)?arte\.tv/guide/(?P<lang>fr|de|en|es)/[^#]*#collection/(?P<id>PL-\d+)'
  384. _TESTS = [{
  385. 'url': 'http://www.arte.tv/guide/de/plus7/?country=DE#collection/PL-013263/ARTETV',
  386. 'info_dict': {
  387. 'id': 'PL-013263',
  388. 'title': 'Areva & Uramin',
  389. 'description': 'md5:a1dc0312ce357c262259139cfd48c9bf',
  390. },
  391. 'playlist_mincount': 6,
  392. }, {
  393. 'url': 'http://www.arte.tv/guide/de/playlists?country=DE#collection/PL-013190/ARTETV',
  394. 'only_matching': True,
  395. }]
  396. def _real_extract(self, url):
  397. playlist_id, lang = self._extract_url_info(url)
  398. collection = self._download_json(
  399. 'https://api.arte.tv/api/player/v1/collectionData/%s/%s?source=videos'
  400. % (lang, playlist_id), playlist_id)
  401. title = collection.get('title')
  402. description = collection.get('shortDescription') or collection.get('teaserText')
  403. entries = [
  404. self._extract_from_json_url(
  405. video['jsonUrl'], video.get('programId') or playlist_id, lang)
  406. for video in collection['videos'] if video.get('jsonUrl')]
  407. return self.playlist_result(entries, playlist_id, title, description)