]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/smotri.py
[smotri] Fix password protected video extraction
[yt-dlp.git] / youtube_dl / extractor / smotri.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import json
6 import hashlib
7 import uuid
8
9 from .common import InfoExtractor
10 from ..compat import (
11 compat_urllib_parse,
12 compat_urllib_request,
13 )
14 from ..utils import (
15 ExtractorError,
16 int_or_none,
17 unified_strdate,
18 )
19
20
21 class SmotriIE(InfoExtractor):
22 IE_DESC = 'Smotri.com'
23 IE_NAME = 'smotri'
24 _VALID_URL = r'^https?://(?:www\.)?(?:smotri\.com/video/view/\?id=|pics\.smotri\.com/(?:player|scrubber_custom8)\.swf\?file=)(?P<id>v(?P<realvideoid>[0-9]+)[a-z0-9]{4})'
25 _NETRC_MACHINE = 'smotri'
26
27 _TESTS = [
28 # real video id 2610366
29 {
30 'url': 'http://smotri.com/video/view/?id=v261036632ab',
31 'md5': '2a7b08249e6f5636557579c368040eb9',
32 'info_dict': {
33 'id': 'v261036632ab',
34 'ext': 'mp4',
35 'title': 'катастрофа с камер видеонаблюдения',
36 'uploader': 'rbc2008',
37 'uploader_id': 'rbc08',
38 'upload_date': '20131118',
39 'thumbnail': 'http://frame6.loadup.ru/8b/a9/2610366.3.3.jpg',
40 },
41 },
42 # real video id 57591
43 {
44 'url': 'http://smotri.com/video/view/?id=v57591cb20',
45 'md5': '830266dfc21f077eac5afd1883091bcd',
46 'info_dict': {
47 'id': 'v57591cb20',
48 'ext': 'flv',
49 'title': 'test',
50 'uploader': 'Support Photofile@photofile',
51 'uploader_id': 'support-photofile',
52 'upload_date': '20070704',
53 'thumbnail': 'http://frame4.loadup.ru/03/ed/57591.2.3.jpg',
54 },
55 },
56 # video-password
57 {
58 'url': 'http://smotri.com/video/view/?id=v1390466a13c',
59 'md5': 'f6331cef33cad65a0815ee482a54440b',
60 'info_dict': {
61 'id': 'v1390466a13c',
62 'ext': 'mp4',
63 'title': 'TOCCA_A_NOI_-_LE_COSE_NON_VANNO_CAMBIAMOLE_ORA-1',
64 'uploader': 'timoxa40',
65 'uploader_id': 'timoxa40',
66 'upload_date': '20100404',
67 'thumbnail': 'http://frame7.loadup.ru/af/3f/1390466.3.3.jpg',
68 },
69 'params': {
70 'videopassword': 'qwerty',
71 },
72 'skip': 'Video is not approved by moderator',
73 },
74 # age limit + video-password
75 {
76 'url': 'http://smotri.com/video/view/?id=v15408898bcf',
77 'md5': '91e909c9f0521adf5ee86fbe073aad70',
78 'info_dict': {
79 'id': 'v15408898bcf',
80 'ext': 'flv',
81 'title': 'этот ролик не покажут по ТВ',
82 'uploader': 'zzxxx',
83 'uploader_id': 'ueggb',
84 'upload_date': '20101001',
85 'thumbnail': 'http://frame3.loadup.ru/75/75/1540889.1.3.jpg',
86 'age_limit': 18,
87 },
88 'params': {
89 'videopassword': '333'
90 },
91 'skip': 'Video is not approved by moderator',
92 },
93 # not approved by moderator, but available
94 {
95 'url': 'http://smotri.com/video/view/?id=v28888533b73',
96 'md5': 'f44bc7adac90af518ef1ecf04893bb34',
97 'info_dict': {
98 'id': 'v28888533b73',
99 'ext': 'mp4',
100 'title': 'Russian Spies Killed By ISIL Child Soldier',
101 'uploader': 'Mopeder',
102 'uploader_id': 'mopeder',
103 'duration': 71,
104 'thumbnail': 'http://frame9.loadup.ru/d7/32/2888853.2.3.jpg',
105 'upload_date': '20150114',
106 },
107 },
108 # swf player
109 {
110 'url': 'http://pics.smotri.com/scrubber_custom8.swf?file=v9188090500',
111 'md5': '31099eeb4bc906712c5f40092045108d',
112 'info_dict': {
113 'id': 'v9188090500',
114 'ext': 'mp4',
115 'title': 'Shakira - Don\'t Bother',
116 'uploader': 'HannahL',
117 'uploader_id': 'lisaha95',
118 'upload_date': '20090331',
119 'thumbnail': 'http://frame8.loadup.ru/44/0b/918809.7.3.jpg',
120 },
121 },
122 ]
123
124 @classmethod
125 def _extract_url(cls, webpage):
126 mobj = re.search(
127 r'<embed[^>]src=(["\'])(?P<url>http://pics\.smotri\.com/(?:player|scrubber_custom8)\.swf\?file=v.+?\1)',
128 webpage)
129 if mobj is not None:
130 return mobj.group('url')
131
132 mobj = re.search(
133 r'''(?x)<div\s+class="video_file">http://smotri\.com/video/download/file/[^<]+</div>\s*
134 <div\s+class="video_image">[^<]+</div>\s*
135 <div\s+class="video_id">(?P<id>[^<]+)</div>''', webpage)
136 if mobj is not None:
137 return 'http://smotri.com/video/view/?id=%s' % mobj.group('id')
138
139 def _search_meta(self, name, html, display_name=None):
140 if display_name is None:
141 display_name = name
142 return self._html_search_meta(name, html, display_name)
143
144 def _real_extract(self, url):
145 video_id = self._match_id(url)
146
147 video_form = {
148 'ticket': video_id,
149 'video_url': '1',
150 'frame_url': '1',
151 'devid': 'LoadupFlashPlayer',
152 'getvideoinfo': '1',
153 }
154
155 video_password = self._downloader.params.get('videopassword', None)
156 if video_password:
157 video_form['pass'] = hashlib.md5(video_password.encode('utf-8')).hexdigest()
158
159 request = compat_urllib_request.Request(
160 'http://smotri.com/video/view/url/bot/', compat_urllib_parse.urlencode(video_form))
161 request.add_header('Content-Type', 'application/x-www-form-urlencoded')
162
163 video = self._download_json(request, video_id, 'Downloading video JSON')
164
165 video_url = video.get('_vidURL') or video.get('_vidURL_mp4')
166
167 if not video_url:
168 if video.get('_moderate_no'):
169 raise ExtractorError(
170 'Video %s has not been approved by moderator' % video_id, expected=True)
171
172 if video.get('error'):
173 raise ExtractorError('Video %s does not exist' % video_id, expected=True)
174
175 if video.get('_pass_protected') == 1:
176 msg = ('Invalid video password' if video_password
177 else 'This video is protected by a password, use the --video-password option')
178 raise ExtractorError(msg, expected=True)
179
180 title = video['title']
181 thumbnail = video['_imgURL']
182 upload_date = unified_strdate(video['added'])
183 uploader = video['userNick']
184 uploader_id = video['userLogin']
185 duration = int_or_none(video['duration'])
186
187 # Video JSON does not provide enough meta data
188 # We will extract some from the video web page instead
189 webpage_url = 'http://smotri.com/video/view/?id=%s' % video_id
190 webpage = self._download_webpage(webpage_url, video_id, 'Downloading video page')
191
192 # Warning if video is unavailable
193 warning = self._html_search_regex(
194 r'<div class="videoUnModer">(.*?)</div>', webpage,
195 'warning message', default=None)
196 if warning is not None:
197 self._downloader.report_warning(
198 'Video %s may not be available; smotri said: %s ' %
199 (video_id, warning))
200
201 # Adult content
202 if re.search('EroConfirmText">', webpage) is not None:
203 self.report_age_confirmation()
204 confirm_string = self._html_search_regex(
205 r'<a href="/video/view/\?id=%s&confirm=([^"]+)" title="[^"]+">' % video_id,
206 webpage, 'confirm string')
207 confirm_url = webpage_url + '&confirm=%s' % confirm_string
208 webpage = self._download_webpage(confirm_url, video_id, 'Downloading video page (age confirmed)')
209 adult_content = True
210 else:
211 adult_content = False
212
213 view_count = self._html_search_regex(
214 'Общее количество просмотров.*?<span class="Number">(\\d+)</span>',
215 webpage, 'view count', fatal=False, flags=re.MULTILINE | re.DOTALL)
216
217 return {
218 'id': video_id,
219 'url': video_url,
220 'title': title,
221 'thumbnail': thumbnail,
222 'uploader': uploader,
223 'upload_date': upload_date,
224 'uploader_id': uploader_id,
225 'duration': duration,
226 'view_count': int_or_none(view_count),
227 'age_limit': 18 if adult_content else 0,
228 }
229
230
231 class SmotriCommunityIE(InfoExtractor):
232 IE_DESC = 'Smotri.com community videos'
233 IE_NAME = 'smotri:community'
234 _VALID_URL = r'^https?://(?:www\.)?smotri\.com/community/video/(?P<communityid>[0-9A-Za-z_\'-]+)'
235 _TEST = {
236 'url': 'http://smotri.com/community/video/kommuna',
237 'info_dict': {
238 'id': 'kommuna',
239 'title': 'КПРФ',
240 },
241 'playlist_mincount': 4,
242 }
243
244 def _real_extract(self, url):
245 mobj = re.match(self._VALID_URL, url)
246 community_id = mobj.group('communityid')
247
248 url = 'http://smotri.com/export/rss/video/by/community/-/%s/video.xml' % community_id
249 rss = self._download_xml(url, community_id, 'Downloading community RSS')
250
251 entries = [self.url_result(video_url.text, 'Smotri')
252 for video_url in rss.findall('./channel/item/link')]
253
254 description_text = rss.find('./channel/description').text
255 community_title = self._html_search_regex(
256 '^Видео сообщества "([^"]+)"$', description_text, 'community title')
257
258 return self.playlist_result(entries, community_id, community_title)
259
260
261 class SmotriUserIE(InfoExtractor):
262 IE_DESC = 'Smotri.com user videos'
263 IE_NAME = 'smotri:user'
264 _VALID_URL = r'^https?://(?:www\.)?smotri\.com/user/(?P<userid>[0-9A-Za-z_\'-]+)'
265 _TESTS = [{
266 'url': 'http://smotri.com/user/inspector',
267 'info_dict': {
268 'id': 'inspector',
269 'title': 'Inspector',
270 },
271 'playlist_mincount': 9,
272 }]
273
274 def _real_extract(self, url):
275 mobj = re.match(self._VALID_URL, url)
276 user_id = mobj.group('userid')
277
278 url = 'http://smotri.com/export/rss/user/video/-/%s/video.xml' % user_id
279 rss = self._download_xml(url, user_id, 'Downloading user RSS')
280
281 entries = [self.url_result(video_url.text, 'Smotri')
282 for video_url in rss.findall('./channel/item/link')]
283
284 description_text = rss.find('./channel/description').text
285 user_nickname = self._html_search_regex(
286 '^Видео режиссера (.*)$', description_text,
287 'user nickname')
288
289 return self.playlist_result(entries, user_id, user_nickname)
290
291
292 class SmotriBroadcastIE(InfoExtractor):
293 IE_DESC = 'Smotri.com broadcasts'
294 IE_NAME = 'smotri:broadcast'
295 _VALID_URL = r'^https?://(?:www\.)?(?P<url>smotri\.com/live/(?P<broadcastid>[^/]+))/?.*'
296
297 def _real_extract(self, url):
298 mobj = re.match(self._VALID_URL, url)
299 broadcast_id = mobj.group('broadcastid')
300
301 broadcast_url = 'http://' + mobj.group('url')
302 broadcast_page = self._download_webpage(broadcast_url, broadcast_id, 'Downloading broadcast page')
303
304 if re.search('>Режиссер с логином <br/>"%s"<br/> <span>не существует<' % broadcast_id, broadcast_page) is not None:
305 raise ExtractorError(
306 'Broadcast %s does not exist' % broadcast_id, expected=True)
307
308 # Adult content
309 if re.search('EroConfirmText">', broadcast_page) is not None:
310
311 (username, password) = self._get_login_info()
312 if username is None:
313 raise ExtractorError(
314 'Erotic broadcasts allowed only for registered users, '
315 'use --username and --password options to provide account credentials.',
316 expected=True)
317
318 login_form = {
319 'login-hint53': '1',
320 'confirm_erotic': '1',
321 'login': username,
322 'password': password,
323 }
324
325 request = compat_urllib_request.Request(
326 broadcast_url + '/?no_redirect=1', compat_urllib_parse.urlencode(login_form))
327 request.add_header('Content-Type', 'application/x-www-form-urlencoded')
328 broadcast_page = self._download_webpage(
329 request, broadcast_id, 'Logging in and confirming age')
330
331 if re.search('>Неверный логин или пароль<', broadcast_page) is not None:
332 raise ExtractorError('Unable to log in: bad username or password', expected=True)
333
334 adult_content = True
335 else:
336 adult_content = False
337
338 ticket = self._html_search_regex(
339 r"window\.broadcast_control\.addFlashVar\('file'\s*,\s*'([^']+)'\)",
340 broadcast_page, 'broadcast ticket')
341
342 url = 'http://smotri.com/broadcast/view/url/?ticket=%s' % ticket
343
344 broadcast_password = self._downloader.params.get('videopassword', None)
345 if broadcast_password:
346 url += '&pass=%s' % hashlib.md5(broadcast_password.encode('utf-8')).hexdigest()
347
348 broadcast_json_page = self._download_webpage(
349 url, broadcast_id, 'Downloading broadcast JSON')
350
351 try:
352 broadcast_json = json.loads(broadcast_json_page)
353
354 protected_broadcast = broadcast_json['_pass_protected'] == 1
355 if protected_broadcast and not broadcast_password:
356 raise ExtractorError(
357 'This broadcast is protected by a password, use the --video-password option',
358 expected=True)
359
360 broadcast_offline = broadcast_json['is_play'] == 0
361 if broadcast_offline:
362 raise ExtractorError('Broadcast %s is offline' % broadcast_id, expected=True)
363
364 rtmp_url = broadcast_json['_server']
365 mobj = re.search(r'^rtmp://[^/]+/(?P<app>.+)/?$', rtmp_url)
366 if not mobj:
367 raise ExtractorError('Unexpected broadcast rtmp URL')
368
369 broadcast_playpath = broadcast_json['_streamName']
370 broadcast_app = '%s/%s' % (mobj.group('app'), broadcast_json['_vidURL'])
371 broadcast_thumbnail = broadcast_json['_imgURL']
372 broadcast_title = self._live_title(broadcast_json['title'])
373 broadcast_description = broadcast_json['description']
374 broadcaster_nick = broadcast_json['nick']
375 broadcaster_login = broadcast_json['login']
376 rtmp_conn = 'S:%s' % uuid.uuid4().hex
377 except KeyError:
378 if protected_broadcast:
379 raise ExtractorError('Bad broadcast password', expected=True)
380 raise ExtractorError('Unexpected broadcast JSON')
381
382 return {
383 'id': broadcast_id,
384 'url': rtmp_url,
385 'title': broadcast_title,
386 'thumbnail': broadcast_thumbnail,
387 'description': broadcast_description,
388 'uploader': broadcaster_nick,
389 'uploader_id': broadcaster_login,
390 'age_limit': 18 if adult_content else 0,
391 'ext': 'flv',
392 'play_path': broadcast_playpath,
393 'player_url': 'http://pics.smotri.com/broadcast_play.swf',
394 'app': broadcast_app,
395 'rtmp_live': True,
396 'rtmp_conn': rtmp_conn,
397 'is_live': True,
398 }