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.

441 lines
18 KiB

11 years ago
10 years ago
11 years ago
  1. # encoding: 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)/(?:[^/]+/)*(?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. @classmethod
  165. def suitable(cls, url):
  166. return False if ArteTVPlaylistIE.suitable(url) else super(ArteTVPlus7IE, cls).suitable(url)
  167. def _real_extract(self, url):
  168. video_id, lang = self._extract_url_info(url)
  169. webpage = self._download_webpage(url, video_id)
  170. return self._extract_from_webpage(webpage, video_id, lang)
  171. def _extract_from_webpage(self, webpage, video_id, lang):
  172. patterns_templates = (r'arte_vp_url=["\'](.*?%s.*?)["\']', r'data-url=["\']([^"]+%s[^"]+)["\']')
  173. ids = (video_id, '')
  174. # some pages contain multiple videos (like
  175. # http://www.arte.tv/guide/de/sendungen/XEN/xenius/?vid=055918-015_PLUS7-D),
  176. # so we first try to look for json URLs that contain the video id from
  177. # the 'vid' parameter.
  178. patterns = [t % re.escape(_id) for _id in ids for t in patterns_templates]
  179. json_url = self._html_search_regex(
  180. patterns, webpage, 'json vp url', default=None)
  181. if not json_url:
  182. def find_iframe_url(webpage, default=NO_DEFAULT):
  183. return self._html_search_regex(
  184. r'<iframe[^>]+src=(["\'])(?P<url>.+\bjson_url=.+?)\1',
  185. webpage, 'iframe url', group='url', default=default)
  186. iframe_url = find_iframe_url(webpage, None)
  187. if not iframe_url:
  188. embed_url = self._html_search_regex(
  189. r'arte_vp_url_oembed=\'([^\']+?)\'', webpage, 'embed url', default=None)
  190. if embed_url:
  191. player = self._download_json(
  192. embed_url, video_id, 'Downloading player page')
  193. iframe_url = find_iframe_url(player['html'])
  194. # en and es URLs produce react-based pages with different layout (e.g.
  195. # http://www.arte.tv/guide/en/053330-002-A/carnival-italy?zone=world)
  196. if not iframe_url:
  197. program = self._search_regex(
  198. r'program\s*:\s*({.+?["\']embed_html["\'].+?}),?\s*\n',
  199. webpage, 'program', default=None)
  200. if program:
  201. embed_html = self._parse_json(program, video_id)
  202. if embed_html:
  203. iframe_url = find_iframe_url(embed_html['embed_html'])
  204. if iframe_url:
  205. json_url = compat_parse_qs(
  206. compat_urllib_parse_urlparse(iframe_url).query)['json_url'][0]
  207. if json_url:
  208. title = self._search_regex(
  209. r'<h3[^>]+title=(["\'])(?P<title>.+?)\1',
  210. webpage, 'title', default=None, group='title')
  211. return self._extract_from_json_url(json_url, video_id, lang, title=title)
  212. # Different kind of embed URL (e.g.
  213. # http://www.arte.tv/magazine/trepalium/fr/episode-0406-replay-trepalium)
  214. entries = [
  215. self.url_result(url)
  216. for _, url in re.findall(r'<iframe[^>]+src=(["\'])(?P<url>.+?)\1', webpage)]
  217. return self.playlist_result(entries)
  218. # It also uses the arte_vp_url url from the webpage to extract the information
  219. class ArteTVCreativeIE(ArteTVPlus7IE):
  220. IE_NAME = 'arte.tv:creative'
  221. _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de|en|es)/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  222. _TESTS = [{
  223. 'url': 'http://creative.arte.tv/fr/episode/osmosis-episode-1',
  224. 'info_dict': {
  225. 'id': '057405-001-A',
  226. 'ext': 'mp4',
  227. 'title': 'OSMOSIS - N\'AYEZ PLUS PEUR D\'AIMER (1)',
  228. 'upload_date': '20150716',
  229. },
  230. }, {
  231. 'url': 'http://creative.arte.tv/fr/Monty-Python-Reunion',
  232. 'playlist_count': 11,
  233. 'add_ie': ['Youtube'],
  234. }, {
  235. 'url': 'http://creative.arte.tv/de/episode/agentur-amateur-4-der-erste-kunde',
  236. 'only_matching': True,
  237. }]
  238. class ArteTVInfoIE(ArteTVPlus7IE):
  239. IE_NAME = 'arte.tv:info'
  240. _VALID_URL = r'https?://info\.arte\.tv/(?P<lang>fr|de|en|es)/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  241. _TESTS = [{
  242. 'url': 'http://info.arte.tv/fr/service-civique-un-cache-misere',
  243. 'info_dict': {
  244. 'id': '067528-000-A',
  245. 'ext': 'mp4',
  246. 'title': 'Service civique, un cache misère ?',
  247. 'upload_date': '20160403',
  248. },
  249. }]
  250. class ArteTVFutureIE(ArteTVPlus7IE):
  251. IE_NAME = 'arte.tv:future'
  252. _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  253. _TESTS = [{
  254. 'url': 'http://future.arte.tv/fr/info-sciences/les-ecrevisses-aussi-sont-anxieuses',
  255. 'info_dict': {
  256. 'id': '050940-028-A',
  257. 'ext': 'mp4',
  258. 'title': 'Les écrevisses aussi peuvent être anxieuses',
  259. 'upload_date': '20140902',
  260. },
  261. }, {
  262. 'url': 'http://future.arte.tv/fr/la-science-est-elle-responsable',
  263. 'only_matching': True,
  264. }]
  265. class ArteTVDDCIE(ArteTVPlus7IE):
  266. IE_NAME = 'arte.tv:ddc'
  267. _VALID_URL = r'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>[^/?#&]+)'
  268. _TESTS = []
  269. def _real_extract(self, url):
  270. video_id, lang = self._extract_url_info(url)
  271. if lang == 'folge':
  272. lang = 'de'
  273. elif lang == 'emission':
  274. lang = 'fr'
  275. webpage = self._download_webpage(url, video_id)
  276. scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
  277. script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
  278. javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
  279. json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
  280. return self._extract_from_json_url(json_url, video_id, lang)
  281. class ArteTVConcertIE(ArteTVPlus7IE):
  282. IE_NAME = 'arte.tv:concert'
  283. _VALID_URL = r'https?://concert\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  284. _TESTS = [{
  285. 'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde',
  286. 'md5': '9ea035b7bd69696b67aa2ccaaa218161',
  287. 'info_dict': {
  288. 'id': '186',
  289. 'ext': 'mp4',
  290. 'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"',
  291. 'upload_date': '20140128',
  292. 'description': 'md5:486eb08f991552ade77439fe6d82c305',
  293. },
  294. }]
  295. class ArteTVCinemaIE(ArteTVPlus7IE):
  296. IE_NAME = 'arte.tv:cinema'
  297. _VALID_URL = r'https?://cinema\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>.+)'
  298. _TESTS = [{
  299. 'url': 'http://cinema.arte.tv/fr/article/les-ailes-du-desir-de-julia-reck',
  300. 'md5': 'a5b9dd5575a11d93daf0e3f404f45438',
  301. 'info_dict': {
  302. 'id': '062494-000-A',
  303. 'ext': 'mp4',
  304. 'title': 'Film lauréat du concours web - "Les ailes du désir" de Julia Reck',
  305. 'upload_date': '20150807',
  306. },
  307. }]
  308. class ArteTVMagazineIE(ArteTVPlus7IE):
  309. IE_NAME = 'arte.tv:magazine'
  310. _VALID_URL = r'https?://(?:www\.)?arte\.tv/magazine/[^/]+/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  311. _TESTS = [{
  312. # Embedded via <iframe src="http://www.arte.tv/arte_vp/index.php?json_url=..."
  313. 'url': 'http://www.arte.tv/magazine/trepalium/fr/entretien-avec-le-realisateur-vincent-lannoo-trepalium',
  314. 'md5': '2a9369bcccf847d1c741e51416299f25',
  315. 'info_dict': {
  316. 'id': '065965-000-A',
  317. 'ext': 'mp4',
  318. 'title': 'Trepalium - Extrait Ep.01',
  319. 'upload_date': '20160121',
  320. },
  321. }, {
  322. # Embedded via <iframe src="http://www.arte.tv/guide/fr/embed/054813-004-A/medium"
  323. 'url': 'http://www.arte.tv/magazine/trepalium/fr/episode-0406-replay-trepalium',
  324. 'md5': 'fedc64fc7a946110fe311634e79782ca',
  325. 'info_dict': {
  326. 'id': '054813-004_PLUS7-F',
  327. 'ext': 'mp4',
  328. 'title': 'Trepalium (4/6)',
  329. 'description': 'md5:10057003c34d54e95350be4f9b05cb40',
  330. 'upload_date': '20160218',
  331. },
  332. }, {
  333. 'url': 'http://www.arte.tv/magazine/metropolis/de/frank-woeste-german-paris-metropolis',
  334. 'only_matching': True,
  335. }]
  336. class ArteTVEmbedIE(ArteTVPlus7IE):
  337. IE_NAME = 'arte.tv:embed'
  338. _VALID_URL = r'''(?x)
  339. http://www\.arte\.tv
  340. /(?:playerv2/embed|arte_vp/index)\.php\?json_url=
  341. (?P<json_url>
  342. http://arte\.tv/papi/tvguide/videos/stream/player/
  343. (?P<lang>[^/]+)/(?P<id>[^/]+)[^&]*
  344. )
  345. '''
  346. _TESTS = []
  347. def _real_extract(self, url):
  348. mobj = re.match(self._VALID_URL, url)
  349. video_id = mobj.group('id')
  350. lang = mobj.group('lang')
  351. json_url = mobj.group('json_url')
  352. return self._extract_from_json_url(json_url, video_id, lang)
  353. class ArteTVPlaylistIE(ArteTVBaseIE):
  354. IE_NAME = 'arte.tv:playlist'
  355. _VALID_URL = r'https?://(?:www\.)?arte\.tv/guide/(?P<lang>fr|de|en|es)/[^#]*#collection/(?P<id>PL-\d+)'
  356. _TESTS = [{
  357. 'url': 'http://www.arte.tv/guide/de/plus7/?country=DE#collection/PL-013263/ARTETV',
  358. 'info_dict': {
  359. 'id': 'PL-013263',
  360. 'title': 'Areva & Uramin',
  361. 'description': 'md5:a1dc0312ce357c262259139cfd48c9bf',
  362. },
  363. 'playlist_mincount': 6,
  364. }, {
  365. 'url': 'http://www.arte.tv/guide/de/playlists?country=DE#collection/PL-013190/ARTETV',
  366. 'only_matching': True,
  367. }]
  368. def _real_extract(self, url):
  369. playlist_id, lang = self._extract_url_info(url)
  370. collection = self._download_json(
  371. 'https://api.arte.tv/api/player/v1/collectionData/%s/%s?source=videos'
  372. % (lang, playlist_id), playlist_id)
  373. title = collection.get('title')
  374. description = collection.get('shortDescription') or collection.get('teaserText')
  375. entries = [
  376. self._extract_from_json_url(
  377. video['jsonUrl'], video.get('programId') or playlist_id, lang)
  378. for video in collection['videos'] if video.get('jsonUrl')]
  379. return self.playlist_result(entries, playlist_id, title, description)