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.

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