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