arte.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/(?P<lang>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. lang = mobj.group('lang')
  54. # This is not a real id, it can be for example AJT for the news
  55. # http://www.arte.tv/guide/fr/emissions/AJT/arte-journal
  56. video_id = mobj.group('id')
  57. return self._extract_emission(url, video_id, lang)
  58. mobj = re.match(self._VIDEOS_URL, url)
  59. if mobj is not None:
  60. id = mobj.group('id')
  61. return self._extract_video(url, id)
  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_emission(self, url, video_id, lang):
  67. """Extract from www.arte.tv/guide"""
  68. json_url = 'http://org-www.arte.tv/papi/tvguide/videos/stream/player/F/%s_PLUS7-F/ALL/ALL.json' % video_id
  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. def _match_lang(f):
  82. # Return true if that format is in the language of the url
  83. if lang == 'fr':
  84. l = 'F'
  85. elif lang == 'de':
  86. l = 'A'
  87. regexes = [r'VO?%s' % l, r'V%s-ST.' % l]
  88. return any(re.match(r, f['versionCode']) for r in regexes)
  89. # Some formats may not be in the same language as the url
  90. formats = filter(_match_lang, formats)
  91. # We order the formats by quality
  92. formats = sorted(formats, key=lambda f: int(f['height']))
  93. # Pick the best quality
  94. format_info = formats[-1]
  95. if format_info['mediaType'] == u'rtmp':
  96. info_dict['url'] = format_info['streamer']
  97. info_dict['play_path'] = 'mp4:' + format_info['url']
  98. else:
  99. info_dict['url'] = format_info['url']
  100. return info_dict
  101. def _extract_video(self, url, video_id):
  102. """Extract from videos.arte.tv"""
  103. config_xml_url = url.replace('/videos/', '/do_delegate/videos/')
  104. config_xml_url = config_xml_url.replace('.html', ',view,asPlayerXml.xml')
  105. config_xml = self._download_webpage(config_xml_url, video_id)
  106. config_xml_url = self._html_search_regex(r'<video lang=".*?" ref="(.*?)"', config_xml, 'config xml url')
  107. config_xml = self._download_webpage(config_xml_url, video_id)
  108. video_urls = list(re.finditer(r'<url quality="(?P<quality>.*?)">(?P<url>.*?)</url>', config_xml))
  109. def _key(m):
  110. quality = m.group('quality')
  111. if quality == 'hd':
  112. return 2
  113. else:
  114. return 1
  115. # We pick the best quality
  116. video_urls = sorted(video_urls, key=_key)
  117. video_url = list(video_urls)[-1].group('url')
  118. title = self._html_search_regex(r'<name>(.*?)</name>', config_xml, 'title')
  119. thumbnail = self._html_search_regex(r'<firstThumbnailUrl>(.*?)</firstThumbnailUrl>',
  120. config_xml, 'thumbnail')
  121. return {'id': video_id,
  122. 'title': title,
  123. 'thumbnail': thumbnail,
  124. 'url': video_url,
  125. 'ext': 'flv',
  126. }