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