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.

133 lines
5.2 KiB

  1. import re
  2. import json
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. # This is used by the not implemented extractLiveStream method
  6. compat_urllib_parse,
  7. ExtractorError,
  8. unified_strdate,
  9. )
  10. class ArteTvIE(InfoExtractor):
  11. """
  12. There are two sources of video in arte.tv: videos.arte.tv and
  13. www.arte.tv/guide, the extraction process is different for each one.
  14. The videos expire in 7 days, so we can't add tests.
  15. """
  16. _EMISSION_URL = r'(?:http://)?www\.arte.tv/guide/(?:fr|de)/(?:(?:sendungen|emissions)/)?(?P<id>.*?)/(?P<name>.*?)(\?.*)?'
  17. _VIDEOS_URL = r'(?:http://)?videos.arte.tv/(?:fr|de)/.*-(?P<id>.*?).html'
  18. _LIVE_URL = r'index-[0-9]+\.html$'
  19. IE_NAME = u'arte.tv'
  20. @classmethod
  21. def suitable(cls, url):
  22. return any(re.match(regex, url) for regex in (cls._EMISSION_URL, cls._VIDEOS_URL))
  23. # TODO implement Live Stream
  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._EMISSION_URL, url)
  51. if mobj is not None:
  52. name = mobj.group('name')
  53. # This is not a real id, it can be for example AJT for the news
  54. # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
  55. video_id = mobj.group('id')
  56. return self._extract_emission(url, video_id)
  57. mobj = re.match(self._VIDEOS_URL, url)
  58. if mobj is not None:
  59. id = mobj.group('id')
  60. return self._extract_video(url, id)
  61. if re.search(self._LIVE_URL, video_id) is not None:
  62. raise ExtractorError(u'Arte live streams are not yet supported, sorry')
  63. # self.extractLiveStream(url)
  64. # return
  65. def _extract_emission(self, url, video_id):
  66. """Extract from www.arte.tv/guide"""
  67. webpage = self._download_webpage(url, video_id)
  68. json_url = self._html_search_regex(r'arte_vp_url="(.*?)"', webpage, 'json url')
  69. json_info = self._download_webpage(json_url, video_id, 'Downloading info json')
  70. self.report_extraction(video_id)
  71. info = json.loads(json_info)
  72. player_info = info['videoJsonPlayer']
  73. info_dict = {'id': player_info['VID'],
  74. 'title': player_info['VTI'],
  75. 'description': player_info['VDE'],
  76. 'upload_date': unified_strdate(player_info['VDA'].split(' ')[0]),
  77. 'thumbnail': player_info['programImage'],
  78. 'ext': 'flv',
  79. }
  80. formats = player_info['VSR'].values()
  81. # We order the formats by quality
  82. formats = sorted(formats, key=lambda f: int(f['height']))
  83. # Pick the best quality
  84. format_info = formats[-1]
  85. if format_info['mediaType'] == u'rtmp':
  86. info_dict['url'] = format_info['streamer']
  87. info_dict['play_path'] = 'mp4:' + format_info['url']
  88. else:
  89. info_dict['url'] = format_info['url']
  90. return info_dict
  91. def _extract_video(self, url, video_id):
  92. """Extract from videos.arte.tv"""
  93. config_xml_url = url.replace('/videos/', '/do_delegate/videos/')
  94. config_xml_url = config_xml_url.replace('.html', ',view,asPlayerXml.xml')
  95. config_xml = self._download_webpage(config_xml_url, video_id)
  96. config_xml_url = self._html_search_regex(r'<video lang=".*?" ref="(.*?)"', config_xml, 'config xml url')
  97. config_xml = self._download_webpage(config_xml_url, video_id)
  98. video_urls = list(re.finditer(r'<url quality="(?P<quality>.*?)">(?P<url>.*?)</url>', config_xml))
  99. def _key(m):
  100. quality = m.group('quality')
  101. if quality == 'hd':
  102. return 2
  103. else:
  104. return 1
  105. # We pick the best quality
  106. video_urls = sorted(video_urls, key=_key)
  107. video_url = list(video_urls)[-1].group('url')
  108. title = self._html_search_regex(r'<name>(.*?)</name>', config_xml, 'title')
  109. thumbnail = self._html_search_regex(r'<firstThumbnailUrl>(.*?)</firstThumbnailUrl>',
  110. config_xml, 'thumbnail')
  111. return {'id': video_id,
  112. 'title': title,
  113. 'thumbnail': thumbnail,
  114. 'url': video_url,
  115. 'ext': 'flv',
  116. }