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.

398 lines
16 KiB

11 years ago
10 years ago
11 years ago
9 years ago
9 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 ArteTVPlus7IE(InfoExtractor):
  54. IE_NAME = 'arte.tv:+7'
  55. _VALID_URL = r'https?://(?:www\.)?arte\.tv/guide/(?P<lang>fr|de|en|es)/(?:(?:sendungen|emissions|embed)/)?(?P<id>[^/]+)/(?P<name>[^/?#&]+)'
  56. @classmethod
  57. def _extract_url_info(cls, url):
  58. mobj = re.match(cls._VALID_URL, url)
  59. lang = mobj.group('lang')
  60. query = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
  61. if 'vid' in query:
  62. video_id = query['vid'][0]
  63. else:
  64. # This is not a real id, it can be for example AJT for the news
  65. # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
  66. video_id = mobj.group('id')
  67. return video_id, lang
  68. def _real_extract(self, url):
  69. video_id, lang = self._extract_url_info(url)
  70. webpage = self._download_webpage(url, video_id)
  71. return self._extract_from_webpage(webpage, video_id, lang)
  72. def _extract_from_webpage(self, webpage, video_id, lang):
  73. patterns_templates = (r'arte_vp_url=["\'](.*?%s.*?)["\']', r'data-url=["\']([^"]+%s[^"]+)["\']')
  74. ids = (video_id, '')
  75. # some pages contain multiple videos (like
  76. # http://www.arte.tv/guide/de/sendungen/XEN/xenius/?vid=055918-015_PLUS7-D),
  77. # so we first try to look for json URLs that contain the video id from
  78. # the 'vid' parameter.
  79. patterns = [t % re.escape(_id) for _id in ids for t in patterns_templates]
  80. json_url = self._html_search_regex(
  81. patterns, webpage, 'json vp url', default=None)
  82. if not json_url:
  83. def find_iframe_url(webpage, default=NO_DEFAULT):
  84. return self._html_search_regex(
  85. r'<iframe[^>]+src=(["\'])(?P<url>.+\bjson_url=.+?)\1',
  86. webpage, 'iframe url', group='url', default=default)
  87. iframe_url = find_iframe_url(webpage, None)
  88. if not iframe_url:
  89. embed_url = self._html_search_regex(
  90. r'arte_vp_url_oembed=\'([^\']+?)\'', webpage, 'embed url', default=None)
  91. if embed_url:
  92. player = self._download_json(
  93. embed_url, video_id, 'Downloading player page')
  94. iframe_url = find_iframe_url(player['html'])
  95. # en and es URLs produce react-based pages with different layout (e.g.
  96. # http://www.arte.tv/guide/en/053330-002-A/carnival-italy?zone=world)
  97. if not iframe_url:
  98. program = self._search_regex(
  99. r'program\s*:\s*({.+?["\']embed_html["\'].+?}),?\s*\n',
  100. webpage, 'program', default=None)
  101. if program:
  102. embed_html = self._parse_json(program, video_id)
  103. if embed_html:
  104. iframe_url = find_iframe_url(embed_html['embed_html'])
  105. if iframe_url:
  106. json_url = compat_parse_qs(
  107. compat_urllib_parse_urlparse(iframe_url).query)['json_url'][0]
  108. if json_url:
  109. title = self._search_regex(
  110. r'<h3[^>]+title=(["\'])(?P<title>.+?)\1',
  111. webpage, 'title', default=None, group='title')
  112. return self._extract_from_json_url(json_url, video_id, lang, title=title)
  113. # Different kind of embed URL (e.g.
  114. # http://www.arte.tv/magazine/trepalium/fr/episode-0406-replay-trepalium)
  115. embed_url = self._search_regex(
  116. r'<iframe[^>]+src=(["\'])(?P<url>.+?)\1',
  117. webpage, 'embed url', group='url')
  118. return self.url_result(embed_url)
  119. def _extract_from_json_url(self, json_url, video_id, lang, title=None):
  120. info = self._download_json(json_url, video_id)
  121. player_info = info['videoJsonPlayer']
  122. upload_date_str = player_info.get('shootingDate')
  123. if not upload_date_str:
  124. upload_date_str = (player_info.get('VRA') or player_info.get('VDA') or '').split(' ')[0]
  125. title = (player_info.get('VTI') or title or player_info['VID']).strip()
  126. subtitle = player_info.get('VSU', '').strip()
  127. if subtitle:
  128. title += ' - %s' % subtitle
  129. info_dict = {
  130. 'id': player_info['VID'],
  131. 'title': title,
  132. 'description': player_info.get('VDE'),
  133. 'upload_date': unified_strdate(upload_date_str),
  134. 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
  135. }
  136. qfunc = qualities(['HQ', 'MQ', 'EQ', 'SQ'])
  137. LANGS = {
  138. 'fr': 'F',
  139. 'de': 'A',
  140. 'en': 'E[ANG]',
  141. 'es': 'E[ESP]',
  142. }
  143. langcode = LANGS.get(lang, lang)
  144. formats = []
  145. for format_id, format_dict in player_info['VSR'].items():
  146. f = dict(format_dict)
  147. versionCode = f.get('versionCode')
  148. l = re.escape(langcode)
  149. # Language preference from most to least priority
  150. # Reference: section 5.6.3 of
  151. # http://www.arte.tv/sites/en/corporate/files/complete-technical-guidelines-arte-geie-v1-05.pdf
  152. PREFERENCES = (
  153. # original version in requested language, without subtitles
  154. r'VO{0}$'.format(l),
  155. # original version in requested language, with partial subtitles in requested language
  156. r'VO{0}-ST{0}$'.format(l),
  157. # original version in requested language, with subtitles for the deaf and hard-of-hearing in requested language
  158. r'VO{0}-STM{0}$'.format(l),
  159. # non-original (dubbed) version in requested language, without subtitles
  160. r'V{0}$'.format(l),
  161. # non-original (dubbed) version in requested language, with subtitles partial subtitles in requested language
  162. r'V{0}-ST{0}$'.format(l),
  163. # non-original (dubbed) version in requested language, with subtitles for the deaf and hard-of-hearing in requested language
  164. r'V{0}-STM{0}$'.format(l),
  165. # original version in requested language, with partial subtitles in different language
  166. r'VO{0}-ST(?!{0}).+?$'.format(l),
  167. # original version in requested language, with subtitles for the deaf and hard-of-hearing in different language
  168. r'VO{0}-STM(?!{0}).+?$'.format(l),
  169. # original version in different language, with partial subtitles in requested language
  170. r'VO(?:(?!{0}).+?)?-ST{0}$'.format(l),
  171. # original version in different language, with subtitles for the deaf and hard-of-hearing in requested language
  172. r'VO(?:(?!{0}).+?)?-STM{0}$'.format(l),
  173. # original version in different language, without subtitles
  174. r'VO(?:(?!{0}))?$'.format(l),
  175. # original version in different language, with partial subtitles in different language
  176. r'VO(?:(?!{0}).+?)?-ST(?!{0}).+?$'.format(l),
  177. # original version in different language, with subtitles for the deaf and hard-of-hearing in different language
  178. r'VO(?:(?!{0}).+?)?-STM(?!{0}).+?$'.format(l),
  179. )
  180. for pref, p in enumerate(PREFERENCES):
  181. if re.match(p, versionCode):
  182. lang_pref = len(PREFERENCES) - pref
  183. break
  184. else:
  185. lang_pref = -1
  186. format = {
  187. 'format_id': format_id,
  188. 'preference': -10 if f.get('videoFormat') == 'M3U8' else None,
  189. 'language_preference': lang_pref,
  190. 'format_note': '%s, %s' % (f.get('versionCode'), f.get('versionLibelle')),
  191. 'width': int_or_none(f.get('width')),
  192. 'height': int_or_none(f.get('height')),
  193. 'tbr': int_or_none(f.get('bitrate')),
  194. 'quality': qfunc(f.get('quality')),
  195. }
  196. if f.get('mediaType') == 'rtmp':
  197. format['url'] = f['streamer']
  198. format['play_path'] = 'mp4:' + f['url']
  199. format['ext'] = 'flv'
  200. else:
  201. format['url'] = f['url']
  202. formats.append(format)
  203. self._check_formats(formats, video_id)
  204. self._sort_formats(formats)
  205. info_dict['formats'] = formats
  206. return info_dict
  207. # It also uses the arte_vp_url url from the webpage to extract the information
  208. class ArteTVCreativeIE(ArteTVPlus7IE):
  209. IE_NAME = 'arte.tv:creative'
  210. _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de|en|es)/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  211. _TESTS = [{
  212. 'url': 'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
  213. 'info_dict': {
  214. 'id': '72176',
  215. 'ext': 'mp4',
  216. 'title': 'Folge 2 - Corporate Design',
  217. 'upload_date': '20131004',
  218. },
  219. }, {
  220. 'url': 'http://creative.arte.tv/fr/Monty-Python-Reunion',
  221. 'info_dict': {
  222. 'id': '160676',
  223. 'ext': 'mp4',
  224. 'title': 'Monty Python live (mostly)',
  225. 'description': 'Événement ! Quarante-cinq ans après leurs premiers succès, les légendaires Monty Python remontent sur scène.\n',
  226. 'upload_date': '20140805',
  227. }
  228. }, {
  229. 'url': 'http://creative.arte.tv/de/episode/agentur-amateur-4-der-erste-kunde',
  230. 'only_matching': True,
  231. }]
  232. class ArteTVInfoIE(ArteTVPlus7IE):
  233. IE_NAME = 'arte.tv:info'
  234. _VALID_URL = r'https?://info\.arte\.tv/(?P<lang>fr|de|en|es)/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  235. _TEST = {
  236. 'url': 'http://info.arte.tv/fr/service-civique-un-cache-misere',
  237. 'info_dict': {
  238. 'id': '067528-000-A',
  239. 'ext': 'mp4',
  240. 'title': 'Service civique, un cache misère ?',
  241. 'upload_date': '20160403',
  242. },
  243. }
  244. class ArteTVFutureIE(ArteTVPlus7IE):
  245. IE_NAME = 'arte.tv:future'
  246. _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  247. _TESTS = [{
  248. 'url': 'http://future.arte.tv/fr/info-sciences/les-ecrevisses-aussi-sont-anxieuses',
  249. 'info_dict': {
  250. 'id': '050940-028-A',
  251. 'ext': 'mp4',
  252. 'title': 'Les écrevisses aussi peuvent être anxieuses',
  253. 'upload_date': '20140902',
  254. },
  255. }, {
  256. 'url': 'http://future.arte.tv/fr/la-science-est-elle-responsable',
  257. 'only_matching': True,
  258. }]
  259. class ArteTVDDCIE(ArteTVPlus7IE):
  260. IE_NAME = 'arte.tv:ddc'
  261. _VALID_URL = r'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>[^/?#&]+)'
  262. def _real_extract(self, url):
  263. video_id, lang = self._extract_url_info(url)
  264. if lang == 'folge':
  265. lang = 'de'
  266. elif lang == 'emission':
  267. lang = 'fr'
  268. webpage = self._download_webpage(url, video_id)
  269. scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
  270. script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
  271. javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
  272. json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
  273. return self._extract_from_json_url(json_url, video_id, lang)
  274. class ArteTVConcertIE(ArteTVPlus7IE):
  275. IE_NAME = 'arte.tv:concert'
  276. _VALID_URL = r'https?://concert\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  277. _TEST = {
  278. 'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde',
  279. 'md5': '9ea035b7bd69696b67aa2ccaaa218161',
  280. 'info_dict': {
  281. 'id': '186',
  282. 'ext': 'mp4',
  283. 'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"',
  284. 'upload_date': '20140128',
  285. 'description': 'md5:486eb08f991552ade77439fe6d82c305',
  286. },
  287. }
  288. class ArteTVCinemaIE(ArteTVPlus7IE):
  289. IE_NAME = 'arte.tv:cinema'
  290. _VALID_URL = r'https?://cinema\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>.+)'
  291. _TEST = {
  292. 'url': 'http://cinema.arte.tv/de/node/38291',
  293. 'md5': '6b275511a5107c60bacbeeda368c3aa1',
  294. 'info_dict': {
  295. 'id': '055876-000_PWA12025-D',
  296. 'ext': 'mp4',
  297. 'title': 'Tod auf dem Nil',
  298. 'upload_date': '20160122',
  299. 'description': 'md5:7f749bbb77d800ef2be11d54529b96bc',
  300. },
  301. }
  302. class ArteTVMagazineIE(ArteTVPlus7IE):
  303. IE_NAME = 'arte.tv:magazine'
  304. _VALID_URL = r'https?://(?:www\.)?arte\.tv/magazine/[^/]+/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  305. _TESTS = [{
  306. # Embedded via <iframe src="http://www.arte.tv/arte_vp/index.php?json_url=..."
  307. 'url': 'http://www.arte.tv/magazine/trepalium/fr/entretien-avec-le-realisateur-vincent-lannoo-trepalium',
  308. 'md5': '2a9369bcccf847d1c741e51416299f25',
  309. 'info_dict': {
  310. 'id': '065965-000-A',
  311. 'ext': 'mp4',
  312. 'title': 'Trepalium - Extrait Ep.01',
  313. 'upload_date': '20160121',
  314. },
  315. }, {
  316. # Embedded via <iframe src="http://www.arte.tv/guide/fr/embed/054813-004-A/medium"
  317. 'url': 'http://www.arte.tv/magazine/trepalium/fr/episode-0406-replay-trepalium',
  318. 'md5': 'fedc64fc7a946110fe311634e79782ca',
  319. 'info_dict': {
  320. 'id': '054813-004_PLUS7-F',
  321. 'ext': 'mp4',
  322. 'title': 'Trepalium (4/6)',
  323. 'description': 'md5:10057003c34d54e95350be4f9b05cb40',
  324. 'upload_date': '20160218',
  325. },
  326. }, {
  327. 'url': 'http://www.arte.tv/magazine/metropolis/de/frank-woeste-german-paris-metropolis',
  328. 'only_matching': True,
  329. }]
  330. class ArteTVEmbedIE(ArteTVPlus7IE):
  331. IE_NAME = 'arte.tv:embed'
  332. _VALID_URL = r'''(?x)
  333. http://www\.arte\.tv
  334. /(?:playerv2/embed|arte_vp/index)\.php\?json_url=
  335. (?P<json_url>
  336. http://arte\.tv/papi/tvguide/videos/stream/player/
  337. (?P<lang>[^/]+)/(?P<id>[^/]+)[^&]*
  338. )
  339. '''
  340. def _real_extract(self, url):
  341. mobj = re.match(self._VALID_URL, url)
  342. video_id = mobj.group('id')
  343. lang = mobj.group('lang')
  344. json_url = mobj.group('json_url')
  345. return self._extract_from_json_url(json_url, video_id, lang)