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.

251 lines
10 KiB

11 years ago
11 years ago
11 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. all_formats = player_info['VSR'].values()
  140. # Some formats use the m3u8 protocol
  141. all_formats = list(filter(lambda f: f.get('videoFormat') != 'M3U8', all_formats))
  142. def _match_lang(f):
  143. if f.get('versionCode') is None:
  144. return True
  145. # Return true if that format is in the language of the url
  146. if lang == 'fr':
  147. l = 'F'
  148. elif lang == 'de':
  149. l = 'A'
  150. regexes = [r'VO?%s' % l, r'VO?.-ST%s' % l]
  151. return any(re.match(r, f['versionCode']) for r in regexes)
  152. # Some formats may not be in the same language as the url
  153. formats = filter(_match_lang, all_formats)
  154. formats = list(formats) # in python3 filter returns an iterator
  155. if not formats:
  156. # Some videos are only available in the 'Originalversion'
  157. # they aren't tagged as being in French or German
  158. if all(f['versionCode'] == 'VO' for f in all_formats):
  159. formats = all_formats
  160. else:
  161. raise ExtractorError(u'The formats list is empty')
  162. # We order the formats by quality
  163. if re.match(r'[A-Z]Q', formats[0]['quality']) is not None:
  164. sort_key = lambda f: ['HQ', 'MQ', 'EQ', 'SQ'].index(f['quality'])
  165. else:
  166. sort_key = lambda f: int(f.get('height',-1))
  167. formats = sorted(formats, key=sort_key)
  168. # Prefer videos without subtitles in the same language
  169. formats = sorted(formats, key=lambda f: re.match(r'VO(F|A)-STM\1', f.get('versionCode', '')) is None)
  170. # Pick the best quality
  171. def _format(format_info):
  172. quality = format_info['quality']
  173. m_quality = re.match(r'\w*? - (\d*)p', quality)
  174. if m_quality is not None:
  175. quality = m_quality.group(1)
  176. if format_info.get('versionCode') is not None:
  177. format_id = u'%s-%s' % (quality, format_info['versionCode'])
  178. else:
  179. format_id = quality
  180. info = {
  181. 'format_id': format_id,
  182. 'format_note': format_info.get('versionLibelle'),
  183. 'width': format_info.get('width'),
  184. 'height': format_info.get('height'),
  185. }
  186. if format_info['mediaType'] == u'rtmp':
  187. info['url'] = format_info['streamer']
  188. info['play_path'] = 'mp4:' + format_info['url']
  189. info['ext'] = 'flv'
  190. else:
  191. info['url'] = format_info['url']
  192. info['ext'] = determine_ext(info['url'])
  193. return info
  194. info_dict['formats'] = [_format(f) for f in formats]
  195. return info_dict
  196. # It also uses the arte_vp_url url from the webpage to extract the information
  197. class ArteTVCreativeIE(ArteTVPlus7IE):
  198. IE_NAME = u'arte.tv:creative'
  199. _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de)/magazine?/(?P<id>.+)'
  200. _TEST = {
  201. u'url': u'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
  202. u'file': u'050489-002.mp4',
  203. u'info_dict': {
  204. u'title': u'Agentur Amateur / Agence Amateur #2 : Corporate Design',
  205. },
  206. }
  207. class ArteTVFutureIE(ArteTVPlus7IE):
  208. IE_NAME = u'arte.tv:future'
  209. _VALID_URL = r'https?://future\.arte\.tv/(?P<lang>fr|de)/(thema|sujet)/.*?#article-anchor-(?P<id>\d+)'
  210. _TEST = {
  211. u'url': u'http://future.arte.tv/fr/sujet/info-sciences#article-anchor-7081',
  212. u'file': u'050940-003.mp4',
  213. u'info_dict': {
  214. u'title': u'Les champignons au secours de la planète',
  215. },
  216. }
  217. def _real_extract(self, url):
  218. anchor_id, lang = self._extract_url_info(url)
  219. webpage = self._download_webpage(url, anchor_id)
  220. row = get_element_by_id(anchor_id, webpage)
  221. return self._extract_from_webpage(row, anchor_id, lang)