arte.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import re
  2. import json
  3. import xml.etree.ElementTree
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. find_xpath_attr,
  8. unified_strdate,
  9. determine_ext,
  10. )
  11. # There are different sources of video in arte.tv, the extraction process
  12. # is different for each one. The videos usually expire in 7 days, so we can't
  13. # add tests.
  14. class ArteTvIE(InfoExtractor):
  15. _VIDEOS_URL = r'(?:http://)?videos.arte.tv/(?P<lang>fr|de)/.*-(?P<id>.*?).html'
  16. _LIVEWEB_URL = r'(?:http://)?liveweb.arte.tv/(?P<lang>fr|de)/(?P<subpage>.+?)/(?P<name>.+)'
  17. _LIVE_URL = r'index-[0-9]+\.html$'
  18. IE_NAME = u'arte.tv'
  19. @classmethod
  20. def suitable(cls, url):
  21. return any(re.match(regex, url) for regex in (cls._VIDEOS_URL, cls._LIVEWEB_URL))
  22. # TODO implement Live Stream
  23. # from ..utils import compat_urllib_parse
  24. # def extractLiveStream(self, url):
  25. # video_lang = url.split('/')[-4]
  26. # info = self.grep_webpage(
  27. # url,
  28. # r'src="(.*?/videothek_js.*?\.js)',
  29. # 0,
  30. # [
  31. # (1, 'url', u'Invalid URL: %s' % url)
  32. # ]
  33. # )
  34. # http_host = url.split('/')[2]
  35. # next_url = 'http://%s%s' % (http_host, compat_urllib_parse.unquote(info.get('url')))
  36. # info = self.grep_webpage(
  37. # next_url,
  38. # r'(s_artestras_scst_geoFRDE_' + video_lang + '.*?)\'.*?' +
  39. # '(http://.*?\.swf).*?' +
  40. # '(rtmp://.*?)\'',
  41. # re.DOTALL,
  42. # [
  43. # (1, 'path', u'could not extract video path: %s' % url),
  44. # (2, 'player', u'could not extract video player: %s' % url),
  45. # (3, 'url', u'could not extract video url: %s' % url)
  46. # ]
  47. # )
  48. # video_url = u'%s/%s' % (info.get('url'), info.get('path'))
  49. def _real_extract(self, url):
  50. mobj = re.match(self._VIDEOS_URL, url)
  51. if mobj is not None:
  52. id = mobj.group('id')
  53. lang = mobj.group('lang')
  54. return self._extract_video(url, id, lang)
  55. mobj = re.match(self._LIVEWEB_URL, url)
  56. if mobj is not None:
  57. name = mobj.group('name')
  58. lang = mobj.group('lang')
  59. return self._extract_liveweb(url, name, lang)
  60. if re.search(self._LIVE_URL, video_id) is not None:
  61. raise ExtractorError(u'Arte live streams are not yet supported, sorry')
  62. # self.extractLiveStream(url)
  63. # return
  64. def _extract_video(self, url, video_id, lang):
  65. """Extract from videos.arte.tv"""
  66. ref_xml_url = url.replace('/videos/', '/do_delegate/videos/')
  67. ref_xml_url = ref_xml_url.replace('.html', ',view,asPlayerXml.xml')
  68. ref_xml = self._download_webpage(ref_xml_url, video_id, note=u'Downloading metadata')
  69. ref_xml_doc = xml.etree.ElementTree.fromstring(ref_xml)
  70. config_node = find_xpath_attr(ref_xml_doc, './/video', 'lang', lang)
  71. config_xml_url = config_node.attrib['ref']
  72. config_xml = self._download_webpage(config_xml_url, video_id, note=u'Downloading configuration')
  73. video_urls = list(re.finditer(r'<url quality="(?P<quality>.*?)">(?P<url>.*?)</url>', config_xml))
  74. def _key(m):
  75. quality = m.group('quality')
  76. if quality == 'hd':
  77. return 2
  78. else:
  79. return 1
  80. # We pick the best quality
  81. video_urls = sorted(video_urls, key=_key)
  82. video_url = list(video_urls)[-1].group('url')
  83. title = self._html_search_regex(r'<name>(.*?)</name>', config_xml, 'title')
  84. thumbnail = self._html_search_regex(r'<firstThumbnailUrl>(.*?)</firstThumbnailUrl>',
  85. config_xml, 'thumbnail')
  86. return {'id': video_id,
  87. 'title': title,
  88. 'thumbnail': thumbnail,
  89. 'url': video_url,
  90. 'ext': 'flv',
  91. }
  92. def _extract_liveweb(self, url, name, lang):
  93. """Extract form http://liveweb.arte.tv/"""
  94. webpage = self._download_webpage(url, name)
  95. video_id = self._search_regex(r'eventId=(\d+?)("|&)', webpage, u'event id')
  96. config_xml = self._download_webpage('http://download.liveweb.arte.tv/o21/liveweb/events/event-%s.xml' % video_id,
  97. video_id, u'Downloading information')
  98. config_doc = xml.etree.ElementTree.fromstring(config_xml.encode('utf-8'))
  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 = video_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. def _real_extract(self, url):
  113. mobj = re.match(self._VALID_URL, url)
  114. lang = mobj.group('lang')
  115. # This is not a real id, it can be for example AJT for the news
  116. # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
  117. video_id = mobj.group('id')
  118. webpage = self._download_webpage(url, video_id)
  119. json_url = self._html_search_regex(r'arte_vp_url="(.*?)"', webpage, 'json url')
  120. json_info = self._download_webpage(json_url, video_id, 'Downloading info json')
  121. self.report_extraction(video_id)
  122. info = json.loads(json_info)
  123. player_info = info['videoJsonPlayer']
  124. info_dict = {
  125. 'id': player_info['VID'],
  126. 'title': player_info['VTI'],
  127. 'description': player_info.get('VDE'),
  128. 'upload_date': unified_strdate(player_info.get('VDA', '').split(' ')[0]),
  129. 'thumbnail': player_info.get('programImage') or player_info.get('VTU', {}).get('IUR'),
  130. }
  131. formats = player_info['VSR'].values()
  132. def _match_lang(f):
  133. if f.get('versionCode') is None:
  134. return True
  135. # Return true if that format is in the language of the url
  136. if lang == 'fr':
  137. l = 'F'
  138. elif lang == 'de':
  139. l = 'A'
  140. regexes = [r'VO?%s' % l, r'VO?.-ST%s' % l]
  141. return any(re.match(r, f['versionCode']) for r in regexes)
  142. # Some formats may not be in the same language as the url
  143. formats = filter(_match_lang, formats)
  144. # Some formats use the m3u8 protocol
  145. formats = filter(lambda f: f.get('videoFormat') != 'M3U8', formats)
  146. # We order the formats by quality
  147. formats = sorted(formats, key=lambda f: int(f.get('height',-1)))
  148. # Prefer videos without subtitles in the same language
  149. formats = sorted(formats, key=lambda f: re.match(r'VO(F|A)-STM\1', f.get('versionCode', '')) is None)
  150. # Pick the best quality
  151. def _format(format_info):
  152. info = {
  153. 'width': format_info.get('width'),
  154. 'height': format_info.get('height'),
  155. }
  156. if format_info['mediaType'] == u'rtmp':
  157. info['url'] = format_info['streamer']
  158. info['play_path'] = 'mp4:' + format_info['url']
  159. info['ext'] = 'flv'
  160. else:
  161. info['url'] = format_info['url']
  162. info['ext'] = determine_ext(info['url'])
  163. return info
  164. info_dict['formats'] = [_format(f) for f in formats]
  165. # TODO: Remove when #980 has been merged
  166. info_dict.update(info_dict['formats'][-1])
  167. return info_dict
  168. # It also uses the arte_vp_url url from the webpage to extract the information
  169. class ArteTVCreativeIE(ArteTVPlus7IE):
  170. IE_NAME = u'arte.tv:creative'
  171. _VALID_URL = r'https?://creative\.arte\.tv/(?P<lang>fr|de)/magazine?/(?P<id>.+)'
  172. _TEST = {
  173. u'url': u'http://creative.arte.tv/de/magazin/agentur-amateur-corporate-design',
  174. u'file': u'050489-002.mp4',
  175. u'info_dict': {
  176. u'title': u'Agentur Amateur #2 - Corporate Design',
  177. },
  178. }