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.

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