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.

280 lines
11 KiB

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