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.

58 lines
2.0 KiB

  1. # encoding: utf-8
  2. import re
  3. import json
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_urlparse,
  7. compat_str,
  8. ExtractorError,
  9. )
  10. class RutubeIE(InfoExtractor):
  11. _VALID_URL = r'https?://rutube\.ru/video/(?P<long_id>\w+)'
  12. _TEST = {
  13. u'url': u'http://rutube.ru/video/3eac3b4561676c17df9132a9a1e62e3e/',
  14. u'file': u'3eac3b4561676c17df9132a9a1e62e3e.mp4',
  15. u'info_dict': {
  16. u'title': u'Раненный кенгуру забежал в аптеку',
  17. u'uploader': u'NTDRussian',
  18. u'uploader_id': u'29790',
  19. },
  20. u'params': {
  21. # It requires ffmpeg (m3u8 download)
  22. u'skip_download': True,
  23. },
  24. }
  25. def _get_api_response(self, short_id, subpath):
  26. api_url = 'http://rutube.ru/api/play/%s/%s/?format=json' % (subpath, short_id)
  27. response_json = self._download_webpage(api_url, short_id,
  28. u'Downloading %s json' % subpath)
  29. return json.loads(response_json)
  30. def _real_extract(self, url):
  31. mobj = re.match(self._VALID_URL, url)
  32. long_id = mobj.group('long_id')
  33. webpage = self._download_webpage(url, long_id)
  34. og_video = self._og_search_video_url(webpage)
  35. short_id = compat_urlparse.urlparse(og_video).path[1:]
  36. options = self._get_api_response(short_id, 'options')
  37. trackinfo = self._get_api_response(short_id, 'trackinfo')
  38. # Some videos don't have the author field
  39. author = trackinfo.get('author') or {}
  40. m3u8_url = trackinfo['video_balancer'].get('m3u8')
  41. if m3u8_url is None:
  42. raise ExtractorError(u'Couldn\'t find m3u8 manifest url')
  43. return {
  44. 'id': trackinfo['id'],
  45. 'title': trackinfo['title'],
  46. 'url': m3u8_url,
  47. 'ext': 'mp4',
  48. 'thumbnail': options['thumbnail_url'],
  49. 'uploader': author.get('name'),
  50. 'uploader_id': compat_str(author['id']) if author else None,
  51. }