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.

152 lines
6.1 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. if video_id.replace('-','').isdigit():
  71. json_url = 'http://org-www.arte.tv/papi/tvguide/videos/stream/player/F/%s_PLUS7-F/ALL/ALL.json' % video_id
  72. else:
  73. # We don't know the real id of the video, we have to search in the webpage
  74. webpage = self._download_webpage(url, video_id)
  75. json_url = self._html_search_regex(r'arte_vp_url="(.*?)"', webpage, 'json url')
  76. json_info = self._download_webpage(json_url, video_id, 'Downloading info json')
  77. self.report_extraction(video_id)
  78. info = json.loads(json_info)
  79. player_info = info['videoJsonPlayer']
  80. info_dict = {'id': player_info['VID'],
  81. 'title': player_info['VTI'],
  82. 'description': player_info['VDE'],
  83. 'upload_date': unified_strdate(player_info['VDA'].split(' ')[0]),
  84. 'thumbnail': player_info['programImage'],
  85. 'ext': 'flv',
  86. }
  87. formats = player_info['VSR'].values()
  88. def _match_lang(f):
  89. # Return true if that format is in the language of the url
  90. if lang == 'fr':
  91. l = 'F'
  92. elif lang == 'de':
  93. l = 'A'
  94. regexes = [r'VO?%s' % l, r'V%s-ST.' % l]
  95. return any(re.match(r, f['versionCode']) for r in regexes)
  96. # Some formats may not be in the same language as the url
  97. formats = filter(_match_lang, formats)
  98. # We order the formats by quality
  99. formats = sorted(formats, key=lambda f: int(f['height']))
  100. # Pick the best quality
  101. format_info = formats[-1]
  102. if format_info['mediaType'] == u'rtmp':
  103. info_dict['url'] = format_info['streamer']
  104. info_dict['play_path'] = 'mp4:' + format_info['url']
  105. else:
  106. info_dict['url'] = format_info['url']
  107. return info_dict
  108. def _extract_video(self, url, video_id, lang):
  109. """Extract from videos.arte.tv"""
  110. ref_xml_url = url.replace('/videos/', '/do_delegate/videos/')
  111. ref_xml_url = ref_xml_url.replace('.html', ',view,asPlayerXml.xml')
  112. ref_xml = self._download_webpage(ref_xml_url, video_id, note=u'Downloading metadata')
  113. ref_xml_doc = xml.etree.ElementTree.fromstring(ref_xml)
  114. config_node = ref_xml_doc.find('.//video[@lang="%s"]' % lang)
  115. config_xml_url = config_node.attrib['ref']
  116. config_xml = self._download_webpage(config_xml_url, video_id, note=u'Downloading configuration')
  117. video_urls = list(re.finditer(r'<url quality="(?P<quality>.*?)">(?P<url>.*?)</url>', config_xml))
  118. def _key(m):
  119. quality = m.group('quality')
  120. if quality == 'hd':
  121. return 2
  122. else:
  123. return 1
  124. # We pick the best quality
  125. video_urls = sorted(video_urls, key=_key)
  126. video_url = list(video_urls)[-1].group('url')
  127. title = self._html_search_regex(r'<name>(.*?)</name>', config_xml, 'title')
  128. thumbnail = self._html_search_regex(r'<firstThumbnailUrl>(.*?)</firstThumbnailUrl>',
  129. config_xml, 'thumbnail')
  130. return {'id': video_id,
  131. 'title': title,
  132. 'thumbnail': thumbnail,
  133. 'url': video_url,
  134. 'ext': 'flv',
  135. }