6 from .common import InfoExtractor
13 class IviIE(InfoExtractor):
16 _VALID_URL = r'^https?://(?:www\.)?ivi\.ru/watch(?:/(?P<compilationid>[^/]+))?/(?P<videoid>\d+)'
21 u'url': u'http://www.ivi.ru/watch/53141',
22 u'file': u'53141.mp4',
23 u'md5': u'6ff5be2254e796ed346251d117196cf4',
25 u'title': u'Иван Васильевич меняет профессию',
26 u'description': u'md5:14d8eda24e9d93d29b5857012c6d6346',
28 u'thumbnail': u'http://thumbs.ivi.ru/f20.vcp.digitalaccess.ru/contents/d/1/c3c885163a082c29bceeb7b5a267a6.jpg',
30 u'skip': u'Only works from Russia',
34 u'url': u'http://www.ivi.ru/watch/dezhurnyi_angel/74791',
35 u'file': u'74791.mp4',
36 u'md5': u'3e6cc9a848c1d2ebcc6476444967baa9',
38 u'title': u'Дежурный ангел - 1 серия',
40 u'thumbnail': u'http://thumbs.ivi.ru/f7.vcp.digitalaccess.ru/contents/8/e/bc2f6c2b6e5d291152fdd32c059141.jpg',
42 u'skip': u'Only works from Russia',
47 _known_formats = ['MP4-low-mobile', 'MP4-mobile', 'FLV-lo', 'MP4-lo', 'FLV-hi', 'MP4-hi', 'MP4-SHQ']
50 _known_thumbnails = ['Thumb-120x90', 'Thumb-160', 'Thumb-640x480']
52 def _extract_description(self, html):
53 m = re.search(r'<meta name="description" content="(?P<description>[^"]+)"/>', html)
54 return m.group('description') if m is not None else None
56 def _extract_comment_count(self, html):
57 m = re.search(u'(?s)<a href="#" id="view-comments" class="action-button dim gradient">\s*Комментарии:\s*(?P<commentcount>\d+)\s*</a>', html)
58 return int(m.group('commentcount')) if m is not None else 0
60 def _real_extract(self, url):
61 mobj = re.match(self._VALID_URL, url)
62 video_id = mobj.group('videoid')
64 api_url = 'http://api.digitalaccess.ru/api/json/'
66 data = {u'method': u'da.content.get',
67 u'params': [video_id, {u'site': u's183',
68 u'referrer': u'http://www.ivi.ru/watch/%s' % video_id,
69 u'contentid': video_id
74 request = compat_urllib_request.Request(api_url, json.dumps(data))
76 video_json_page = self._download_webpage(request, video_id, u'Downloading video JSON')
77 video_json = json.loads(video_json_page)
79 if u'error' in video_json:
80 error = video_json[u'error']
81 if error[u'origin'] == u'NoRedisValidData':
82 raise ExtractorError(u'Video %s does not exist' % video_id, expected=True)
83 raise ExtractorError(u'Unable to download video %s: %s' % (video_id, error[u'message']), expected=True)
85 result = video_json[u'result']
89 'format_id': x[u'content_format'],
90 'preference': self._known_formats.index(x[u'content_format']),
91 } for x in result[u'files'] if x[u'content_format'] in self._known_formats]
93 self._sort_formats(formats)
96 raise ExtractorError(u'No media links available for %s' % video_id)
98 duration = result[u'duration']
99 compilation = result[u'compilation']
100 title = result[u'title']
102 title = '%s - %s' % (compilation, title) if compilation is not None else title
104 previews = result[u'preview']
105 previews.sort(key=lambda fmt: self._known_thumbnails.index(fmt['content_format']))
106 thumbnail = previews[-1][u'url'] if len(previews) > 0 else None
108 video_page = self._download_webpage(url, video_id, u'Downloading video page')
109 description = self._extract_description(video_page)
110 comment_count = self._extract_comment_count(video_page)
115 'thumbnail': thumbnail,
116 'description': description,
117 'duration': duration,
118 'comment_count': comment_count,
123 class IviCompilationIE(InfoExtractor):
124 IE_DESC = u'ivi.ru compilations'
125 IE_NAME = u'ivi:compilation'
126 _VALID_URL = r'^https?://(?:www\.)?ivi\.ru/watch/(?!\d+)(?P<compilationid>[a-z\d_-]+)(?:/season(?P<seasonid>\d+))?$'
128 def _extract_entries(self, html, compilation_id):
129 return [self.url_result('http://www.ivi.ru/watch/%s/%s' % (compilation_id, serie), 'Ivi')
130 for serie in re.findall(r'<strong><a href="/watch/%s/(\d+)">(?:[^<]+)</a></strong>' % compilation_id, html)]
132 def _real_extract(self, url):
133 mobj = re.match(self._VALID_URL, url)
134 compilation_id = mobj.group('compilationid')
135 season_id = mobj.group('seasonid')
137 if season_id is not None: # Season link
138 season_page = self._download_webpage(url, compilation_id, u'Downloading season %s web page' % season_id)
139 playlist_id = '%s/season%s' % (compilation_id, season_id)
140 playlist_title = self._html_search_meta(u'title', season_page, u'title')
141 entries = self._extract_entries(season_page, compilation_id)
142 else: # Compilation link
143 compilation_page = self._download_webpage(url, compilation_id, u'Downloading compilation web page')
144 playlist_id = compilation_id
145 playlist_title = self._html_search_meta(u'title', compilation_page, u'title')
146 seasons = re.findall(r'<a href="/watch/%s/season(\d+)">[^<]+</a>' % compilation_id, compilation_page)
147 if len(seasons) == 0: # No seasons in this compilation
148 entries = self._extract_entries(compilation_page, compilation_id)
151 for season_id in seasons:
152 season_page = self._download_webpage('http://www.ivi.ru/watch/%s/season%s' % (compilation_id, season_id),
153 compilation_id, u'Downloading season %s web page' % season_id)
154 entries.extend(self._extract_entries(season_page, compilation_id))
156 return self.playlist_result(entries, playlist_id, playlist_title)