]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/smotri.py
f035a3214c60461ff2c35ee14faf159d4c6d44f5
[youtubedl] / youtube_dl / extractor / smotri.py
1 # encoding: utf-8
2
3 import re
4 import json
5 import hashlib
6
7 from .common import InfoExtractor
8 from ..utils import (
9 determine_ext,
10 ExtractorError
11 )
12
13
14 class SmotriIE(InfoExtractor):
15 IE_DESC = u'Smotri.com'
16 IE_NAME = u'smotri'
17 _VALID_URL = r'^https?://(?:www\.)?(?P<url>smotri\.com/video/view/\?id=(?P<videoid>v(?P<realvideoid>[0-9]+)[a-z0-9]{4}))'
18
19 _TESTS = [
20 # real video id 2610366
21 {
22 u'url': u'http://smotri.com/video/view/?id=v261036632ab',
23 u'file': u'v261036632ab.mp4',
24 u'md5': u'2a7b08249e6f5636557579c368040eb9',
25 u'info_dict': {
26 u'title': u'катастрофа с камер видеонаблюдения',
27 u'uploader': u'rbc2008',
28 u'uploader_id': u'rbc08',
29 u'upload_date': u'20131118',
30 u'description': u'катастрофа с камер видеонаблюдения, видео катастрофа с камер видеонаблюдения',
31 u'thumbnail': u'http://frame6.loadup.ru/8b/a9/2610366.3.3.jpg',
32 },
33 },
34 # real video id 57591
35 {
36 u'url': u'http://smotri.com/video/view/?id=v57591cb20',
37 u'file': u'v57591cb20.flv',
38 u'md5': u'830266dfc21f077eac5afd1883091bcd',
39 u'info_dict': {
40 u'title': u'test',
41 u'uploader': u'Support Photofile@photofile',
42 u'uploader_id': u'support-photofile',
43 u'upload_date': u'20070704',
44 u'description': u'test, видео test',
45 u'thumbnail': u'http://frame4.loadup.ru/03/ed/57591.2.3.jpg',
46 },
47 },
48 # video-password
49 {
50 u'url': u'http://smotri.com/video/view/?id=v1390466a13c',
51 u'file': u'v1390466a13c.mp4',
52 u'md5': u'f6331cef33cad65a0815ee482a54440b',
53 u'info_dict': {
54 u'title': u'TOCCA_A_NOI_-_LE_COSE_NON_VANNO_CAMBIAMOLE_ORA-1',
55 u'uploader': u'timoxa40',
56 u'uploader_id': u'timoxa40',
57 u'upload_date': u'20100404',
58 u'thumbnail': u'http://frame7.loadup.ru/af/3f/1390466.3.3.jpg',
59 u'description': u'TOCCA_A_NOI_-_LE_COSE_NON_VANNO_CAMBIAMOLE_ORA-1, видео TOCCA_A_NOI_-_LE_COSE_NON_VANNO_CAMBIAMOLE_ORA-1',
60 },
61 u'params': {
62 u'videopassword': u'qwerty',
63 },
64 },
65 # age limit + video-password
66 {
67 u'url': u'http://smotri.com/video/view/?id=v15408898bcf',
68 u'file': u'v15408898bcf.flv',
69 u'md5': u'91e909c9f0521adf5ee86fbe073aad70',
70 u'info_dict': {
71 u'title': u'этот ролик не покажут по ТВ',
72 u'uploader': u'zzxxx',
73 u'uploader_id': u'ueggb',
74 u'upload_date': u'20101001',
75 u'thumbnail': u'http://frame3.loadup.ru/75/75/1540889.1.3.jpg',
76 u'age_limit': 18,
77 u'description': u'этот ролик не покажут по ТВ, видео этот ролик не покажут по ТВ',
78 },
79 u'params': {
80 u'videopassword': u'333'
81 }
82 }
83 ]
84
85 _SUCCESS = 0
86 _PASSWORD_NOT_VERIFIED = 1
87 _PASSWORD_DETECTED = 2
88 _VIDEO_NOT_FOUND = 3
89
90 def _search_meta(self, name, html, display_name=None):
91 if display_name is None:
92 display_name = name
93 return self._html_search_regex(
94 r'<meta itemprop="%s" content="([^"]+)" />' % re.escape(name),
95 html, display_name, fatal=False)
96 return self._html_search_meta(name, html, display_name)
97
98 def _real_extract(self, url):
99 mobj = re.match(self._VALID_URL, url)
100 video_id = mobj.group('videoid')
101 real_video_id = mobj.group('realvideoid')
102
103 # Download video JSON data
104 video_json_url = 'http://smotri.com/vt.php?id=%s' % real_video_id
105 video_json_page = self._download_webpage(video_json_url, video_id, u'Downloading video JSON')
106 video_json = json.loads(video_json_page)
107
108 status = video_json['status']
109 if status == self._VIDEO_NOT_FOUND:
110 raise ExtractorError(u'Video %s does not exist' % video_id, expected=True)
111 elif status == self._PASSWORD_DETECTED: # The video is protected by a password, retry with
112 # video-password set
113 video_password = self._downloader.params.get('videopassword', None)
114 if not video_password:
115 raise ExtractorError(u'This video is protected by a password, use the --video-password option', expected=True)
116 video_json_url += '&md5pass=%s' % hashlib.md5(video_password.encode('utf-8')).hexdigest()
117 video_json_page = self._download_webpage(video_json_url, video_id, u'Downloading video JSON (video-password set)')
118 video_json = json.loads(video_json_page)
119 status = video_json['status']
120 if status == self._PASSWORD_NOT_VERIFIED:
121 raise ExtractorError(u'Video password is invalid', expected=True)
122
123 if status != self._SUCCESS:
124 raise ExtractorError(u'Unexpected status value %s' % status)
125
126 # Extract the URL of the video
127 video_url = video_json['file_data']
128
129 # Video JSON does not provide enough meta data
130 # We will extract some from the video web page instead
131 video_page_url = 'http://' + mobj.group('url')
132 video_page = self._download_webpage(video_page_url, video_id, u'Downloading video page')
133
134 # Adult content
135 if re.search(u'EroConfirmText">', video_page) is not None:
136 self.report_age_confirmation()
137 confirm_string = self._html_search_regex(
138 r'<a href="/video/view/\?id=%s&confirm=([^"]+)" title="[^"]+">' % video_id,
139 video_page, u'confirm string')
140 confirm_url = video_page_url + '&confirm=%s' % confirm_string
141 video_page = self._download_webpage(confirm_url, video_id, u'Downloading video page (age confirmed)')
142 adult_content = True
143 else:
144 adult_content = False
145
146 # Extract the rest of meta data
147 video_title = self._search_meta(u'name', video_page, u'title')
148 if not video_title:
149 video_title = video_url.rsplit('/', 1)[-1]
150
151 video_description = self._search_meta(u'description', video_page)
152 END_TEXT = u' на сайте Smotri.com'
153 if video_description.endswith(END_TEXT):
154 video_description = video_description[:-len(END_TEXT)]
155 START_TEXT = u'Смотреть онлайн ролик '
156 if video_description.startswith(START_TEXT):
157 video_description = video_description[len(START_TEXT):]
158 video_thumbnail = self._search_meta(u'thumbnail', video_page)
159
160 upload_date_str = self._search_meta(u'uploadDate', video_page, u'upload date')
161 upload_date_m = re.search(r'(?P<year>\d{4})\.(?P<month>\d{2})\.(?P<day>\d{2})T', upload_date_str)
162 video_upload_date = (
163 (
164 upload_date_m.group('year') +
165 upload_date_m.group('month') +
166 upload_date_m.group('day')
167 )
168 if upload_date_m else None
169 )
170
171 duration_str = self._search_meta(u'duration', video_page)
172 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)
173 video_duration = (
174 (
175 (int(duration_m.group('hours')) * 60 * 60) +
176 (int(duration_m.group('minutes')) * 60) +
177 int(duration_m.group('seconds'))
178 )
179 if duration_m else None
180 )
181
182 video_uploader = self._html_search_regex(
183 u'<div class="DescrUser"><div>Автор.*?onmouseover="popup_user_info[^"]+">(.*?)</a>',
184 video_page, u'uploader', fatal=False, flags=re.MULTILINE|re.DOTALL)
185
186 video_uploader_id = self._html_search_regex(
187 u'<div class="DescrUser"><div>Автор.*?onmouseover="popup_user_info\\(.*?\'([^\']+)\'\\);">',
188 video_page, u'uploader id', fatal=False, flags=re.MULTILINE|re.DOTALL)
189
190 video_view_count = self._html_search_regex(
191 u'Общее количество просмотров.*?<span class="Number">(\\d+)</span>',
192 video_page, u'view count', fatal=False, flags=re.MULTILINE|re.DOTALL)
193
194 return {
195 'id': video_id,
196 'url': video_url,
197 'title': video_title,
198 'thumbnail': video_thumbnail,
199 'description': video_description,
200 'uploader': video_uploader,
201 'upload_date': video_upload_date,
202 'uploader_id': video_uploader_id,
203 'video_duration': video_duration,
204 'view_count': video_view_count,
205 'age_limit': 18 if adult_content else 0,
206 'video_page_url': video_page_url
207 }
208
209
210 class SmotriCommunityIE(InfoExtractor):
211 IE_DESC = u'Smotri.com community videos'
212 IE_NAME = u'smotri:community'
213 _VALID_URL = r'^https?://(?:www\.)?smotri\.com/community/video/(?P<communityid>[0-9A-Za-z_\'-]+)'
214
215 def _real_extract(self, url):
216 mobj = re.match(self._VALID_URL, url)
217 community_id = mobj.group('communityid')
218
219 url = 'http://smotri.com/export/rss/video/by/community/-/%s/video.xml' % community_id
220 rss = self._download_xml(url, community_id, u'Downloading community RSS')
221
222 entries = [self.url_result(video_url.text, 'Smotri')
223 for video_url in rss.findall('./channel/item/link')]
224
225 description_text = rss.find('./channel/description').text
226 community_title = self._html_search_regex(
227 u'^Видео сообщества "([^"]+)"$', description_text, u'community title')
228
229 return self.playlist_result(entries, community_id, community_title)
230
231
232 class SmotriUserIE(InfoExtractor):
233 IE_DESC = u'Smotri.com user videos'
234 IE_NAME = u'smotri:user'
235 _VALID_URL = r'^https?://(?:www\.)?smotri\.com/user/(?P<userid>[0-9A-Za-z_\'-]+)'
236
237 def _real_extract(self, url):
238 mobj = re.match(self._VALID_URL, url)
239 user_id = mobj.group('userid')
240
241 url = 'http://smotri.com/export/rss/user/video/-/%s/video.xml' % user_id
242 rss = self._download_xml(url, user_id, u'Downloading user RSS')
243
244 entries = [self.url_result(video_url.text, 'Smotri')
245 for video_url in rss.findall('./channel/item/link')]
246
247 description_text = rss.find('./channel/description').text
248 user_nickname = self._html_search_regex(
249 u'^Видео режиссера (.*)$', description_text,
250 u'user nickname')
251
252 return self.playlist_result(entries, user_id, user_nickname)