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