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.

468 lines
18 KiB

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