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.

306 lines
12 KiB

12 years ago
12 years ago
12 years ago
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. ExtractorError,
  8. find_xpath_attr,
  9. unified_strdate,
  10. determine_ext,
  11. get_element_by_id,
  12. compat_str,
  13. get_element_by_attribute,
  14. )
  15. # There are different sources of video in arte.tv, the extraction process
  16. # is different for each one. The videos usually expire in 7 days, so we can't
  17. # add tests.
  18. class ArteTvIE(InfoExtractor):
  19. _VIDEOS_URL = r'(?:http://)?videos\.arte\.tv/(?P<lang>fr|de)/.*-(?P<id>.*?)\.html'
  20. _LIVEWEB_URL = r'(?:http://)?liveweb\.arte\.tv/(?P<lang>fr|de)/(?P<subpage>.+?)/(?P<name>.+)'
  21. _LIVE_URL = r'index-[0-9]+\.html$'
  22. IE_NAME = 'arte.tv'
  23. @classmethod
  24. def suitable(cls, url):
  25. return any(re.match(regex, url) for regex in (cls._VIDEOS_URL, cls._LIVEWEB_URL))
  26. # TODO implement Live Stream
  27. # from ..utils import compat_urllib_parse
  28. # def extractLiveStream(self, url):
  29. # video_lang = url.split('/')[-4]
  30. # info = self.grep_webpage(
  31. # url,
  32. # r'src="(.*?/videothek_js.*?\.js)',
  33. # 0,
  34. # [
  35. # (1, 'url', 'Invalid URL: %s' % url)
  36. # ]
  37. # )
  38. # http_host = url.split('/')[2]
  39. # next_url = 'http://%s%s' % (http_host, compat_urllib_parse.unquote(info.get('url')))
  40. # info = self.grep_webpage(
  41. # next_url,
  42. # r'(s_artestras_scst_geoFRDE_' + video_lang + '.*?)\'.*?' +
  43. # '(http://.*?\.swf).*?' +
  44. # '(rtmp://.*?)\'',
  45. # re.DOTALL,
  46. # [
  47. # (1, 'path', 'could not extract video path: %s' % url),
  48. # (2, 'player', 'could not extract video player: %s' % url),
  49. # (3, 'url', 'could not extract video url: %s' % url)
  50. # ]
  51. # )
  52. # video_url = '%s/%s' % (info.get('url'), info.get('path'))
  53. def _real_extract(self, url):
  54. mobj = re.match(self._VIDEOS_URL, url)
  55. if mobj is not None:
  56. id = mobj.group('id')
  57. lang = mobj.group('lang')
  58. return self._extract_video(url, id, lang)
  59. mobj = re.match(self._LIVEWEB_URL, url)
  60. if mobj is not None:
  61. name = mobj.group('name')
  62. lang = mobj.group('lang')
  63. return self._extract_liveweb(url, name, lang)
  64. if re.search(self._LIVE_URL, url) is not None:
  65. raise ExtractorError('Arte live streams are not yet supported, sorry')
  66. # self.extractLiveStream(url)
  67. # return
  68. raise ExtractorError('No video found')
  69. def _extract_video(self, url, video_id, lang):
  70. """Extract from videos.arte.tv"""
  71. ref_xml_url = url.replace('/videos/', '/do_delegate/videos/')
  72. ref_xml_url = ref_xml_url.replace('.html', ',view,asPlayerXml.xml')
  73. ref_xml_doc = self._download_xml(
  74. ref_xml_url, video_id, note='Downloading metadata')
  75. config_node = find_xpath_attr(ref_xml_doc, './/video', 'lang', lang)
  76. config_xml_url = config_node.attrib['ref']
  77. config_xml = self._download_webpage(
  78. config_xml_url, video_id, note='Downloading configuration')
  79. video_urls = list(re.finditer(r'<url quality="(?P<quality>.*?)">(?P<url>.*?)</url>', config_xml))
  80. def _key(m):
  81. quality = m.group('quality')
  82. if quality == 'hd':
  83. return 2
  84. else:
  85. return 1
  86. # We pick the best quality
  87. video_urls = sorted(video_urls, key=_key)
  88. video_url = list(video_urls)[-1].group('url')
  89. title = self._html_search_regex(r'<name>(.*?)</name>', config_xml, 'title')
  90. thumbnail = self._html_search_regex(r'<firstThumbnailUrl>(.*?)</firstThumbnailUrl>',
  91. config_xml, 'thumbnail')
  92. return {'id': video_id,
  93. 'title': title,
  94. 'thumbnail': thumbnail,
  95. 'url': video_url,
  96. 'ext': 'flv',
  97. }
  98. def _extract_liveweb(self, url, name, lang):
  99. """Extract form http://liveweb.arte.tv/"""
  100. webpage = self._download_webpage(url, name)
  101. video_id = self._search_regex(r'eventId=(\d+?)("|&)', webpage, 'event id')
  102. config_doc = self._download_xml('http://download.liveweb.arte.tv/o21/liveweb/events/event-%s.xml' % video_id,
  103. video_id, 'Downloading information')
  104. event_doc = config_doc.find('event')
  105. url_node = event_doc.find('video').find('urlHd')
  106. if url_node is None:
  107. url_node = event_doc.find('urlSd')
  108. return {'id': video_id,
  109. 'title': event_doc.find('name%s' % lang.capitalize()).text,
  110. 'url': url_node.text.replace('MP4', 'mp4'),
  111. 'ext': 'flv',
  112. 'thumbnail': self._og_search_thumbnail(webpage),
  113. }
  114. class ArteTVPlus7IE(InfoExtractor):
  115. IE_NAME = 'arte.tv:+7'
  116. _VALID_URL = r'https?://(?:www\.)?arte\.tv/guide/(?P<lang>fr|de)/(?:(?:sendungen|emissions)/)?(?P<id>.*?)/(?P<name>.*?)(\?.*)?'
  117. @classmethod
  118. def _extract_url_info(cls, url):
  119. mobj = re.match(cls._VALID_URL, url)
  120. lang = mobj.group('lang')
  121. # This is not a real id, it can be for example AJT for the news
  122. # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
  123. video_id = mobj.group('id')
  124. return video_id, lang
  125. def _real_extract(self, url):
  126. video_id, lang = self._extract_url_info(url)
  127. webpage = self._download_webpage(url, video_id)
  128. return self._extract_from_webpage(webpage, video_id, lang)
  129. def _extract_from_webpage(self, webpage, video_id, lang):
  130. json_url = self._html_search_regex(r'arte_vp_url="(.*?)"', webpage, 'json url')
  131. return self._extract_from_json_url(json_url, video_id, lang)
  132. def _extract_from_json_url(self, json_url, video_id, lang):
  133. json_info = self._download_webpage(json_url, video_id, 'Downloading info json')
  134. self.report_extraction(video_id)
  135. info = json.loads(json_info)
  136. player_info = info['videoJsonPlayer']
  137. info_dict = {
  138. 'id': player_info['VID'],
  139. 'title': player_info['VTI'],
  140. 'description': player_info.get('VDE'),
  141. 'upload_date': unified_strdate(player_info.get('VDA', '').split(' ')[0]),
  142. 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
  143. }
  144. all_formats = player_info['VSR'].values()
  145. # Some formats use the m3u8 protocol
  146. all_formats = list(filter(lambda f: f.get('videoFormat') != 'M3U8', all_formats))
  147. def _match_lang(f):
  148. if f.get('versionCode') is None:
  149. return True
  150. # Return true if that format is in the language of the url
  151. if lang == 'fr':
  152. l = 'F'
  153. elif lang == 'de':
  154. l = 'A'
  155. regexes = [r'VO?%s' % l, r'VO?.-ST%s' % l]
  156. return any(re.match(r, f['versionCode']) for r in regexes)
  157. # Some formats may not be in the same language as the url
  158. formats = filter(_match_lang, all_formats)
  159. formats = list(formats) # in python3 filter returns an iterator
  160. if not formats:
  161. # Some videos are only available in the 'Originalversion'
  162. # they aren't tagged as being in French or German
  163. if all(f['versionCode'] == 'VO' for f in all_formats):
  164. formats = all_formats
  165. else:
  166. raise ExtractorError(u'The formats list is empty')
  167. if re.match(r'[A-Z]Q', formats[0]['quality']) is not None:
  168. def sort_key(f):
  169. return ['HQ', 'MQ', 'EQ', 'SQ'].index(f['quality'])
  170. else:
  171. def sort_key(f):
  172. return (
  173. # Sort first by quality
  174. int(f.get('height',-1)),
  175. int(f.get('bitrate',-1)),
  176. # The original version with subtitles has lower relevance
  177. re.match(r'VO-ST(F|A)', f.get('versionCode', '')) is None,
  178. # The version with sourds/mal subtitles has also lower relevance
  179. re.match(r'VO?(F|A)-STM\1', f.get('versionCode', '')) is None,
  180. # Prefer http downloads over m3u8
  181. 0 if f['url'].endswith('m3u8') else 1,
  182. )
  183. formats = sorted(formats, key=sort_key)
  184. def _format(format_info):
  185. quality = ''
  186. height = format_info.get('height')
  187. if height is not None:
  188. quality = compat_str(height)
  189. bitrate = format_info.get('bitrate')
  190. if bitrate is not None:
  191. quality += '-%d' % bitrate
  192. if format_info.get('versionCode') is not None:
  193. format_id = '%s-%s' % (quality, format_info['versionCode'])
  194. else:
  195. format_id = quality
  196. info = {
  197. 'format_id': format_id,
  198. 'format_note': format_info.get('versionLibelle'),
  199. 'width': format_info.get('width'),
  200. 'height': height,
  201. }
  202. if format_info['mediaType'] == 'rtmp':
  203. info['url'] = format_info['streamer']
  204. info['play_path'] = 'mp4:' + format_info['url']
  205. info['ext'] = 'flv'
  206. else:
  207. info['url'] = format_info['url']
  208. info['ext'] = determine_ext(info['url'])
  209. return info
  210. info_dict['formats'] = [_format(f) for f in formats]
  211. return info_dict
  212. # It also uses the arte_vp_url url from the webpage to extract the information
  213. class ArteTVCreativeIE(ArteTVPlus7IE):
  214. IE_NAME = 'arte.tv:creative'
  215. _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de)/magazine?/(?P<id>.+)'
  216. _TEST = {
  217. 'url': 'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
  218. 'info_dict': {
  219. 'id': '050489-002',
  220. 'ext': 'mp4',
  221. 'title': 'Agentur Amateur / Agence Amateur #2 : Corporate Design',
  222. },
  223. }
  224. class ArteTVFutureIE(ArteTVPlus7IE):
  225. IE_NAME = 'arte.tv:future'
  226. _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de)/(thema|sujet)/.*?#article-anchor-(?P<id>\d+)'
  227. _TEST = {
  228. 'url': 'http://future.arte.tv/fr/sujet/info-sciences#article-anchor-7081',
  229. 'info_dict': {
  230. 'id': '050940-003',
  231. 'ext': 'mp4',
  232. 'title': 'Les champignons au secours de la planète',
  233. },
  234. }
  235. def _real_extract(self, url):
  236. anchor_id, lang = self._extract_url_info(url)
  237. webpage = self._download_webpage(url, anchor_id)
  238. row = get_element_by_id(anchor_id, webpage)
  239. return self._extract_from_webpage(row, anchor_id, lang)
  240. class ArteTVDDCIE(ArteTVPlus7IE):
  241. IE_NAME = 'arte.tv:ddc'
  242. _VALID_URL = r'https?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>.+)'
  243. def _real_extract(self, url):
  244. video_id, lang = self._extract_url_info(url)
  245. if lang == 'folge':
  246. lang = 'de'
  247. elif lang == 'emission':
  248. lang = 'fr'
  249. webpage = self._download_webpage(url, video_id)
  250. scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
  251. script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
  252. javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
  253. json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
  254. return self._extract_from_json_url(json_url, video_id, lang)
  255. class ArteTVConcertIE(ArteTVPlus7IE):
  256. IE_NAME = 'arte.tv:concert'
  257. _VALID_URL = r'https?://concert\.arte\.tv/(?P<lang>de|fr)/(?P<id>.+)'
  258. _TEST = {
  259. 'url': 'http://concert.arte.tv/de/notwist-im-pariser-konzertclub-divan-du-monde',
  260. 'md5': '9ea035b7bd69696b67aa2ccaaa218161',
  261. 'info_dict': {
  262. 'id': '186',
  263. 'ext': 'mp4',
  264. 'title': 'The Notwist im Pariser Konzertclub "Divan du Monde"',
  265. 'upload_date': '20140128',
  266. },
  267. }