35a81ee87fda041cb8208793e3f4fe34d3445cfd
[youtube-dl] / 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, not approved by moderator
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         # video-password
75         {
76             'url': 'http://smotri.com/video/view/?id=v6984858774#',
77             'md5': 'f11e01d13ac676370fc3b95b9bda11b0',
78             'info_dict': {
79                 'id': 'v6984858774',
80                 'ext': 'mp4',
81                 'title': 'Дача Солженицина ПАРОЛЬ 223322',
82                 'uploader': 'psavari1',
83                 'uploader_id': 'psavari1',
84                 'upload_date': '20081103',
85                 'thumbnail': 're:^https?://.*\.jpg$',
86             },
87             'params': {
88                 'videopassword': '223322',
89             },
90         },
91         # age limit + video-password, not approved by moderator
92         {
93             'url': 'http://smotri.com/video/view/?id=v15408898bcf',
94             'md5': '91e909c9f0521adf5ee86fbe073aad70',
95             'info_dict': {
96                 'id': 'v15408898bcf',
97                 'ext': 'flv',
98                 'title': 'этот ролик не покажут по ТВ',
99                 'uploader': 'zzxxx',
100                 'uploader_id': 'ueggb',
101                 'upload_date': '20101001',
102                 'thumbnail': 'http://frame3.loadup.ru/75/75/1540889.1.3.jpg',
103                 'age_limit': 18,
104             },
105             'params': {
106                 'videopassword': '333'
107             },
108             'skip': 'Video is not approved by moderator',
109         },
110         # age limit + video-password
111         {
112             'url': 'http://smotri.com/video/view/?id=v7780025814',
113             'md5': 'b4599b068422559374a59300c5337d72',
114             'info_dict': {
115                 'id': 'v7780025814',
116                 'ext': 'mp4',
117                 'title': 'Sexy Beach (пароль 123)',
118                 'uploader': 'вАся',
119                 'uploader_id': 'asya_prosto',
120                 'upload_date': '20081218',
121                 'thumbnail': 're:^https?://.*\.jpg$',
122                 'age_limit': 18,
123             },
124             'params': {
125                 'videopassword': '123'
126             },
127         },
128         # swf player
129         {
130             'url': 'http://pics.smotri.com/scrubber_custom8.swf?file=v9188090500',
131             'md5': '31099eeb4bc906712c5f40092045108d',
132             'info_dict': {
133                 'id': 'v9188090500',
134                 'ext': 'mp4',
135                 'title': 'Shakira - Don\'t Bother',
136                 'uploader': 'HannahL',
137                 'uploader_id': 'lisaha95',
138                 'upload_date': '20090331',
139                 'thumbnail': 'http://frame8.loadup.ru/44/0b/918809.7.3.jpg',
140             },
141         },
142     ]
143
144     @classmethod
145     def _extract_url(cls, webpage):
146         mobj = re.search(
147             r'<embed[^>]src=(["\'])(?P<url>http://pics\.smotri\.com/(?:player|scrubber_custom8)\.swf\?file=v.+?\1)',
148             webpage)
149         if mobj is not None:
150             return mobj.group('url')
151
152         mobj = re.search(
153             r'''(?x)<div\s+class="video_file">http://smotri\.com/video/download/file/[^<]+</div>\s*
154                     <div\s+class="video_image">[^<]+</div>\s*
155                     <div\s+class="video_id">(?P<id>[^<]+)</div>''', webpage)
156         if mobj is not None:
157             return 'http://smotri.com/video/view/?id=%s' % mobj.group('id')
158
159     def _search_meta(self, name, html, display_name=None):
160         if display_name is None:
161             display_name = name
162         return self._html_search_meta(name, html, display_name)
163
164     def _real_extract(self, url):
165         video_id = self._match_id(url)
166
167         video_form = {
168             'ticket': video_id,
169             'video_url': '1',
170             'frame_url': '1',
171             'devid': 'LoadupFlashPlayer',
172             'getvideoinfo': '1',
173         }
174
175         video_password = self._downloader.params.get('videopassword', None)
176         if video_password:
177             video_form['pass'] = hashlib.md5(video_password.encode('utf-8')).hexdigest()
178
179         request = compat_urllib_request.Request(
180             'http://smotri.com/video/view/url/bot/', compat_urllib_parse.urlencode(video_form))
181         request.add_header('Content-Type', 'application/x-www-form-urlencoded')
182
183         video = self._download_json(request, video_id, 'Downloading video JSON')
184
185         video_url = video.get('_vidURL') or video.get('_vidURL_mp4')
186
187         if not video_url:
188             if video.get('_moderate_no'):
189                 raise ExtractorError(
190                     'Video %s has not been approved by moderator' % video_id, expected=True)
191
192             if video.get('error'):
193                 raise ExtractorError('Video %s does not exist' % video_id, expected=True)
194
195             if video.get('_pass_protected') == 1:
196                 msg = ('Invalid video password' if video_password
197                        else 'This video is protected by a password, use the --video-password option')
198                 raise ExtractorError(msg, expected=True)
199
200         title = video['title']
201         thumbnail = video['_imgURL']
202         upload_date = unified_strdate(video['added'])
203         uploader = video['userNick']
204         uploader_id = video['userLogin']
205         duration = int_or_none(video['duration'])
206
207         # Video JSON does not provide enough meta data
208         # We will extract some from the video web page instead
209         webpage_url = 'http://smotri.com/video/view/?id=%s' % video_id
210         webpage = self._download_webpage(webpage_url, video_id, 'Downloading video page')
211
212         # Warning if video is unavailable
213         warning = self._html_search_regex(
214             r'<div class="videoUnModer">(.*?)</div>', webpage,
215             'warning message', default=None)
216         if warning is not None:
217             self._downloader.report_warning(
218                 'Video %s may not be available; smotri said: %s ' %
219                 (video_id, warning))
220
221         # Adult content
222         if re.search('EroConfirmText">', webpage) is not None:
223             self.report_age_confirmation()
224             confirm_string = self._html_search_regex(
225                 r'<a href="/video/view/\?id=%s&confirm=([^"]+)" title="[^"]+">' % video_id,
226                 webpage, 'confirm string')
227             confirm_url = webpage_url + '&confirm=%s' % confirm_string
228             webpage = self._download_webpage(confirm_url, video_id, 'Downloading video page (age confirmed)')
229             adult_content = True
230         else:
231             adult_content = False
232
233         view_count = self._html_search_regex(
234             'Общее количество просмотров.*?<span class="Number">(\\d+)</span>',
235             webpage, 'view count', fatal=False, flags=re.MULTILINE | re.DOTALL)
236
237         return {
238             'id': video_id,
239             'url': video_url,
240             'title': title,
241             'thumbnail': thumbnail,
242             'uploader': uploader,
243             'upload_date': upload_date,
244             'uploader_id': uploader_id,
245             'duration': duration,
246             'view_count': int_or_none(view_count),
247             'age_limit': 18 if adult_content else 0,
248         }
249
250
251 class SmotriCommunityIE(InfoExtractor):
252     IE_DESC = 'Smotri.com community videos'
253     IE_NAME = 'smotri:community'
254     _VALID_URL = r'^https?://(?:www\.)?smotri\.com/community/video/(?P<communityid>[0-9A-Za-z_\'-]+)'
255     _TEST = {
256         'url': 'http://smotri.com/community/video/kommuna',
257         'info_dict': {
258             'id': 'kommuna',
259             'title': 'КПРФ',
260         },
261         'playlist_mincount': 4,
262     }
263
264     def _real_extract(self, url):
265         mobj = re.match(self._VALID_URL, url)
266         community_id = mobj.group('communityid')
267
268         url = 'http://smotri.com/export/rss/video/by/community/-/%s/video.xml' % community_id
269         rss = self._download_xml(url, community_id, 'Downloading community RSS')
270
271         entries = [self.url_result(video_url.text, 'Smotri')
272                    for video_url in rss.findall('./channel/item/link')]
273
274         description_text = rss.find('./channel/description').text
275         community_title = self._html_search_regex(
276             '^Видео сообщества "([^"]+)"$', description_text, 'community title')
277
278         return self.playlist_result(entries, community_id, community_title)
279
280
281 class SmotriUserIE(InfoExtractor):
282     IE_DESC = 'Smotri.com user videos'
283     IE_NAME = 'smotri:user'
284     _VALID_URL = r'^https?://(?:www\.)?smotri\.com/user/(?P<userid>[0-9A-Za-z_\'-]+)'
285     _TESTS = [{
286         'url': 'http://smotri.com/user/inspector',
287         'info_dict': {
288             'id': 'inspector',
289             'title': 'Inspector',
290         },
291         'playlist_mincount': 9,
292     }]
293
294     def _real_extract(self, url):
295         mobj = re.match(self._VALID_URL, url)
296         user_id = mobj.group('userid')
297
298         url = 'http://smotri.com/export/rss/user/video/-/%s/video.xml' % user_id
299         rss = self._download_xml(url, user_id, 'Downloading user RSS')
300
301         entries = [self.url_result(video_url.text, 'Smotri')
302                    for video_url in rss.findall('./channel/item/link')]
303
304         description_text = rss.find('./channel/description').text
305         user_nickname = self._html_search_regex(
306             '^Видео режиссера (.*)$', description_text,
307             'user nickname')
308
309         return self.playlist_result(entries, user_id, user_nickname)
310
311
312 class SmotriBroadcastIE(InfoExtractor):
313     IE_DESC = 'Smotri.com broadcasts'
314     IE_NAME = 'smotri:broadcast'
315     _VALID_URL = r'^https?://(?:www\.)?(?P<url>smotri\.com/live/(?P<broadcastid>[^/]+))/?.*'
316
317     def _real_extract(self, url):
318         mobj = re.match(self._VALID_URL, url)
319         broadcast_id = mobj.group('broadcastid')
320
321         broadcast_url = 'http://' + mobj.group('url')
322         broadcast_page = self._download_webpage(broadcast_url, broadcast_id, 'Downloading broadcast page')
323
324         if re.search('>Режиссер с логином <br/>"%s"<br/> <span>не существует<' % broadcast_id, broadcast_page) is not None:
325             raise ExtractorError(
326                 'Broadcast %s does not exist' % broadcast_id, expected=True)
327
328         # Adult content
329         if re.search('EroConfirmText">', broadcast_page) is not None:
330
331             (username, password) = self._get_login_info()
332             if username is None:
333                 self.raise_login_required('Erotic broadcasts allowed only for registered users')
334
335             login_form = {
336                 'login-hint53': '1',
337                 'confirm_erotic': '1',
338                 'login': username,
339                 'password': password,
340             }
341
342             request = compat_urllib_request.Request(
343                 broadcast_url + '/?no_redirect=1', compat_urllib_parse.urlencode(login_form))
344             request.add_header('Content-Type', 'application/x-www-form-urlencoded')
345             broadcast_page = self._download_webpage(
346                 request, broadcast_id, 'Logging in and confirming age')
347
348             if re.search('>Неверный логин или пароль<', broadcast_page) is not None:
349                 raise ExtractorError('Unable to log in: bad username or password', expected=True)
350
351             adult_content = True
352         else:
353             adult_content = False
354
355         ticket = self._html_search_regex(
356             r"window\.broadcast_control\.addFlashVar\('file'\s*,\s*'([^']+)'\)",
357             broadcast_page, 'broadcast ticket')
358
359         url = 'http://smotri.com/broadcast/view/url/?ticket=%s' % ticket
360
361         broadcast_password = self._downloader.params.get('videopassword', None)
362         if broadcast_password:
363             url += '&pass=%s' % hashlib.md5(broadcast_password.encode('utf-8')).hexdigest()
364
365         broadcast_json_page = self._download_webpage(
366             url, broadcast_id, 'Downloading broadcast JSON')
367
368         try:
369             broadcast_json = json.loads(broadcast_json_page)
370
371             protected_broadcast = broadcast_json['_pass_protected'] == 1
372             if protected_broadcast and not broadcast_password:
373                 raise ExtractorError(
374                     'This broadcast is protected by a password, use the --video-password option',
375                     expected=True)
376
377             broadcast_offline = broadcast_json['is_play'] == 0
378             if broadcast_offline:
379                 raise ExtractorError('Broadcast %s is offline' % broadcast_id, expected=True)
380
381             rtmp_url = broadcast_json['_server']
382             mobj = re.search(r'^rtmp://[^/]+/(?P<app>.+)/?$', rtmp_url)
383             if not mobj:
384                 raise ExtractorError('Unexpected broadcast rtmp URL')
385
386             broadcast_playpath = broadcast_json['_streamName']
387             broadcast_app = '%s/%s' % (mobj.group('app'), broadcast_json['_vidURL'])
388             broadcast_thumbnail = broadcast_json['_imgURL']
389             broadcast_title = self._live_title(broadcast_json['title'])
390             broadcast_description = broadcast_json['description']
391             broadcaster_nick = broadcast_json['nick']
392             broadcaster_login = broadcast_json['login']
393             rtmp_conn = 'S:%s' % uuid.uuid4().hex
394         except KeyError:
395             if protected_broadcast:
396                 raise ExtractorError('Bad broadcast password', expected=True)
397             raise ExtractorError('Unexpected broadcast JSON')
398
399         return {
400             'id': broadcast_id,
401             'url': rtmp_url,
402             'title': broadcast_title,
403             'thumbnail': broadcast_thumbnail,
404             'description': broadcast_description,
405             'uploader': broadcaster_nick,
406             'uploader_id': broadcaster_login,
407             'age_limit': 18 if adult_content else 0,
408             'ext': 'flv',
409             'play_path': broadcast_playpath,
410             'player_url': 'http://pics.smotri.com/broadcast_play.swf',
411             'app': broadcast_app,
412             'rtmp_live': True,
413             'rtmp_conn': rtmp_conn,
414             'is_live': True,
415         }