]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/smotri.py
Merge remote-tracking branch 'rzhxeo/crunchyroll'
[yt-dlp.git] / youtube_dl / extractor / smotri.py
1 # encoding: utf-8
2
3 import re
4 import json
5 import hashlib
6 import uuid
7
8 from .common import InfoExtractor
9 from ..utils import (
10 compat_urllib_parse,
11 compat_urllib_request,
12 ExtractorError,
13 )
14
15
16 class SmotriIE(InfoExtractor):
17 IE_DESC = u'Smotri.com'
18 IE_NAME = u'smotri'
19 _VALID_URL = r'^https?://(?:www\.)?(?P<url>smotri\.com/video/view/\?id=(?P<videoid>v(?P<realvideoid>[0-9]+)[a-z0-9]{4}))'
20
21 _TESTS = [
22 # real video id 2610366
23 {
24 u'url': u'http://smotri.com/video/view/?id=v261036632ab',
25 u'file': u'v261036632ab.mp4',
26 u'md5': u'2a7b08249e6f5636557579c368040eb9',
27 u'info_dict': {
28 u'title': u'катастрофа с камер видеонаблюдения',
29 u'uploader': u'rbc2008',
30 u'uploader_id': u'rbc08',
31 u'upload_date': u'20131118',
32 u'description': u'катастрофа с камер видеонаблюдения, видео катастрофа с камер видеонаблюдения',
33 u'thumbnail': u'http://frame6.loadup.ru/8b/a9/2610366.3.3.jpg',
34 },
35 },
36 # real video id 57591
37 {
38 u'url': u'http://smotri.com/video/view/?id=v57591cb20',
39 u'file': u'v57591cb20.flv',
40 u'md5': u'830266dfc21f077eac5afd1883091bcd',
41 u'info_dict': {
42 u'title': u'test',
43 u'uploader': u'Support Photofile@photofile',
44 u'uploader_id': u'support-photofile',
45 u'upload_date': u'20070704',
46 u'description': u'test, видео test',
47 u'thumbnail': u'http://frame4.loadup.ru/03/ed/57591.2.3.jpg',
48 },
49 },
50 # video-password
51 {
52 u'url': u'http://smotri.com/video/view/?id=v1390466a13c',
53 u'file': u'v1390466a13c.mp4',
54 u'md5': u'f6331cef33cad65a0815ee482a54440b',
55 u'info_dict': {
56 u'title': u'TOCCA_A_NOI_-_LE_COSE_NON_VANNO_CAMBIAMOLE_ORA-1',
57 u'uploader': u'timoxa40',
58 u'uploader_id': u'timoxa40',
59 u'upload_date': u'20100404',
60 u'thumbnail': u'http://frame7.loadup.ru/af/3f/1390466.3.3.jpg',
61 u'description': u'TOCCA_A_NOI_-_LE_COSE_NON_VANNO_CAMBIAMOLE_ORA-1, видео TOCCA_A_NOI_-_LE_COSE_NON_VANNO_CAMBIAMOLE_ORA-1',
62 },
63 u'params': {
64 u'videopassword': u'qwerty',
65 },
66 },
67 # age limit + video-password
68 {
69 u'url': u'http://smotri.com/video/view/?id=v15408898bcf',
70 u'file': u'v15408898bcf.flv',
71 u'md5': u'91e909c9f0521adf5ee86fbe073aad70',
72 u'info_dict': {
73 u'title': u'этот ролик не покажут по ТВ',
74 u'uploader': u'zzxxx',
75 u'uploader_id': u'ueggb',
76 u'upload_date': u'20101001',
77 u'thumbnail': u'http://frame3.loadup.ru/75/75/1540889.1.3.jpg',
78 u'age_limit': 18,
79 u'description': u'этот ролик не покажут по ТВ, видео этот ролик не покажут по ТВ',
80 },
81 u'params': {
82 u'videopassword': u'333'
83 }
84 }
85 ]
86
87 _SUCCESS = 0
88 _PASSWORD_NOT_VERIFIED = 1
89 _PASSWORD_DETECTED = 2
90 _VIDEO_NOT_FOUND = 3
91
92 def _search_meta(self, name, html, display_name=None):
93 if display_name is None:
94 display_name = name
95 return self._html_search_regex(
96 r'<meta itemprop="%s" content="([^"]+)" />' % re.escape(name),
97 html, display_name, fatal=False)
98 return self._html_search_meta(name, html, display_name)
99
100 def _real_extract(self, url):
101 mobj = re.match(self._VALID_URL, url)
102 video_id = mobj.group('videoid')
103 real_video_id = mobj.group('realvideoid')
104
105 # Download video JSON data
106 video_json_url = 'http://smotri.com/vt.php?id=%s' % real_video_id
107 video_json_page = self._download_webpage(video_json_url, video_id, u'Downloading video JSON')
108 video_json = json.loads(video_json_page)
109
110 status = video_json['status']
111 if status == self._VIDEO_NOT_FOUND:
112 raise ExtractorError(u'Video %s does not exist' % video_id, expected=True)
113 elif status == self._PASSWORD_DETECTED: # The video is protected by a password, retry with
114 # video-password set
115 video_password = self._downloader.params.get('videopassword', None)
116 if not video_password:
117 raise ExtractorError(u'This video is protected by a password, use the --video-password option', expected=True)
118 video_json_url += '&md5pass=%s' % hashlib.md5(video_password.encode('utf-8')).hexdigest()
119 video_json_page = self._download_webpage(video_json_url, video_id, u'Downloading video JSON (video-password set)')
120 video_json = json.loads(video_json_page)
121 status = video_json['status']
122 if status == self._PASSWORD_NOT_VERIFIED:
123 raise ExtractorError(u'Video password is invalid', expected=True)
124
125 if status != self._SUCCESS:
126 raise ExtractorError(u'Unexpected status value %s' % status)
127
128 # Extract the URL of the video
129 video_url = video_json['file_data']
130
131 # Video JSON does not provide enough meta data
132 # We will extract some from the video web page instead
133 video_page_url = 'http://' + mobj.group('url')
134 video_page = self._download_webpage(video_page_url, video_id, u'Downloading video page')
135
136 # Adult content
137 if re.search(u'EroConfirmText">', video_page) is not None:
138 self.report_age_confirmation()
139 confirm_string = self._html_search_regex(
140 r'<a href="/video/view/\?id=%s&confirm=([^"]+)" title="[^"]+">' % video_id,
141 video_page, u'confirm string')
142 confirm_url = video_page_url + '&confirm=%s' % confirm_string
143 video_page = self._download_webpage(confirm_url, video_id, u'Downloading video page (age confirmed)')
144 adult_content = True
145 else:
146 adult_content = False
147
148 # Extract the rest of meta data
149 video_title = self._search_meta(u'name', video_page, u'title')
150 if not video_title:
151 video_title = video_url.rsplit('/', 1)[-1]
152
153 video_description = self._search_meta(u'description', video_page)
154 END_TEXT = u' на сайте Smotri.com'
155 if video_description.endswith(END_TEXT):
156 video_description = video_description[:-len(END_TEXT)]
157 START_TEXT = u'Смотреть онлайн ролик '
158 if video_description.startswith(START_TEXT):
159 video_description = video_description[len(START_TEXT):]
160 video_thumbnail = self._search_meta(u'thumbnail', video_page)
161
162 upload_date_str = self._search_meta(u'uploadDate', video_page, u'upload date')
163 upload_date_m = re.search(r'(?P<year>\d{4})\.(?P<month>\d{2})\.(?P<day>\d{2})T', upload_date_str)
164 video_upload_date = (
165 (
166 upload_date_m.group('year') +
167 upload_date_m.group('month') +
168 upload_date_m.group('day')
169 )
170 if upload_date_m else None
171 )
172
173 duration_str = self._search_meta(u'duration', video_page)
174 duration_m = re.search(r'T(?P<hours>[0-9]{2})H(?P<minutes>[0-9]{2})M(?P<seconds>[0-9]{2})S', duration_str)
175 video_duration = (
176 (
177 (int(duration_m.group('hours')) * 60 * 60) +
178 (int(duration_m.group('minutes')) * 60) +
179 int(duration_m.group('seconds'))
180 )
181 if duration_m else None
182 )
183
184 video_uploader = self._html_search_regex(
185 u'<div class="DescrUser"><div>Автор.*?onmouseover="popup_user_info[^"]+">(.*?)</a>',
186 video_page, u'uploader', fatal=False, flags=re.MULTILINE|re.DOTALL)
187
188 video_uploader_id = self._html_search_regex(
189 u'<div class="DescrUser"><div>Автор.*?onmouseover="popup_user_info\\(.*?\'([^\']+)\'\\);">',
190 video_page, u'uploader id', fatal=False, flags=re.MULTILINE|re.DOTALL)
191
192 video_view_count = self._html_search_regex(
193 u'Общее количество просмотров.*?<span class="Number">(\\d+)</span>',
194 video_page, u'view count', fatal=False, flags=re.MULTILINE|re.DOTALL)
195
196 return {
197 'id': video_id,
198 'url': video_url,
199 'title': video_title,
200 'thumbnail': video_thumbnail,
201 'description': video_description,
202 'uploader': video_uploader,
203 'upload_date': video_upload_date,
204 'uploader_id': video_uploader_id,
205 'duration': video_duration,
206 'view_count': video_view_count,
207 'age_limit': 18 if adult_content else 0,
208 'video_page_url': video_page_url
209 }
210
211
212 class SmotriCommunityIE(InfoExtractor):
213 IE_DESC = u'Smotri.com community videos'
214 IE_NAME = u'smotri:community'
215 _VALID_URL = r'^https?://(?:www\.)?smotri\.com/community/video/(?P<communityid>[0-9A-Za-z_\'-]+)'
216
217 def _real_extract(self, url):
218 mobj = re.match(self._VALID_URL, url)
219 community_id = mobj.group('communityid')
220
221 url = 'http://smotri.com/export/rss/video/by/community/-/%s/video.xml' % community_id
222 rss = self._download_xml(url, community_id, u'Downloading community RSS')
223
224 entries = [self.url_result(video_url.text, 'Smotri')
225 for video_url in rss.findall('./channel/item/link')]
226
227 description_text = rss.find('./channel/description').text
228 community_title = self._html_search_regex(
229 u'^Видео сообщества "([^"]+)"$', description_text, u'community title')
230
231 return self.playlist_result(entries, community_id, community_title)
232
233
234 class SmotriUserIE(InfoExtractor):
235 IE_DESC = u'Smotri.com user videos'
236 IE_NAME = u'smotri:user'
237 _VALID_URL = r'^https?://(?:www\.)?smotri\.com/user/(?P<userid>[0-9A-Za-z_\'-]+)'
238
239 def _real_extract(self, url):
240 mobj = re.match(self._VALID_URL, url)
241 user_id = mobj.group('userid')
242
243 url = 'http://smotri.com/export/rss/user/video/-/%s/video.xml' % user_id
244 rss = self._download_xml(url, user_id, u'Downloading user RSS')
245
246 entries = [self.url_result(video_url.text, 'Smotri')
247 for video_url in rss.findall('./channel/item/link')]
248
249 description_text = rss.find('./channel/description').text
250 user_nickname = self._html_search_regex(
251 u'^Видео режиссера (.*)$', description_text,
252 u'user nickname')
253
254 return self.playlist_result(entries, user_id, user_nickname)
255
256
257 class SmotriBroadcastIE(InfoExtractor):
258 IE_DESC = u'Smotri.com broadcasts'
259 IE_NAME = u'smotri:broadcast'
260 _VALID_URL = r'^https?://(?:www\.)?(?P<url>smotri\.com/live/(?P<broadcastid>[^/]+))/?.*'
261
262 def _real_extract(self, url):
263 mobj = re.match(self._VALID_URL, url)
264 broadcast_id = mobj.group('broadcastid')
265
266 broadcast_url = 'http://' + mobj.group('url')
267 broadcast_page = self._download_webpage(broadcast_url, broadcast_id, u'Downloading broadcast page')
268
269 if re.search(u'>Режиссер с логином <br/>"%s"<br/> <span>не существует<' % broadcast_id, broadcast_page) is not None:
270 raise ExtractorError(u'Broadcast %s does not exist' % broadcast_id, expected=True)
271
272 # Adult content
273 if re.search(u'EroConfirmText">', broadcast_page) is not None:
274
275 (username, password) = self._get_login_info()
276 if username is None:
277 raise ExtractorError(u'Erotic broadcasts allowed only for registered users, '
278 u'use --username and --password options to provide account credentials.', expected=True)
279
280 # Log in
281 login_form_strs = {
282 u'login-hint53': '1',
283 u'confirm_erotic': '1',
284 u'login': username,
285 u'password': password,
286 }
287 # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
288 # chokes on unicode
289 login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k,v in login_form_strs.items())
290 login_data = compat_urllib_parse.urlencode(login_form).encode('utf-8')
291 login_url = broadcast_url + '/?no_redirect=1'
292 request = compat_urllib_request.Request(login_url, login_data)
293 request.add_header('Content-Type', 'application/x-www-form-urlencoded')
294 broadcast_page = self._download_webpage(
295 request, broadcast_id, note=u'Logging in and confirming age')
296
297 if re.search(u'>Неверный логин или пароль<', broadcast_page) is not None:
298 raise ExtractorError(u'Unable to log in: bad username or password', expected=True)
299
300 adult_content = True
301 else:
302 adult_content = False
303
304 ticket = self._html_search_regex(
305 u'window\.broadcast_control\.addFlashVar\\(\'file\', \'([^\']+)\'\\);',
306 broadcast_page, u'broadcast ticket')
307
308 url = 'http://smotri.com/broadcast/view/url/?ticket=%s' % ticket
309
310 broadcast_password = self._downloader.params.get('videopassword', None)
311 if broadcast_password:
312 url += '&pass=%s' % hashlib.md5(broadcast_password.encode('utf-8')).hexdigest()
313
314 broadcast_json_page = self._download_webpage(url, broadcast_id, u'Downloading broadcast JSON')
315
316 try:
317 broadcast_json = json.loads(broadcast_json_page)
318
319 protected_broadcast = broadcast_json['_pass_protected'] == 1
320 if protected_broadcast and not broadcast_password:
321 raise ExtractorError(u'This broadcast is protected by a password, use the --video-password option', expected=True)
322
323 broadcast_offline = broadcast_json['is_play'] == 0
324 if broadcast_offline:
325 raise ExtractorError(u'Broadcast %s is offline' % broadcast_id, expected=True)
326
327 rtmp_url = broadcast_json['_server']
328 if not rtmp_url.startswith('rtmp://'):
329 raise ExtractorError(u'Unexpected broadcast rtmp URL')
330
331 broadcast_playpath = broadcast_json['_streamName']
332 broadcast_thumbnail = broadcast_json['_imgURL']
333 broadcast_title = broadcast_json['title']
334 broadcast_description = broadcast_json['description']
335 broadcaster_nick = broadcast_json['nick']
336 broadcaster_login = broadcast_json['login']
337 rtmp_conn = 'S:%s' % uuid.uuid4().hex
338 except KeyError:
339 if protected_broadcast:
340 raise ExtractorError(u'Bad broadcast password', expected=True)
341 raise ExtractorError(u'Unexpected broadcast JSON')
342
343 return {
344 'id': broadcast_id,
345 'url': rtmp_url,
346 'title': broadcast_title,
347 'thumbnail': broadcast_thumbnail,
348 'description': broadcast_description,
349 'uploader': broadcaster_nick,
350 'uploader_id': broadcaster_login,
351 'age_limit': 18 if adult_content else 0,
352 'ext': 'flv',
353 'play_path': broadcast_playpath,
354 'rtmp_live': True,
355 'rtmp_conn': rtmp_conn
356 }