]>
Commit | Line | Data |
---|---|---|
35409e11 PH |
1 | from __future__ import unicode_literals |
2 | ||
df228355 | 3 | import itertools |
cb10cded PH |
4 | import re |
5 | ||
6 | from .common import InfoExtractor | |
d852c6bc | 7 | from ..compat import compat_str |
cb10cded | 8 | from ..utils import ( |
85552042 | 9 | clean_html, |
fea92aa6 | 10 | determine_ext, |
065c4b27 | 11 | dict_get, |
df228355 | 12 | extract_attributes, |
103f8c8d | 13 | ExtractorError, |
ccb079ee | 14 | int_or_none, |
51378d35 | 15 | parse_duration, |
fea92aa6 | 16 | try_get, |
44731e30 | 17 | unified_strdate, |
3052a30d | 18 | url_or_none, |
cb10cded PH |
19 | ) |
20 | ||
21 | ||
22 | class XHamsterIE(InfoExtractor): | |
10709fc7 | 23 | _DOMAINS = r'(?:xhamster\.(?:com|one|desi)|xhms\.pro|xhamster\d+\.com)' |
00e5c363 S |
24 | _VALID_URL = r'''(?x) |
25 | https?:// | |
8945b10f | 26 | (?:.+?\.)?%s/ |
00e5c363 | 27 | (?: |
6cb30ea5 J |
28 | movies/(?P<id>[\dA-Za-z]+)/(?P<display_id>[^/]*)\.html| |
29 | videos/(?P<display_id_2>[^/]*)-(?P<id_2>[\dA-Za-z]+) | |
00e5c363 | 30 | ) |
8945b10f | 31 | ''' % _DOMAINS |
6b43132c | 32 | _TESTS = [{ |
8945b10f S |
33 | 'url': 'https://xhamster.com/videos/femaleagent-shy-beauty-takes-the-bait-1509445', |
34 | 'md5': '98b4687efb1ffd331c4197854dc09e8f', | |
6b43132c S |
35 | 'info_dict': { |
36 | 'id': '1509445', | |
8945b10f | 37 | 'display_id': 'femaleagent-shy-beauty-takes-the-bait', |
6b43132c S |
38 | 'ext': 'mp4', |
39 | 'title': 'FemaleAgent Shy beauty takes the bait', | |
fea92aa6 | 40 | 'timestamp': 1350194821, |
6b43132c S |
41 | 'upload_date': '20121014', |
42 | 'uploader': 'Ruseful2011', | |
51378d35 | 43 | 'duration': 893, |
6b43132c | 44 | 'age_limit': 18, |
ccb079ee | 45 | }, |
6b43132c | 46 | }, { |
8945b10f | 47 | 'url': 'https://xhamster.com/videos/britney-spears-sexy-booty-2221348?hd=', |
6b43132c S |
48 | 'info_dict': { |
49 | 'id': '2221348', | |
8945b10f | 50 | 'display_id': 'britney-spears-sexy-booty', |
6b43132c S |
51 | 'ext': 'mp4', |
52 | 'title': 'Britney Spears Sexy Booty', | |
fea92aa6 | 53 | 'timestamp': 1379123460, |
6b43132c S |
54 | 'upload_date': '20130914', |
55 | 'uploader': 'jojo747400', | |
51378d35 | 56 | 'duration': 200, |
6b43132c | 57 | 'age_limit': 18, |
5b9aefef | 58 | }, |
6b43132c S |
59 | 'params': { |
60 | 'skip_download': True, | |
a4690b32 | 61 | }, |
6b43132c | 62 | }, { |
8945b10f | 63 | # empty seo, unavailable via new URL schema |
6b43132c S |
64 | 'url': 'http://xhamster.com/movies/5667973/.html', |
65 | 'info_dict': { | |
66 | 'id': '5667973', | |
67 | 'ext': 'mp4', | |
68 | 'title': '....', | |
fea92aa6 | 69 | 'timestamp': 1454948101, |
6b43132c S |
70 | 'upload_date': '20160208', |
71 | 'uploader': 'parejafree', | |
51378d35 | 72 | 'duration': 72, |
6b43132c | 73 | 'age_limit': 18, |
5b9aefef | 74 | }, |
6b43132c S |
75 | 'params': { |
76 | 'skip_download': True, | |
77 | }, | |
b271e335 W |
78 | }, { |
79 | # mobile site | |
80 | 'url': 'https://m.xhamster.com/videos/cute-teen-jacqueline-solo-masturbation-8559111', | |
81 | 'only_matching': True, | |
6b43132c S |
82 | }, { |
83 | 'url': 'https://xhamster.com/movies/2272726/amber_slayed_by_the_knight.html', | |
84 | 'only_matching': True, | |
103f8c8d S |
85 | }, { |
86 | # This video is visible for marcoalfa123456's friends only | |
87 | 'url': 'https://it.xhamster.com/movies/7263980/la_mia_vicina.html', | |
88 | 'only_matching': True, | |
00e5c363 S |
89 | }, { |
90 | # new URL schema | |
91 | 'url': 'https://pt.xhamster.com/videos/euro-pedal-pumping-7937821', | |
92 | 'only_matching': True, | |
b43c5f47 S |
93 | }, { |
94 | 'url': 'https://xhamster.one/videos/femaleagent-shy-beauty-takes-the-bait-1509445', | |
95 | 'only_matching': True, | |
8945b10f S |
96 | }, { |
97 | 'url': 'https://xhamster.desi/videos/femaleagent-shy-beauty-takes-the-bait-1509445', | |
98 | 'only_matching': True, | |
99 | }, { | |
100 | 'url': 'https://xhamster2.com/videos/femaleagent-shy-beauty-takes-the-bait-1509445', | |
101 | 'only_matching': True, | |
10709fc7 S |
102 | }, { |
103 | 'url': 'https://xhamster11.com/videos/femaleagent-shy-beauty-takes-the-bait-1509445', | |
104 | 'only_matching': True, | |
105 | }, { | |
106 | 'url': 'https://xhamster26.com/videos/femaleagent-shy-beauty-takes-the-bait-1509445', | |
107 | 'only_matching': True, | |
8945b10f S |
108 | }, { |
109 | 'url': 'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html', | |
110 | 'only_matching': True, | |
111 | }, { | |
112 | 'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd', | |
113 | 'only_matching': True, | |
6cb30ea5 J |
114 | }, { |
115 | 'url': 'http://de.xhamster.com/videos/skinny-girl-fucks-herself-hard-in-the-forest-xhnBJZx', | |
116 | 'only_matching': True, | |
6b43132c | 117 | }] |
cb10cded | 118 | |
5f6a1245 | 119 | def _real_extract(self, url): |
cb10cded | 120 | mobj = re.match(self._VALID_URL, url) |
00e5c363 S |
121 | video_id = mobj.group('id') or mobj.group('id_2') |
122 | display_id = mobj.group('display_id') or mobj.group('display_id_2') | |
123 | ||
b271e335 | 124 | desktop_url = re.sub(r'^(https?://(?:.+?\.)?)m\.', r'\1', url) |
b274e48d | 125 | webpage, urlh = self._download_webpage_handle(desktop_url, video_id) |
cb10cded | 126 | |
103f8c8d S |
127 | error = self._html_search_regex( |
128 | r'<div[^>]+id=["\']videoClosed["\'][^>]*>(.+?)</div>', | |
129 | webpage, 'error', default=None) | |
130 | if error: | |
131 | raise ExtractorError(error, expected=True) | |
132 | ||
fea92aa6 S |
133 | age_limit = self._rta_search(webpage) |
134 | ||
135 | def get_height(s): | |
136 | return int_or_none(self._search_regex( | |
137 | r'^(\d+)[pP]', s, 'height', default=None)) | |
138 | ||
139 | initials = self._parse_json( | |
140 | self._search_regex( | |
39e7107d U |
141 | (r'window\.initials\s*=\s*({.+?})\s*;\s*</script>', |
142 | r'window\.initials\s*=\s*({.+?})\s*;'), webpage, 'initials', | |
fea92aa6 S |
143 | default='{}'), |
144 | video_id, fatal=False) | |
145 | if initials: | |
146 | video = initials['videoModel'] | |
147 | title = video['title'] | |
148 | formats = [] | |
149 | for format_id, formats_dict in video['sources'].items(): | |
150 | if not isinstance(formats_dict, dict): | |
151 | continue | |
152 | for quality, format_item in formats_dict.items(): | |
153 | if format_id == 'download': | |
154 | # Download link takes some time to be generated, | |
155 | # skipping for now | |
156 | continue | |
157 | if not isinstance(format_item, dict): | |
158 | continue | |
159 | format_url = format_item.get('link') | |
160 | filesize = int_or_none( | |
161 | format_item.get('size'), invscale=1000000) | |
162 | else: | |
163 | format_url = format_item | |
164 | filesize = None | |
3052a30d S |
165 | format_url = url_or_none(format_url) |
166 | if not format_url: | |
fea92aa6 S |
167 | continue |
168 | formats.append({ | |
169 | 'format_id': '%s-%s' % (format_id, quality), | |
170 | 'url': format_url, | |
171 | 'ext': determine_ext(format_url, 'mp4'), | |
172 | 'height': get_height(quality), | |
173 | 'filesize': filesize, | |
b274e48d S |
174 | 'http_headers': { |
175 | 'Referer': urlh.geturl(), | |
176 | }, | |
fea92aa6 S |
177 | }) |
178 | self._sort_formats(formats) | |
179 | ||
180 | categories_list = video.get('categories') | |
181 | if isinstance(categories_list, list): | |
182 | categories = [] | |
183 | for c in categories_list: | |
184 | if not isinstance(c, dict): | |
185 | continue | |
186 | c_name = c.get('name') | |
187 | if isinstance(c_name, compat_str): | |
188 | categories.append(c_name) | |
189 | else: | |
190 | categories = None | |
191 | ||
192 | return { | |
193 | 'id': video_id, | |
194 | 'display_id': display_id, | |
195 | 'title': title, | |
196 | 'description': video.get('description'), | |
197 | 'timestamp': int_or_none(video.get('created')), | |
198 | 'uploader': try_get( | |
199 | video, lambda x: x['author']['name'], compat_str), | |
200 | 'thumbnail': video.get('thumbURL'), | |
201 | 'duration': int_or_none(video.get('duration')), | |
202 | 'view_count': int_or_none(video.get('views')), | |
203 | 'like_count': int_or_none(try_get( | |
204 | video, lambda x: x['rating']['likes'], int)), | |
205 | 'dislike_count': int_or_none(try_get( | |
206 | video, lambda x: x['rating']['dislikes'], int)), | |
207 | 'comment_count': int_or_none(video.get('views')), | |
208 | 'age_limit': age_limit, | |
209 | 'categories': categories, | |
210 | 'formats': formats, | |
211 | } | |
212 | ||
213 | # Old layout fallback | |
214 | ||
4395ca2e | 215 | title = self._html_search_regex( |
1a6d9284 S |
216 | [r'<h1[^>]*>([^<]+)</h1>', |
217 | r'<meta[^>]+itemprop=".*?caption.*?"[^>]+content="(.+?)"', | |
218 | r'<title[^>]*>(.+?)(?:,\s*[^,]*?\s*Porn\s*[^,]*?:\s*xHamster[^<]*| - xHamster\.com)</title>'], | |
219 | webpage, 'title') | |
cb10cded | 220 | |
d852c6bc S |
221 | formats = [] |
222 | format_urls = set() | |
223 | ||
224 | sources = self._parse_json( | |
225 | self._search_regex( | |
226 | r'sources\s*:\s*({.+?})\s*,?\s*\n', webpage, 'sources', | |
227 | default='{}'), | |
228 | video_id, fatal=False) | |
229 | for format_id, format_url in sources.items(): | |
3052a30d S |
230 | format_url = url_or_none(format_url) |
231 | if not format_url: | |
d852c6bc S |
232 | continue |
233 | if format_url in format_urls: | |
234 | continue | |
235 | format_urls.add(format_url) | |
236 | formats.append({ | |
237 | 'format_id': format_id, | |
238 | 'url': format_url, | |
fea92aa6 | 239 | 'height': get_height(format_id), |
d852c6bc S |
240 | }) |
241 | ||
242 | video_url = self._search_regex( | |
243 | [r'''file\s*:\s*(?P<q>["'])(?P<mp4>.+?)(?P=q)''', | |
244 | r'''<a\s+href=(?P<q>["'])(?P<mp4>.+?)(?P=q)\s+class=["']mp4Thumb''', | |
245 | r'''<video[^>]+file=(?P<q>["'])(?P<mp4>.+?)(?P=q)[^>]*>'''], | |
246 | webpage, 'video url', group='mp4', default=None) | |
247 | if video_url and video_url not in format_urls: | |
248 | formats.append({ | |
249 | 'url': video_url, | |
250 | }) | |
251 | ||
252 | self._sort_formats(formats) | |
253 | ||
4353cf51 | 254 | # Only a few videos have an description |
22ff1c4a | 255 | mobj = re.search(r'<span>Description: </span>([^<]+)', webpage) |
ccb079ee | 256 | description = mobj.group(1) if mobj else None |
cb10cded | 257 | |
4763b624 S |
258 | upload_date = unified_strdate(self._search_regex( |
259 | r'hint=["\'](\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}', | |
260 | webpage, 'upload date', fatal=False)) | |
cb10cded | 261 | |
3e485224 | 262 | uploader = self._html_search_regex( |
a846173d | 263 | r'<span[^>]+itemprop=["\']author[^>]+><a[^>]+><span[^>]+>([^<]+)', |
3e485224 | 264 | webpage, 'uploader', default='anonymous') |
cb10cded | 265 | |
251a44b7 | 266 | thumbnail = self._search_regex( |
b271e335 W |
267 | [r'''["']thumbUrl["']\s*:\s*(?P<q>["'])(?P<thumbnail>.+?)(?P=q)''', |
268 | r'''<video[^>]+"poster"=(?P<q>["'])(?P<thumbnail>.+?)(?P=q)[^>]*>'''], | |
c73cdd80 | 269 | webpage, 'thumbnail', fatal=False, group='thumbnail') |
ccb079ee | 270 | |
51378d35 | 271 | duration = parse_duration(self._search_regex( |
d852c6bc S |
272 | [r'<[^<]+\bitemprop=["\']duration["\'][^<]+\bcontent=["\'](.+?)["\']', |
273 | r'Runtime:\s*</span>\s*([\d:]+)'], webpage, | |
51378d35 | 274 | 'duration', fatal=False)) |
ccb079ee | 275 | |
6a16fd4a S |
276 | view_count = int_or_none(self._search_regex( |
277 | r'content=["\']User(?:View|Play)s:(\d+)', | |
278 | webpage, 'view count', fatal=False)) | |
ccb079ee | 279 | |
a846173d | 280 | mobj = re.search(r'hint=[\'"](?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes', webpage) |
ccb079ee S |
281 | (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None) |
282 | ||
283 | mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage) | |
284 | comment_count = mobj.group('commentcount') if mobj else 0 | |
cb10cded | 285 | |
85552042 S |
286 | categories_html = self._search_regex( |
287 | r'(?s)<table.+?(<span>Categories:.+?)</table>', webpage, | |
288 | 'categories', default=None) | |
289 | categories = [clean_html(category) for category in re.findall( | |
290 | r'<a[^>]+>(.+?)</a>', categories_html)] if categories_html else None | |
291 | ||
5d0c9754 | 292 | return { |
293 | 'id': video_id, | |
d852c6bc | 294 | 'display_id': display_id, |
ccb079ee S |
295 | 'title': title, |
296 | 'description': description, | |
297 | 'upload_date': upload_date, | |
3e485224 | 298 | 'uploader': uploader, |
ccb079ee S |
299 | 'thumbnail': thumbnail, |
300 | 'duration': duration, | |
301 | 'view_count': view_count, | |
302 | 'like_count': int_or_none(like_count), | |
303 | 'dislike_count': int_or_none(dislike_count), | |
304 | 'comment_count': int_or_none(comment_count), | |
9d92015d | 305 | 'age_limit': age_limit, |
85552042 | 306 | 'categories': categories, |
ccb079ee | 307 | 'formats': formats, |
5d0c9754 | 308 | } |
0bbba43e S |
309 | |
310 | ||
311 | class XHamsterEmbedIE(InfoExtractor): | |
8945b10f | 312 | _VALID_URL = r'https?://(?:.+?\.)?%s/xembed\.php\?video=(?P<id>\d+)' % XHamsterIE._DOMAINS |
0bbba43e S |
313 | _TEST = { |
314 | 'url': 'http://xhamster.com/xembed.php?video=3328539', | |
315 | 'info_dict': { | |
316 | 'id': '3328539', | |
317 | 'ext': 'mp4', | |
318 | 'title': 'Pen Masturbation', | |
b271e335 | 319 | 'timestamp': 1406581861, |
0bbba43e | 320 | 'upload_date': '20140728', |
b271e335 | 321 | 'uploader': 'ManyakisArt', |
0bbba43e S |
322 | 'duration': 5, |
323 | 'age_limit': 18, | |
324 | } | |
325 | } | |
326 | ||
2bb5b6d0 S |
327 | @staticmethod |
328 | def _extract_urls(webpage): | |
329 | return [url for _, url in re.findall( | |
330 | r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?xhamster\.com/xembed\.php\?video=\d+)\1', | |
331 | webpage)] | |
332 | ||
0bbba43e S |
333 | def _real_extract(self, url): |
334 | video_id = self._match_id(url) | |
335 | ||
336 | webpage = self._download_webpage(url, video_id) | |
337 | ||
338 | video_url = self._search_regex( | |
db962528 | 339 | r'href="(https?://xhamster\.com/(?:movies/{0}/[^"]*\.html|videos/[^/]*-{0})[^"]*)"'.format(video_id), |
065c4b27 S |
340 | webpage, 'xhamster url', default=None) |
341 | ||
342 | if not video_url: | |
343 | vars = self._parse_json( | |
344 | self._search_regex(r'vars\s*:\s*({.+?})\s*,\s*\n', webpage, 'vars'), | |
345 | video_id) | |
346 | video_url = dict_get(vars, ('downloadLink', 'homepageLink', 'commentsLink', 'shareUrl')) | |
0bbba43e | 347 | |
25701d5a | 348 | return self.url_result(video_url, 'XHamster') |
df228355 S |
349 | |
350 | ||
351 | class XHamsterUserIE(InfoExtractor): | |
352 | _VALID_URL = r'https?://(?:.+?\.)?%s/users/(?P<id>[^/?#&]+)' % XHamsterIE._DOMAINS | |
353 | _TESTS = [{ | |
354 | # Paginated user profile | |
355 | 'url': 'https://xhamster.com/users/netvideogirls/videos', | |
356 | 'info_dict': { | |
357 | 'id': 'netvideogirls', | |
358 | }, | |
359 | 'playlist_mincount': 267, | |
360 | }, { | |
361 | # Non-paginated user profile | |
362 | 'url': 'https://xhamster.com/users/firatkaan/videos', | |
363 | 'info_dict': { | |
364 | 'id': 'firatkaan', | |
365 | }, | |
366 | 'playlist_mincount': 1, | |
367 | }] | |
368 | ||
369 | def _entries(self, user_id): | |
370 | next_page_url = 'https://xhamster.com/users/%s/videos/1' % user_id | |
371 | for pagenum in itertools.count(1): | |
372 | page = self._download_webpage( | |
373 | next_page_url, user_id, 'Downloading page %s' % pagenum) | |
374 | for video_tag in re.findall( | |
375 | r'(<a[^>]+class=["\'].*?\bvideo-thumb__image-container[^>]+>)', | |
376 | page): | |
377 | video = extract_attributes(video_tag) | |
378 | video_url = url_or_none(video.get('href')) | |
379 | if not video_url or not XHamsterIE.suitable(video_url): | |
380 | continue | |
381 | video_id = XHamsterIE._match_id(video_url) | |
382 | yield self.url_result( | |
383 | video_url, ie=XHamsterIE.ie_key(), video_id=video_id) | |
384 | mobj = re.search(r'<a[^>]+data-page=["\']next[^>]+>', page) | |
385 | if not mobj: | |
386 | break | |
387 | next_page = extract_attributes(mobj.group(0)) | |
388 | next_page_url = url_or_none(next_page.get('href')) | |
389 | if not next_page_url: | |
390 | break | |
391 | ||
392 | def _real_extract(self, url): | |
393 | user_id = self._match_id(url) | |
394 | return self.playlist_result(self._entries(user_id), user_id) |