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.

262 lines
10 KiB

12 years ago
12 years ago
12 years ago
  1. # encoding: utf-8
  2. import re
  3. import json
  4. import xml.etree.ElementTree
  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. )
  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 = self._download_webpage(ref_xml_url, video_id, note=u'Downloading metadata')
  72. ref_xml_doc = xml.etree.ElementTree.fromstring(ref_xml)
  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, u'event id')
  99. config_xml = self._download_webpage('http://download.liveweb.arte.tv/o21/liveweb/events/event-%s.xml' % video_id,
  100. video_id, u'Downloading information')
  101. config_doc = xml.etree.ElementTree.fromstring(config_xml.encode('utf-8'))
  102. event_doc = config_doc.find('event')
  103. url_node = event_doc.find('video').find('urlHd')
  104. if url_node is None:
  105. url_node = event_doc.find('urlSd')
  106. return {'id': video_id,
  107. 'title': event_doc.find('name%s' % lang.capitalize()).text,
  108. 'url': url_node.text.replace('MP4', 'mp4'),
  109. 'ext': 'flv',
  110. 'thumbnail': self._og_search_thumbnail(webpage),
  111. }
  112. class ArteTVPlus7IE(InfoExtractor):
  113. IE_NAME = u'arte.tv:+7'
  114. _VALID_URL = r'https?://www\.arte.tv/guide/(?P<lang>fr|de)/(?:(?:sendungen|emissions)/)?(?P<id>.*?)/(?P<name>.*?)(\?.*)?'
  115. @classmethod
  116. def _extract_url_info(cls, url):
  117. mobj = re.match(cls._VALID_URL, url)
  118. lang = mobj.group('lang')
  119. # This is not a real id, it can be for example AJT for the news
  120. # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
  121. video_id = mobj.group('id')
  122. return video_id, lang
  123. def _real_extract(self, url):
  124. video_id, lang = self._extract_url_info(url)
  125. webpage = self._download_webpage(url, video_id)
  126. return self._extract_from_webpage(webpage, video_id, lang)
  127. def _extract_from_webpage(self, webpage, video_id, lang):
  128. json_url = self._html_search_regex(r'arte_vp_url="(.*?)"', webpage, 'json url')
  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)