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.

349 lines
13 KiB

12 years ago
10 years ago
12 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'http://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. return self._extract_from_json_url(json_url, video_id, lang)
  110. # Differend kind of embed URL (e.g.
  111. # http://www.arte.tv/magazine/trepalium/fr/episode-0406-replay-trepalium)
  112. embed_url = self._search_regex(
  113. r'<iframe[^>]+src=(["\'])(?P<url>.+?)\1',
  114. webpage, 'embed url', group='url')
  115. return self.url_result(embed_url)
  116. def _extract_from_json_url(self, json_url, video_id, lang):
  117. info = self._download_json(json_url, video_id)
  118. player_info = info['videoJsonPlayer']
  119. upload_date_str = player_info.get('shootingDate')
  120. if not upload_date_str:
  121. upload_date_str = (player_info.get('VRA') or player_info.get('VDA') or '').split(' ')[0]
  122. title = player_info['VTI'].strip()
  123. subtitle = player_info.get('VSU', '').strip()
  124. if subtitle:
  125. title += ' - %s' % subtitle
  126. info_dict = {
  127. 'id': player_info['VID'],
  128. 'title': title,
  129. 'description': player_info.get('VDE'),
  130. 'upload_date': unified_strdate(upload_date_str),
  131. 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
  132. }
  133. qfunc = qualities(['HQ', 'MQ', 'EQ', 'SQ'])
  134. LANGS = {
  135. 'fr': 'F',
  136. 'de': 'A',
  137. 'en': 'E[ANG]',
  138. 'es': 'E[ESP]',
  139. }
  140. formats = []
  141. for format_id, format_dict in player_info['VSR'].items():
  142. f = dict(format_dict)
  143. versionCode = f.get('versionCode')
  144. langcode = LANGS.get(lang, lang)
  145. lang_rexs = [r'VO?%s-' % re.escape(langcode), r'VO?.-ST%s$' % re.escape(langcode)]
  146. lang_pref = None
  147. if versionCode:
  148. matched_lang_rexs = [r for r in lang_rexs if re.match(r, versionCode)]
  149. lang_pref = -10 if not matched_lang_rexs else 10 * len(matched_lang_rexs)
  150. source_pref = 0
  151. if versionCode is not None:
  152. # The original version with subtitles has lower relevance
  153. if re.match(r'VO-ST(F|A|E)', versionCode):
  154. source_pref -= 10
  155. # The version with sourds/mal subtitles has also lower relevance
  156. elif re.match(r'VO?(F|A|E)-STM\1', versionCode):
  157. source_pref -= 9
  158. format = {
  159. 'format_id': format_id,
  160. 'preference': -10 if f.get('videoFormat') == 'M3U8' else None,
  161. 'language_preference': lang_pref,
  162. 'format_note': '%s, %s' % (f.get('versionCode'), f.get('versionLibelle')),
  163. 'width': int_or_none(f.get('width')),
  164. 'height': int_or_none(f.get('height')),
  165. 'tbr': int_or_none(f.get('bitrate')),
  166. 'quality': qfunc(f.get('quality')),
  167. 'source_preference': source_pref,
  168. }
  169. if f.get('mediaType') == 'rtmp':
  170. format['url'] = f['streamer']
  171. format['play_path'] = 'mp4:' + f['url']
  172. format['ext'] = 'flv'
  173. else:
  174. format['url'] = f['url']
  175. formats.append(format)
  176. self._check_formats(formats, video_id)
  177. self._sort_formats(formats)
  178. info_dict['formats'] = formats
  179. return info_dict
  180. # It also uses the arte_vp_url url from the webpage to extract the information
  181. class ArteTVCreativeIE(ArteTVPlus7IE):
  182. IE_NAME = 'arte.tv:creative'
  183. _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de|en|es)/(?:magazine?/)?(?P<id>[^/?#&]+)'
  184. _TESTS = [{
  185. 'url': 'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
  186. 'info_dict': {
  187. 'id': '72176',
  188. 'ext': 'mp4',
  189. 'title': 'Folge 2 - Corporate Design',
  190. 'upload_date': '20131004',
  191. },
  192. }, {
  193. 'url': 'http://creative.arte.tv/fr/Monty-Python-Reunion',
  194. 'info_dict': {
  195. 'id': '160676',
  196. 'ext': 'mp4',
  197. 'title': 'Monty Python live (mostly)',
  198. 'description': 'Événement ! Quarante-cinq ans après leurs premiers succès, les légendaires Monty Python remontent sur scène.\n',
  199. 'upload_date': '20140805',
  200. }
  201. }]
  202. class ArteTVFutureIE(ArteTVPlus7IE):
  203. IE_NAME = 'arte.tv:future'
  204. _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  205. _TESTS = [{
  206. 'url': 'http://future.arte.tv/fr/info-sciences/les-ecrevisses-aussi-sont-anxieuses',
  207. 'info_dict': {
  208. 'id': '050940-028-A',
  209. 'ext': 'mp4',
  210. 'title': 'Les écrevisses aussi peuvent être anxieuses',
  211. 'upload_date': '20140902',
  212. },
  213. }, {
  214. 'url': 'http://future.arte.tv/fr/la-science-est-elle-responsable',
  215. 'only_matching': True,
  216. }]
  217. class ArteTVDDCIE(ArteTVPlus7IE):
  218. IE_NAME = 'arte.tv:ddc'
  219. _VALID_URL = r'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>[^/?#&]+)'
  220. def _real_extract(self, url):
  221. video_id, lang = self._extract_url_info(url)
  222. if lang == 'folge':
  223. lang = 'de'
  224. elif lang == 'emission':
  225. lang = 'fr'
  226. webpage = self._download_webpage(url, video_id)
  227. scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
  228. script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
  229. javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
  230. json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
  231. return self._extract_from_json_url(json_url, video_id, lang)
  232. class ArteTVConcertIE(ArteTVPlus7IE):
  233. IE_NAME = 'arte.tv:concert'
  234. _VALID_URL = r'https?://concert\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  235. _TEST = {
  236. 'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde',
  237. 'md5': '9ea035b7bd69696b67aa2ccaaa218161',
  238. 'info_dict': {
  239. 'id': '186',
  240. 'ext': 'mp4',
  241. 'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"',
  242. 'upload_date': '20140128',
  243. 'description': 'md5:486eb08f991552ade77439fe6d82c305',
  244. },
  245. }
  246. class ArteTVCinemaIE(ArteTVPlus7IE):
  247. IE_NAME = 'arte.tv:cinema'
  248. _VALID_URL = r'https?://cinema\.arte\.tv/(?P<lang>fr|de|en|es)/(?P<id>.+)'
  249. _TEST = {
  250. 'url': 'http://cinema.arte.tv/de/node/38291',
  251. 'md5': '6b275511a5107c60bacbeeda368c3aa1',
  252. 'info_dict': {
  253. 'id': '055876-000_PWA12025-D',
  254. 'ext': 'mp4',
  255. 'title': 'Tod auf dem Nil',
  256. 'upload_date': '20160122',
  257. 'description': 'md5:7f749bbb77d800ef2be11d54529b96bc',
  258. },
  259. }
  260. class ArteTVMagazineIE(ArteTVPlus7IE):
  261. IE_NAME = 'arte.tv:magazine'
  262. _VALID_URL = r'https?://(?:www\.)?arte\.tv/magazine/[^/]+/(?P<lang>fr|de|en|es)/(?P<id>[^/?#&]+)'
  263. _TESTS = [{
  264. # Embedded via <iframe src="http://www.arte.tv/arte_vp/index.php?json_url=..."
  265. 'url': 'http://www.arte.tv/magazine/trepalium/fr/entretien-avec-le-realisateur-vincent-lannoo-trepalium',
  266. 'md5': '2a9369bcccf847d1c741e51416299f25',
  267. 'info_dict': {
  268. 'id': '065965-000-A',
  269. 'ext': 'mp4',
  270. 'title': 'Trepalium - Extrait Ep.01',
  271. 'upload_date': '20160121',
  272. },
  273. }, {
  274. # Embedded via <iframe src="http://www.arte.tv/guide/fr/embed/054813-004-A/medium"
  275. 'url': 'http://www.arte.tv/magazine/trepalium/fr/episode-0406-replay-trepalium',
  276. 'md5': 'fedc64fc7a946110fe311634e79782ca',
  277. 'info_dict': {
  278. 'id': '054813-004_PLUS7-F',
  279. 'ext': 'mp4',
  280. 'title': 'Trepalium (4/6)',
  281. 'description': 'md5:10057003c34d54e95350be4f9b05cb40',
  282. 'upload_date': '20160218',
  283. },
  284. }, {
  285. 'url': 'http://www.arte.tv/magazine/metropolis/de/frank-woeste-german-paris-metropolis',
  286. 'only_matching': True,
  287. }]
  288. class ArteTVEmbedIE(ArteTVPlus7IE):
  289. IE_NAME = 'arte.tv:embed'
  290. _VALID_URL = r'''(?x)
  291. http://www\.arte\.tv
  292. /playerv2/embed\.php\?json_url=
  293. (?P<json_url>
  294. http://arte\.tv/papi/tvguide/videos/stream/player/
  295. (?P<lang>[^/]+)/(?P<id>[^/]+)[^&]*
  296. )
  297. '''
  298. def _real_extract(self, url):
  299. mobj = re.match(self._VALID_URL, url)
  300. video_id = mobj.group('id')
  301. lang = mobj.group('lang')
  302. json_url = mobj.group('json_url')
  303. return self._extract_from_json_url(json_url, video_id, lang)