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.

148 lines
5.9 KiB

  1. import re
  2. import json
  3. import xml.etree.ElementTree
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. # This is used by the not implemented extractLiveStream method
  7. compat_urllib_parse,
  8. ExtractorError,
  9. unified_strdate,
  10. )
  11. class ArteTvIE(InfoExtractor):
  12. """
  13. There are two sources of video in arte.tv: videos.arte.tv and
  14. www.arte.tv/guide, the extraction process is different for each one.
  15. The videos expire in 7 days, so we can't add tests.
  16. """
  17. _EMISSION_URL = r'(?:http://)?www\.arte.tv/guide/(?P<lang>fr|de)/(?:(?:sendungen|emissions)/)?(?P<id>.*?)/(?P<name>.*?)(\?.*)?'
  18. _VIDEOS_URL = r'(?:http://)?videos.arte.tv/(?P<lang>fr|de)/.*-(?P<id>.*?).html'
  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._EMISSION_URL, cls._VIDEOS_URL))
  24. # TODO implement Live Stream
  25. # def extractLiveStream(self, url):
  26. # video_lang = url.split('/')[-4]
  27. # info = self.grep_webpage(
  28. # url,
  29. # r'src="(.*?/videothek_js.*?\.js)',
  30. # 0,
  31. # [
  32. # (1, 'url', u'Invalid URL: %s' % url)
  33. # ]
  34. # )
  35. # http_host = url.split('/')[2]
  36. # next_url = 'http://%s%s' % (http_host, compat_urllib_parse.unquote(info.get('url')))
  37. # info = self.grep_webpage(
  38. # next_url,
  39. # r'(s_artestras_scst_geoFRDE_' + video_lang + '.*?)\'.*?' +
  40. # '(http://.*?\.swf).*?' +
  41. # '(rtmp://.*?)\'',
  42. # re.DOTALL,
  43. # [
  44. # (1, 'path', u'could not extract video path: %s' % url),
  45. # (2, 'player', u'could not extract video player: %s' % url),
  46. # (3, 'url', u'could not extract video url: %s' % url)
  47. # ]
  48. # )
  49. # video_url = u'%s/%s' % (info.get('url'), info.get('path'))
  50. def _real_extract(self, url):
  51. mobj = re.match(self._EMISSION_URL, url)
  52. if mobj is not None:
  53. name = mobj.group('name')
  54. lang = mobj.group('lang')
  55. # This is not a real id, it can be for example AJT for the news
  56. # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
  57. video_id = mobj.group('id')
  58. return self._extract_emission(url, video_id, lang)
  59. mobj = re.match(self._VIDEOS_URL, url)
  60. if mobj is not None:
  61. id = mobj.group('id')
  62. lang = mobj.group('lang')
  63. return self._extract_video(url, id, lang)
  64. if re.search(self._LIVE_URL, video_id) is not None:
  65. raise ExtractorError(u'Arte live streams are not yet supported, sorry')
  66. # self.extractLiveStream(url)
  67. # return
  68. def _extract_emission(self, url, video_id, lang):
  69. """Extract from www.arte.tv/guide"""
  70. webpage = self._download_webpage(url, video_id)
  71. json_url = self._html_search_regex(r'arte_vp_url="(.*?)"', webpage, 'json url')
  72. json_info = self._download_webpage(json_url, video_id, 'Downloading info json')
  73. self.report_extraction(video_id)
  74. info = json.loads(json_info)
  75. player_info = info['videoJsonPlayer']
  76. info_dict = {'id': player_info['VID'],
  77. 'title': player_info['VTI'],
  78. 'description': player_info['VDE'],
  79. 'upload_date': unified_strdate(player_info['VDA'].split(' ')[0]),
  80. 'thumbnail': player_info['programImage'],
  81. 'ext': 'flv',
  82. }
  83. formats = player_info['VSR'].values()
  84. def _match_lang(f):
  85. # Return true if that format is in the language of the url
  86. if lang == 'fr':
  87. l = 'F'
  88. elif lang == 'de':
  89. l = 'A'
  90. regexes = [r'VO?%s' % l, r'V%s-ST.' % l]
  91. return any(re.match(r, f['versionCode']) for r in regexes)
  92. # Some formats may not be in the same language as the url
  93. formats = filter(_match_lang, formats)
  94. # We order the formats by quality
  95. formats = sorted(formats, key=lambda f: int(f['height']))
  96. # Pick the best quality
  97. format_info = formats[-1]
  98. if format_info['mediaType'] == u'rtmp':
  99. info_dict['url'] = format_info['streamer']
  100. info_dict['play_path'] = 'mp4:' + format_info['url']
  101. else:
  102. info_dict['url'] = format_info['url']
  103. return info_dict
  104. def _extract_video(self, url, video_id, lang):
  105. """Extract from videos.arte.tv"""
  106. ref_xml_url = url.replace('/videos/', '/do_delegate/videos/')
  107. ref_xml_url = ref_xml_url.replace('.html', ',view,asPlayerXml.xml')
  108. ref_xml = self._download_webpage(ref_xml_url, video_id, note=u'Downloading metadata')
  109. ref_xml_doc = xml.etree.ElementTree.fromstring(ref_xml)
  110. config_node = ref_xml_doc.find('.//video[@lang="%s"]' % lang)
  111. config_xml_url = config_node.attrib['ref']
  112. config_xml = self._download_webpage(config_xml_url, video_id, note=u'Downloading configuration')
  113. video_urls = list(re.finditer(r'<url quality="(?P<quality>.*?)">(?P<url>.*?)</url>', config_xml))
  114. def _key(m):
  115. quality = m.group('quality')
  116. if quality == 'hd':
  117. return 2
  118. else:
  119. return 1
  120. # We pick the best quality
  121. video_urls = sorted(video_urls, key=_key)
  122. video_url = list(video_urls)[-1].group('url')
  123. title = self._html_search_regex(r'<name>(.*?)</name>', config_xml, 'title')
  124. thumbnail = self._html_search_regex(r'<firstThumbnailUrl>(.*?)</firstThumbnailUrl>',
  125. config_xml, 'thumbnail')
  126. return {'id': video_id,
  127. 'title': title,
  128. 'thumbnail': thumbnail,
  129. 'url': video_url,
  130. 'ext': 'flv',
  131. }