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.

282 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(u'Arte live streams are not yet supported, sorry')
  66. # self.extractLiveStream(url)
  67. # return
  68. def _extract_video(self, url, video_id, lang):
  69. """Extract from videos.arte.tv"""
  70. ref_xml_url = url.replace('/videos/', '/do_delegate/videos/')
  71. ref_xml_url = ref_xml_url.replace('.html', ',view,asPlayerXml.xml')
  72. ref_xml_doc = self._download_xml(ref_xml_url, video_id, note=u'Downloading metadata')
  73. config_node = find_xpath_attr(ref_xml_doc, './/video', 'lang', lang)
  74. config_xml_url = config_node.attrib['ref']
  75. config_xml = self._download_webpage(config_xml_url, video_id, note=u'Downloading configuration')
  76. video_urls = list(re.finditer(r'<url quality="(?P<quality>.*?)">(?P<url>.*?)</url>', config_xml))
  77. def _key(m):
  78. quality = m.group('quality')
  79. if quality == 'hd':
  80. return 2
  81. else:
  82. return 1
  83. # We pick the best quality
  84. video_urls = sorted(video_urls, key=_key)
  85. video_url = list(video_urls)[-1].group('url')
  86. title = self._html_search_regex(r'<name>(.*?)</name>', config_xml, 'title')
  87. thumbnail = self._html_search_regex(r'<firstThumbnailUrl>(.*?)</firstThumbnailUrl>',
  88. config_xml, 'thumbnail')
  89. return {'id': video_id,
  90. 'title': title,
  91. 'thumbnail': thumbnail,
  92. 'url': video_url,
  93. 'ext': 'flv',
  94. }
  95. def _extract_liveweb(self, url, name, lang):
  96. """Extract form http://liveweb.arte.tv/"""
  97. webpage = self._download_webpage(url, name)
  98. video_id = self._search_regex(r'eventId=(\d+?)("|&)', webpage, 'event id')
  99. config_doc = self._download_xml('http://download.liveweb.arte.tv/o21/liveweb/events/event-%s.xml' % video_id,
  100. video_id, 'Downloading information')
  101. event_doc = config_doc.find('event')
  102. url_node = event_doc.find('video').find('urlHd')
  103. if url_node is None:
  104. url_node = event_doc.find('urlSd')
  105. return {'id': video_id,
  106. 'title': event_doc.find('name%s' % lang.capitalize()).text,
  107. 'url': url_node.text.replace('MP4', 'mp4'),
  108. 'ext': 'flv',
  109. 'thumbnail': self._og_search_thumbnail(webpage),
  110. }
  111. class ArteTVPlus7IE(InfoExtractor):
  112. IE_NAME = 'arte.tv:+7'
  113. _VALID_URL = r'https?://www\.arte.tv/guide/(?P<lang>fr|de)/(?:(?:sendungen|emissions)/)?(?P<id>.*?)/(?P<name>.*?)(\?.*)?'
  114. @classmethod
  115. def _extract_url_info(cls, url):
  116. mobj = re.match(cls._VALID_URL, url)
  117. lang = mobj.group('lang')
  118. # This is not a real id, it can be for example AJT for the news
  119. # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
  120. video_id = mobj.group('id')
  121. return video_id, lang
  122. def _real_extract(self, url):
  123. video_id, lang = self._extract_url_info(url)
  124. webpage = self._download_webpage(url, video_id)
  125. return self._extract_from_webpage(webpage, video_id, lang)
  126. def _extract_from_webpage(self, webpage, video_id, lang):
  127. json_url = self._html_search_regex(r'arte_vp_url="(.*?)"', webpage, 'json url')
  128. return self._extract_from_json_url(json_url, video_id, lang)
  129. def _extract_from_json_url(self, json_url, video_id, lang):
  130. json_info = self._download_webpage(json_url, video_id, 'Downloading info json')
  131. self.report_extraction(video_id)
  132. info = json.loads(json_info)
  133. player_info = info['videoJsonPlayer']
  134. info_dict = {
  135. 'id': player_info['VID'],
  136. 'title': player_info['VTI'],
  137. 'description': player_info.get('VDE'),
  138. 'upload_date': unified_strdate(player_info.get('VDA', '').split(' ')[0]),
  139. 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
  140. }
  141. all_formats = player_info['VSR'].values()
  142. # Some formats use the m3u8 protocol
  143. all_formats = list(filter(lambda f: f.get('videoFormat') != 'M3U8', all_formats))
  144. def _match_lang(f):
  145. if f.get('versionCode') is None:
  146. return True
  147. # Return true if that format is in the language of the url
  148. if lang == 'fr':
  149. l = 'F'
  150. elif lang == 'de':
  151. l = 'A'
  152. regexes = [r'VO?%s' % l, r'VO?.-ST%s' % l]
  153. return any(re.match(r, f['versionCode']) for r in regexes)
  154. # Some formats may not be in the same language as the url
  155. formats = filter(_match_lang, all_formats)
  156. formats = list(formats) # in python3 filter returns an iterator
  157. if not formats:
  158. # Some videos are only available in the 'Originalversion'
  159. # they aren't tagged as being in French or German
  160. if all(f['versionCode'] == 'VO' for f in all_formats):
  161. formats = all_formats
  162. else:
  163. raise ExtractorError(u'The formats list is empty')
  164. if re.match(r'[A-Z]Q', formats[0]['quality']) is not None:
  165. def sort_key(f):
  166. return ['HQ', 'MQ', 'EQ', 'SQ'].index(f['quality'])
  167. else:
  168. def sort_key(f):
  169. return (
  170. # Sort first by quality
  171. int(f.get('height',-1)),
  172. int(f.get('bitrate',-1)),
  173. # The original version with subtitles has lower relevance
  174. re.match(r'VO-ST(F|A)', f.get('versionCode', '')) is None,
  175. # The version with sourds/mal subtitles has also lower relevance
  176. re.match(r'VO?(F|A)-STM\1', f.get('versionCode', '')) is None,
  177. )
  178. formats = sorted(formats, key=sort_key)
  179. def _format(format_info):
  180. quality = ''
  181. height = format_info.get('height')
  182. if height is not None:
  183. quality = compat_str(height)
  184. bitrate = format_info.get('bitrate')
  185. if bitrate is not None:
  186. quality += '-%d' % bitrate
  187. if format_info.get('versionCode') is not None:
  188. format_id = '%s-%s' % (quality, format_info['versionCode'])
  189. else:
  190. format_id = quality
  191. info = {
  192. 'format_id': format_id,
  193. 'format_note': format_info.get('versionLibelle'),
  194. 'width': format_info.get('width'),
  195. 'height': height,
  196. }
  197. if format_info['mediaType'] == 'rtmp':
  198. info['url'] = format_info['streamer']
  199. info['play_path'] = 'mp4:' + format_info['url']
  200. info['ext'] = 'flv'
  201. else:
  202. info['url'] = format_info['url']
  203. info['ext'] = determine_ext(info['url'])
  204. return info
  205. info_dict['formats'] = [_format(f) for f in 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)/magazine?/(?P<id>.+)'
  211. _TEST = {
  212. 'url': 'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
  213. 'file': '050489-002.mp4',
  214. 'info_dict': {
  215. 'title': 'Agentur Amateur / Agence Amateur #2 : Corporate Design',
  216. },
  217. }
  218. class ArteTVFutureIE(ArteTVPlus7IE):
  219. IE_NAME = 'arte.tv:future'
  220. _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de)/(thema|sujet)/.*?#article-anchor-(?P<id>\d+)'
  221. _TEST = {
  222. 'url': 'http://future.arte.tv/fr/sujet/info-sciences#article-anchor-7081',
  223. 'file': '050940-003.mp4',
  224. 'info_dict': {
  225. 'title': 'Les champignons au secours de la planète',
  226. },
  227. }
  228. def _real_extract(self, url):
  229. anchor_id, lang = self._extract_url_info(url)
  230. webpage = self._download_webpage(url, anchor_id)
  231. row = get_element_by_id(anchor_id, webpage)
  232. return self._extract_from_webpage(row, anchor_id, lang)
  233. class ArteTVDDCIE(ArteTVPlus7IE):
  234. IE_NAME = 'arte.tv:ddc'
  235. _VALID_URL = r'http?://ddc\.arte\.tv/(?P<lang>emission|folge)/(?P<id>.+)'
  236. def _real_extract(self, url):
  237. video_id, lang = self._extract_url_info(url)
  238. if lang == 'folge':
  239. lang = 'de'
  240. elif lang == 'emission':
  241. lang = 'fr'
  242. webpage = self._download_webpage(url, video_id)
  243. scriptElement = get_element_by_attribute('class', 'visu_video_block', webpage)
  244. script_url = self._html_search_regex(r'src="(.*?)"', scriptElement, 'script url')
  245. javascriptPlayerGenerator = self._download_webpage(script_url, video_id, 'Download javascript player generator')
  246. json_url = self._search_regex(r"json_url=(.*)&rendering_place.*", javascriptPlayerGenerator, 'json url')
  247. return self._extract_from_json_url(json_url, video_id, lang)