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.

443 lines
18 KiB

12 years ago
10 years ago
12 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\.)?arte\.tv/guide/(?P<lang>fr|de|en|es)/(?:(?:sendungen|emissions|embed)/)?(?P<id>[^/]+)/(?P<name>[^/?#&]+)'
  157. _TESTS = [{
  158. 'url': 'http://www.arte.tv/guide/de/sendungen/XEN/xenius/?vid=055918-015_PLUS7-D',
  159. 'only_matching': True,
  160. }]
  161. @classmethod
  162. def suitable(cls, url):
  163. return False if ArteTVPlaylistIE.suitable(url) else super(ArteTVPlus7IE, cls).suitable(url)
  164. def _real_extract(self, url):
  165. video_id, lang = self._extract_url_info(url)
  166. webpage = self._download_webpage(url, video_id)
  167. return self._extract_from_webpage(webpage, video_id, lang)
  168. def _extract_from_webpage(self, webpage, video_id, lang):
  169. patterns_templates = (r'arte_vp_url=["\'](.*?%s.*?)["\']', r'data-url=["\']([^"]+%s[^"]+)["\']')
  170. ids = (video_id, '')
  171. # some pages contain multiple videos (like
  172. # http://www.arte.tv/guide/de/sendungen/XEN/xenius/?vid=055918-015_PLUS7-D),
  173. # so we first try to look for json URLs that contain the video id from
  174. # the 'vid' parameter.
  175. patterns = [t % re.escape(_id) for _id in ids for t in patterns_templates]
  176. json_url = self._html_search_regex(
  177. patterns, webpage, 'json vp url', default=None)
  178. if not json_url:
  179. def find_iframe_url(webpage, default=NO_DEFAULT):
  180. return self._html_search_regex(
  181. r'<iframe[^>]+src=(["\'])(?P<url>.+\bjson_url=.+?)\1',
  182. webpage, 'iframe url', group='url', default=default)
  183. iframe_url = find_iframe_url(webpage, None)
  184. if not iframe_url:
  185. embed_url = self._html_search_regex(
  186. r'arte_vp_url_oembed=\'([^\']+?)\'', webpage, 'embed url', default=None)
  187. if embed_url:
  188. player = self._download_json(
  189. embed_url, video_id, 'Downloading player page')
  190. iframe_url = find_iframe_url(player['html'])
  191. # en and es URLs produce react-based pages with different layout (e.g.
  192. # http://www.arte.tv/guide/en/053330-002-A/carnival-italy?zone=world)
  193. if not iframe_url:
  194. program = self._search_regex(
  195. r'program\s*:\s*({.+?["\']embed_html["\'].+?}),?\s*\n',
  196. webpage, 'program', default=None)
  197. if program:
  198. embed_html = self._parse_json(program, video_id)
  199. if embed_html:
  200. iframe_url = find_iframe_url(embed_html['embed_html'])
  201. if iframe_url:
  202. json_url = compat_parse_qs(
  203. compat_urllib_parse_urlparse(iframe_url).query)['json_url'][0]
  204. if json_url:
  205. title = self._search_regex(
  206. r'<h3[^>]+title=(["\'])(?P<title>.+?)\1',
  207. webpage, 'title', default=None, group='title')
  208. return self._extract_from_json_url(json_url, video_id, lang, title=title)
  209. # Different kind of embed URL (e.g.
  210. # http://www.arte.tv/magazine/trepalium/fr/episode-0406-replay-trepalium)
  211. embed_url = self._search_regex(
  212. r'<iframe[^>]+src=(["\'])(?P<url>.+?)\1',
  213. webpage, 'embed url', group='url')
  214. return self.url_result(embed_url)
  215. # It also uses the arte_vp_url url from the webpage to extract the information
  216. class ArteTVCreativeIE(ArteTVPlus7IE):
  217. IE_NAME = 'arte.tv:creative'
  218. _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de|en|es)/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  219. _TESTS = [{
  220. 'url': 'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
  221. 'info_dict': {
  222. 'id': '72176',
  223. 'ext': 'mp4',
  224. 'title': 'Folge 2 - Corporate Design',
  225. 'upload_date': '20131004',
  226. },
  227. }, {
  228. 'url': 'http://creative.arte.tv/fr/Monty-Python-Reunion',
  229. 'info_dict': {
  230. 'id': '160676',
  231. 'ext': 'mp4',
  232. 'title': 'Monty Python live (mostly)',
  233. 'description': 'Événement ! Quarante-cinq ans après leurs premiers succès, les légendaires Monty Python remontent sur scène.\n',
  234. 'upload_date': '20140805',
  235. }
  236. }, {
  237. 'url': 'http://creative.arte.tv/de/episode/agentur-amateur-4-der-erste-kunde',
  238. 'only_matching': True,
  239. }]
  240. class ArteTVInfoIE(ArteTVPlus7IE):
  241. IE_NAME = 'arte.tv:info'
  242. _VALID_URL = r'https?://info\.arte\.tv/(?P<lang>fr|de|en|es)/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  243. _TESTS = [{
  244. 'url': 'http://info.arte.tv/fr/service-civique-un-cache-misere',
  245. 'info_dict': {
  246. 'id': '067528-000-A',
  247. 'ext': 'mp4',
  248. 'title': 'Service civique, un cache misère ?',
  249. 'upload_date': '20160403',
  250. },
  251. }]
  252. class ArteTVFutureIE(ArteTVPlus7IE):
  253. IE_NAME = 'arte.tv:future'
  254. _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  255. _TESTS = [{
  256. 'url': 'http://future.arte.tv/fr/info-sciences/les-ecrevisses-aussi-sont-anxieuses',
  257. 'info_dict': {
  258. 'id': '050940-028-A',
  259. 'ext': 'mp4',
  260. 'title': 'Les écrevisses aussi peuvent être anxieuses',
  261. 'upload_date': '20140902',
  262. },
  263. }, {
  264. 'url': 'http://future.arte.tv/fr/la-science-est-elle-responsable',
  265. 'only_matching': True,
  266. }]
  267. class ArteTVDDCIE(ArteTVPlus7IE):
  268. IE_NAME = 'arte.tv:ddc'
  269. _VALID_URL = r'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>[^/?#&]+)'
  270. _TESTS = []
  271. def _real_extract(self, url):
  272. video_id, lang = self._extract_url_info(url)
  273. if lang == 'folge':
  274. lang = 'de'
  275. elif lang == 'emission':
  276. lang = 'fr'
  277. webpage = self._download_webpage(url, video_id)
  278. scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
  279. script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
  280. javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
  281. json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
  282. return self._extract_from_json_url(json_url, video_id, lang)
  283. class ArteTVConcertIE(ArteTVPlus7IE):
  284. IE_NAME = 'arte.tv:concert'
  285. _VALID_URL = r'https?://concert\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  286. _TESTS = [{
  287. 'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde',
  288. 'md5': '9ea035b7bd69696b67aa2ccaaa218161',
  289. 'info_dict': {
  290. 'id': '186',
  291. 'ext': 'mp4',
  292. 'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"',
  293. 'upload_date': '20140128',
  294. 'description': 'md5:486eb08f991552ade77439fe6d82c305',
  295. },
  296. }]
  297. class ArteTVCinemaIE(ArteTVPlus7IE):
  298. IE_NAME = 'arte.tv:cinema'
  299. _VALID_URL = r'https?://cinema\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>.+)'
  300. _TESTS = [{
  301. 'url': 'http://cinema.arte.tv/de/node/38291',
  302. 'md5': '6b275511a5107c60bacbeeda368c3aa1',
  303. 'info_dict': {
  304. 'id': '055876-000_PWA12025-D',
  305. 'ext': 'mp4',
  306. 'title': 'Tod auf dem Nil',
  307. 'upload_date': '20160122',
  308. 'description': 'md5:7f749bbb77d800ef2be11d54529b96bc',
  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 ArteTVPlaylistIE(ArteTVBaseIE):
  357. IE_NAME = 'arte.tv:playlist'
  358. _VALID_URL = r'https?://(?:www\.)?arte\.tv/guide/(?P<lang>fr|de|en|es)/[^#]*#collection/(?P<id>PL-\d+)'
  359. _TESTS = [{
  360. 'url': 'http://www.arte.tv/guide/de/plus7/?country=DE#collection/PL-013263/ARTETV',
  361. 'info_dict': {
  362. 'id': 'PL-013263',
  363. 'title': 'Areva & Uramin',
  364. },
  365. 'playlist_mincount': 6,
  366. }, {
  367. 'url': 'http://www.arte.tv/guide/de/playlists?country=DE#collection/PL-013190/ARTETV',
  368. 'only_matching': True,
  369. }]
  370. def _real_extract(self, url):
  371. playlist_id, lang = self._extract_url_info(url)
  372. collection = self._download_json(
  373. 'https://api.arte.tv/api/player/v1/collectionData/%s/%s?source=videos'
  374. % (lang, playlist_id), playlist_id)
  375. title = collection.get('title')
  376. description = collection.get('shortDescription') or collection.get('teaserText')
  377. entries = [
  378. self._extract_from_json_url(
  379. video['jsonUrl'], video.get('programId') or playlist_id, lang)
  380. for video in collection['videos'] if video.get('jsonUrl')]
  381. return self.playlist_result(entries, playlist_id, title, description)