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.

231 lines
9.1 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. )
  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, video_id) 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 = self._download_webpage(ref_xml_url, video_id, note=u'Downloading metadata')
  71. ref_xml_doc = xml.etree.ElementTree.fromstring(ref_xml)
  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_xml = self._download_webpage('http://download.liveweb.arte.tv/o21/liveweb/events/event-%s.xml' % video_id,
  99. video_id, u'Downloading information')
  100. config_doc = xml.etree.ElementTree.fromstring(config_xml.encode('utf-8'))
  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 = video_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 = u'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. json_info = self._download_webpage(json_url, video_id, 'Downloading info json')
  129. self.report_extraction(video_id)
  130. info = json.loads(json_info)
  131. player_info = info['videoJsonPlayer']
  132. info_dict = {
  133. 'id': player_info['VID'],
  134. 'title': player_info['VTI'],
  135. 'description': player_info.get('VDE'),
  136. 'upload_date': unified_strdate(player_info.get('VDA', '').split(' ')[0]),
  137. 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
  138. }
  139. formats = player_info['VSR'].values()
  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, formats)
  152. # Some formats use the m3u8 protocol
  153. formats = filter(lambda f: f.get('videoFormat') != 'M3U8', formats)
  154. # We order the formats by quality
  155. formats = sorted(formats, key=lambda f: int(f.get('height',-1)))
  156. # Prefer videos without subtitles in the same language
  157. formats = sorted(formats, key=lambda f: re.match(r'VO(F|A)-STM\1', f.get('versionCode', '')) is None)
  158. # Pick the best quality
  159. def _format(format_info):
  160. info = {
  161. 'width': format_info.get('width'),
  162. 'height': format_info.get('height'),
  163. }
  164. if format_info['mediaType'] == u'rtmp':
  165. info['url'] = format_info['streamer']
  166. info['play_path'] = 'mp4:' + format_info['url']
  167. info['ext'] = 'flv'
  168. else:
  169. info['url'] = format_info['url']
  170. info['ext'] = determine_ext(info['url'])
  171. return info
  172. info_dict['formats'] = [_format(f) for f in formats]
  173. # TODO: Remove when #980 has been merged
  174. info_dict.update(info_dict['formats'][-1])
  175. return info_dict
  176. # It also uses the arte_vp_url url from the webpage to extract the information
  177. class ArteTVCreativeIE(ArteTVPlus7IE):
  178. IE_NAME = u'arte.tv:creative'
  179. _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de)/magazine?/(?P<id>.+)'
  180. _TEST = {
  181. u'url': u'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
  182. u'file': u'050489-002.mp4',
  183. u'info_dict': {
  184. u'title': u'Agentur Amateur #2 - Corporate Design',
  185. },
  186. }
  187. class ArteTVFutureIE(ArteTVPlus7IE):
  188. IE_NAME = u'arte.tv:future'
  189. _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de)/(thema|sujet)/.*?#article-anchor-(?P<id>\d+)'
  190. _TEST = {
  191. u'url': u'http://future.arte.tv/fr/sujet/info-sciences#article-anchor-7081',
  192. u'file': u'050940-003.mp4',
  193. u'info_dict': {
  194. u'title': u'Les champignons au secours de la planète',
  195. },
  196. }
  197. def _real_extract(self, url):
  198. anchor_id, lang = self._extract_url_info(url)
  199. webpage = self._download_webpage(url, anchor_id)
  200. row = get_element_by_id(anchor_id, webpage)
  201. return self._extract_from_webpage(row, anchor_id, lang)