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