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.

286 lines
11 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. )
  181. formats = sorted(formats, key=sort_key)
  182. def _format(format_info):
  183. quality = ''
  184. height = format_info.get('height')
  185. if height is not None:
  186. quality = compat_str(height)
  187. bitrate = format_info.get('bitrate')
  188. if bitrate is not None:
  189. quality += '-%d' % bitrate
  190. if format_info.get('versionCode') is not None:
  191. format_id = '%s-%s' % (quality, format_info['versionCode'])
  192. else:
  193. format_id = quality
  194. info = {
  195. 'format_id': format_id,
  196. 'format_note': format_info.get('versionLibelle'),
  197. 'width': format_info.get('width'),
  198. 'height': height,
  199. }
  200. if format_info['mediaType'] == 'rtmp':
  201. info['url'] = format_info['streamer']
  202. info['play_path'] = 'mp4:' + format_info['url']
  203. info['ext'] = 'flv'
  204. else:
  205. info['url'] = format_info['url']
  206. info['ext'] = determine_ext(info['url'])
  207. return info
  208. info_dict['formats'] = [_format(f) for f in formats]
  209. return info_dict
  210. # It also uses the arte_vp_url url from the webpage to extract the information
  211. class ArteTVCreativeIE(ArteTVPlus7IE):
  212. IE_NAME = 'arte.tv:creative'
  213. _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de)/magazine?/(?P<id>.+)'
  214. _TEST = {
  215. 'url': 'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
  216. 'file': '050489-002.mp4',
  217. 'info_dict': {
  218. 'title': 'Agentur Amateur / Agence Amateur #2 : Corporate Design',
  219. },
  220. }
  221. class ArteTVFutureIE(ArteTVPlus7IE):
  222. IE_NAME = 'arte.tv:future'
  223. _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de)/(thema|sujet)/.*?#article-anchor-(?P<id>\d+)'
  224. _TEST = {
  225. 'url': 'http://future.arte.tv/fr/sujet/info-sciences#article-anchor-7081',
  226. 'file': '050940-003.mp4',
  227. 'info_dict': {
  228. 'title': 'Les champignons au secours de la planète',
  229. },
  230. }
  231. def _real_extract(self, url):
  232. anchor_id, lang = self._extract_url_info(url)
  233. webpage = self._download_webpage(url, anchor_id)
  234. row = get_element_by_id(anchor_id, webpage)
  235. return self._extract_from_webpage(row, anchor_id, lang)
  236. class ArteTVDDCIE(ArteTVPlus7IE):
  237. IE_NAME = 'arte.tv:ddc'
  238. _VALID_URL = r'http?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>.+)'
  239. def _real_extract(self, url):
  240. video_id, lang = self._extract_url_info(url)
  241. if lang == 'folge':
  242. lang = 'de'
  243. elif lang == 'emission':
  244. lang = 'fr'
  245. webpage = self._download_webpage(url, video_id)
  246. scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
  247. script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
  248. javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
  249. json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
  250. return self._extract_from_json_url(json_url, video_id, lang)