]>
Commit | Line | Data |
---|---|---|
6c376029 | 1 | # coding: utf-8 |
9933b574 PH |
2 | from __future__ import unicode_literals |
3 | ||
21fbf0f9 | 4 | import functools |
34541395 | 5 | import itertools |
df2a5633 | 6 | import math |
21fbf0f9 | 7 | import operator |
125cfd78 | 8 | import re |
9 | ||
10 | from .common import InfoExtractor | |
1cc79574 | 11 | from ..compat import ( |
34541395 | 12 | compat_HTTPError, |
79367a98 | 13 | compat_str, |
278d061a | 14 | compat_urllib_request, |
1cc79574 | 15 | ) |
278d061a | 16 | from .openload import PhantomJSwrapper |
1cc79574 | 17 | from ..utils import ( |
d0fb4bd1 | 18 | clean_html, |
b8526c78 | 19 | determine_ext, |
50789175 | 20 | ExtractorError, |
e0ddbd02 | 21 | format_field, |
ed8648a3 | 22 | int_or_none, |
cd85a1bb | 23 | merge_dicts, |
0164cd5d | 24 | NO_DEFAULT, |
8f9a477e | 25 | orderedSet, |
e1e35d1a | 26 | remove_quotes, |
0320ddc1 | 27 | str_to_int, |
2181983a | 28 | update_url_query, |
29 | urlencode_postdata, | |
4938c8d5 | 30 | url_or_none, |
125cfd78 | 31 | ) |
125cfd78 | 32 | |
9933b574 | 33 | |
71a1f617 | 34 | class PornHubBaseIE(InfoExtractor): |
2181983a | 35 | _NETRC_MACHINE = 'pornhub' |
4c968755 | 36 | _PORNHUB_HOST_RE = r'(?:(?P<host>pornhub(?:premium)?\.(?:com|net|org))|pornhubvybmsymdol4iibwgwtkpwmeyd6luq2gxajgjzfjvotyt5zhyd\.onion)' |
2181983a | 37 | |
71a1f617 S |
38 | def _download_webpage_handle(self, *args, **kwargs): |
39 | def dl(*args, **kwargs): | |
40 | return super(PornHubBaseIE, self)._download_webpage_handle(*args, **kwargs) | |
41 | ||
29f7c58a | 42 | ret = dl(*args, **kwargs) |
43 | ||
44 | if not ret: | |
45 | return ret | |
46 | ||
47 | webpage, urlh = ret | |
71a1f617 S |
48 | |
49 | if any(re.search(p, webpage) for p in ( | |
50 | r'<body\b[^>]+\bonload=["\']go\(\)', | |
51 | r'document\.cookie\s*=\s*["\']RNKEY=', | |
52 | r'document\.location\.reload\(true\)')): | |
53 | url_or_request = args[0] | |
54 | url = (url_or_request.get_full_url() | |
55 | if isinstance(url_or_request, compat_urllib_request.Request) | |
56 | else url_or_request) | |
57 | phantom = PhantomJSwrapper(self, required_version='2.0') | |
58 | phantom.get(url, html=webpage) | |
59 | webpage, urlh = dl(*args, **kwargs) | |
60 | ||
61 | return webpage, urlh | |
62 | ||
2181983a | 63 | def _real_initialize(self): |
64 | self._logged_in = False | |
65 | ||
66 | def _login(self, host): | |
67 | if self._logged_in: | |
68 | return | |
69 | ||
70 | site = host.split('.')[0] | |
71 | ||
72 | # Both sites pornhub and pornhubpremium have separate accounts | |
73 | # so there should be an option to provide credentials for both. | |
74 | # At the same time some videos are available under the same video id | |
75 | # on both sites so that we have to identify them as the same video. | |
76 | # For that purpose we have to keep both in the same extractor | |
77 | # but under different netrc machines. | |
78 | username, password = self._get_login_info(netrc_machine=site) | |
79 | if username is None: | |
80 | return | |
81 | ||
82 | login_url = 'https://www.%s/%slogin' % (host, 'premium/' if 'premium' in host else '') | |
83 | login_page = self._download_webpage( | |
84 | login_url, None, 'Downloading %s login page' % site) | |
85 | ||
86 | def is_logged(webpage): | |
87 | return any(re.search(p, webpage) for p in ( | |
88 | r'class=["\']signOut', | |
89 | r'>Sign\s+[Oo]ut\s*<')) | |
90 | ||
91 | if is_logged(login_page): | |
92 | self._logged_in = True | |
93 | return | |
94 | ||
95 | login_form = self._hidden_inputs(login_page) | |
96 | ||
97 | login_form.update({ | |
98 | 'username': username, | |
99 | 'password': password, | |
100 | }) | |
101 | ||
102 | response = self._download_json( | |
103 | 'https://www.%s/front/authenticate' % host, None, | |
104 | 'Logging in to %s' % site, | |
105 | data=urlencode_postdata(login_form), | |
106 | headers={ | |
107 | 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | |
108 | 'Referer': login_url, | |
109 | 'X-Requested-With': 'XMLHttpRequest', | |
110 | }) | |
111 | ||
112 | if response.get('success') == '1': | |
113 | self._logged_in = True | |
114 | return | |
115 | ||
116 | message = response.get('message') | |
117 | if message is not None: | |
118 | raise ExtractorError( | |
119 | 'Unable to login: %s' % message, expected=True) | |
120 | ||
121 | raise ExtractorError('Unable to log in') | |
122 | ||
71a1f617 S |
123 | |
124 | class PornHubIE(PornHubBaseIE): | |
bc4b2d75 S |
125 | IE_DESC = 'PornHub and Thumbzilla' |
126 | _VALID_URL = r'''(?x) | |
127 | https?:// | |
128 | (?: | |
ed807c18 | 129 | (?:[^/]+\.)? |
130 | %s | |
131 | /(?:(?:view_video\.php|video/show)\?viewkey=|embed/)| | |
bc4b2d75 S |
132 | (?:www\.)?thumbzilla\.com/video/ |
133 | ) | |
b52c9ef1 | 134 | (?P<id>[\da-z]+) |
ed807c18 | 135 | ''' % PornHubBaseIE._PORNHUB_HOST_RE |
360075e2 | 136 | _TESTS = [{ |
9933b574 | 137 | 'url': 'http://www.pornhub.com/view_video.php?viewkey=648719015', |
cd85a1bb | 138 | 'md5': 'a6391306d050e4547f62b3f485dd9ba9', |
9933b574 | 139 | 'info_dict': { |
249efaf4 PH |
140 | 'id': '648719015', |
141 | 'ext': 'mp4', | |
611c1dd9 | 142 | 'title': 'Seductive Indian beauty strips down and fingers her pink pussy', |
ed8648a3 | 143 | 'uploader': 'Babes', |
3661ebf2 | 144 | 'upload_date': '20130628', |
cd85a1bb | 145 | 'timestamp': 1372447216, |
ed8648a3 S |
146 | 'duration': 361, |
147 | 'view_count': int, | |
148 | 'like_count': int, | |
149 | 'dislike_count': int, | |
150 | 'comment_count': int, | |
151 | 'age_limit': 18, | |
6bb05b32 YCH |
152 | 'tags': list, |
153 | 'categories': list, | |
d0fb4bd1 | 154 | 'cast': list, |
6c376029 S |
155 | }, |
156 | }, { | |
157 | # non-ASCII title | |
158 | 'url': 'http://www.pornhub.com/view_video.php?viewkey=1331683002', | |
159 | 'info_dict': { | |
160 | 'id': '1331683002', | |
161 | 'ext': 'mp4', | |
162 | 'title': '重庆婷婷女王足交', | |
3661ebf2 | 163 | 'upload_date': '20150213', |
cd85a1bb | 164 | 'timestamp': 1423804862, |
6c376029 S |
165 | 'duration': 1753, |
166 | 'view_count': int, | |
167 | 'like_count': int, | |
168 | 'dislike_count': int, | |
169 | 'comment_count': int, | |
170 | 'age_limit': 18, | |
6bb05b32 YCH |
171 | 'tags': list, |
172 | 'categories': list, | |
6c376029 S |
173 | }, |
174 | 'params': { | |
175 | 'skip_download': True, | |
176 | }, | |
10db0d2f | 177 | 'skip': 'Video has been flagged for verification in accordance with our trust and safety policy', |
4938c8d5 GF |
178 | }, { |
179 | # subtitles | |
180 | 'url': 'https://www.pornhub.com/view_video.php?viewkey=ph5af5fef7c2aa7', | |
181 | 'info_dict': { | |
182 | 'id': 'ph5af5fef7c2aa7', | |
183 | 'ext': 'mp4', | |
184 | 'title': 'BFFS - Cute Teen Girls Share Cock On the Floor', | |
185 | 'uploader': 'BFFs', | |
186 | 'duration': 622, | |
187 | 'view_count': int, | |
188 | 'like_count': int, | |
189 | 'dislike_count': int, | |
190 | 'comment_count': int, | |
191 | 'age_limit': 18, | |
192 | 'tags': list, | |
193 | 'categories': list, | |
194 | 'subtitles': { | |
195 | 'en': [{ | |
196 | "ext": 'srt' | |
197 | }] | |
198 | }, | |
199 | }, | |
200 | 'params': { | |
201 | 'skip_download': True, | |
202 | }, | |
cd85a1bb | 203 | 'skip': 'This video has been disabled', |
360075e2 S |
204 | }, { |
205 | 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph557bbb6676d2d', | |
206 | 'only_matching': True, | |
272e4db5 | 207 | }, { |
eaaaaec0 | 208 | # removed at the request of cam4.com |
272e4db5 S |
209 | 'url': 'http://fr.pornhub.com/view_video.php?viewkey=ph55ca2f9760862', |
210 | 'only_matching': True, | |
eaaaaec0 S |
211 | }, { |
212 | # removed at the request of the copyright owner | |
213 | 'url': 'http://www.pornhub.com/view_video.php?viewkey=788152859', | |
214 | 'only_matching': True, | |
215 | }, { | |
216 | # removed by uploader | |
217 | 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph572716d15a111', | |
218 | 'only_matching': True, | |
195f0845 S |
219 | }, { |
220 | # private video | |
221 | 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph56fd731fce6b7', | |
222 | 'only_matching': True, | |
bc4b2d75 S |
223 | }, { |
224 | 'url': 'https://www.thumbzilla.com/video/ph56c6114abd99a/horny-girlfriend-sex', | |
225 | 'only_matching': True, | |
a99cc4ca S |
226 | }, { |
227 | 'url': 'http://www.pornhub.com/video/show?viewkey=648719015', | |
228 | 'only_matching': True, | |
f97c0991 S |
229 | }, { |
230 | 'url': 'https://www.pornhub.net/view_video.php?viewkey=203640933', | |
231 | 'only_matching': True, | |
29f7c58a | 232 | }, { |
233 | 'url': 'https://www.pornhub.org/view_video.php?viewkey=203640933', | |
234 | 'only_matching': True, | |
fa9b8c66 TW |
235 | }, { |
236 | 'url': 'https://www.pornhubpremium.com/view_video.php?viewkey=ph5e4acdae54a82', | |
237 | 'only_matching': True, | |
2181983a | 238 | }, { |
239 | # Some videos are available with the same id on both premium | |
240 | # and non-premium sites (e.g. this and the following test) | |
241 | 'url': 'https://www.pornhub.com/view_video.php?viewkey=ph5f75b0f4b18e3', | |
242 | 'only_matching': True, | |
243 | }, { | |
244 | 'url': 'https://www.pornhubpremium.com/view_video.php?viewkey=ph5f75b0f4b18e3', | |
245 | 'only_matching': True, | |
ed807c18 | 246 | }, { |
247 | # geo restricted | |
248 | 'url': 'https://www.pornhub.com/view_video.php?viewkey=ph5a9813bfa7156', | |
249 | 'only_matching': True, | |
250 | }, { | |
4c968755 | 251 | 'url': 'http://pornhubvybmsymdol4iibwgwtkpwmeyd6luq2gxajgjzfjvotyt5zhyd.onion/view_video.php?viewkey=ph5a9813bfa7156', |
ed807c18 | 252 | 'only_matching': True, |
360075e2 | 253 | }] |
125cfd78 | 254 | |
b52c9ef1 S |
255 | @staticmethod |
256 | def _extract_urls(webpage): | |
257 | return re.findall( | |
2181983a | 258 | r'<iframe[^>]+?src=["\'](?P<url>(?:https?:)?//(?:www\.)?pornhub(?:premium)?\.(?:com|net|org)/embed/[\da-z]+)', |
b52c9ef1 | 259 | webpage) |
65d161c4 | 260 | |
0320ddc1 | 261 | def _extract_count(self, pattern, webpage, name): |
b69fd25c | 262 | return str_to_int(self._search_regex(pattern, webpage, '%s count' % name, default=None)) |
0320ddc1 | 263 | |
125cfd78 | 264 | def _real_extract(self, url): |
5ad28e7f | 265 | mobj = self._match_valid_url(url) |
3430ff9b S |
266 | host = mobj.group('host') or 'pornhub.com' |
267 | video_id = mobj.group('id') | |
7399ca1f | 268 | |
2181983a | 269 | self._login(host) |
fa9b8c66 | 270 | |
3430ff9b | 271 | self._set_cookie(host, 'age_verified', '1') |
125cfd78 | 272 | |
9a372f14 | 273 | def dl_webpage(platform): |
3430ff9b | 274 | self._set_cookie(host, 'platform', platform) |
9a372f14 | 275 | return self._download_webpage( |
2c53c0eb | 276 | 'https://www.%s/view_video.php?viewkey=%s' % (host, video_id), |
79367a98 | 277 | video_id, 'Downloading %s webpage' % platform) |
9a372f14 S |
278 | |
279 | webpage = dl_webpage('pc') | |
125cfd78 | 280 | |
50789175 | 281 | error_msg = self._html_search_regex( |
10db0d2f | 282 | (r'(?s)<div[^>]+class=(["\'])(?:(?!\1).)*\b(?:removed|userMessageSection)\b(?:(?!\1).)*\1[^>]*>(?P<error>.+?)</div>', |
283 | r'(?s)<section[^>]+class=["\']noVideo["\'][^>]*>(?P<error>.+?)</section>'), | |
3cb3b600 | 284 | webpage, 'error message', default=None, group='error') |
50789175 PH |
285 | if error_msg: |
286 | error_msg = re.sub(r'\s+', ' ', error_msg) | |
287 | raise ExtractorError( | |
288 | 'PornHub said: %s' % error_msg, | |
289 | expected=True, video_id=video_id) | |
290 | ||
ed807c18 | 291 | if any(re.search(p, webpage) for p in ( |
292 | r'class=["\']geoBlocked["\']', | |
293 | r'>\s*This content is unavailable in your country')): | |
294 | self.raise_geo_restricted() | |
295 | ||
6c376029 S |
296 | # video_title from flashvars contains whitespace instead of non-ASCII (see |
297 | # http://www.pornhub.com/view_video.php?viewkey=1331683002), not relying | |
298 | # on that anymore. | |
79367a98 | 299 | title = self._html_search_meta( |
46cc54ca S |
300 | 'twitter:title', webpage, default=None) or self._html_search_regex( |
301 | (r'(?s)<h1[^>]+class=["\']title["\'][^>]*>(?P<title>.+?)</h1>', | |
302 | r'<div[^>]+data-video-title=(["\'])(?P<title>(?:(?!\1).)+)\1', | |
303 | r'shareTitle["\']\s*[=:]\s*(["\'])(?P<title>(?:(?!\1).)+)\1'), | |
6c376029 S |
304 | webpage, 'title', group='title') |
305 | ||
79367a98 S |
306 | video_urls = [] |
307 | video_urls_set = set() | |
4938c8d5 | 308 | subtitles = {} |
79367a98 | 309 | |
ed8648a3 S |
310 | flashvars = self._parse_json( |
311 | self._search_regex( | |
03442072 | 312 | r'var\s+flashvars_\d+\s*=\s*({.+?});', webpage, 'flashvars', default='{}'), |
ed8648a3 S |
313 | video_id) |
314 | if flashvars: | |
4938c8d5 GF |
315 | subtitle_url = url_or_none(flashvars.get('closedCaptionsFile')) |
316 | if subtitle_url: | |
317 | subtitles.setdefault('en', []).append({ | |
318 | 'url': subtitle_url, | |
319 | 'ext': 'srt', | |
320 | }) | |
ed8648a3 S |
321 | thumbnail = flashvars.get('image_url') |
322 | duration = int_or_none(flashvars.get('video_duration')) | |
79367a98 S |
323 | media_definitions = flashvars.get('mediaDefinitions') |
324 | if isinstance(media_definitions, list): | |
325 | for definition in media_definitions: | |
326 | if not isinstance(definition, dict): | |
327 | continue | |
328 | video_url = definition.get('videoUrl') | |
329 | if not video_url or not isinstance(video_url, compat_str): | |
330 | continue | |
331 | if video_url in video_urls_set: | |
332 | continue | |
333 | video_urls_set.add(video_url) | |
334 | video_urls.append( | |
335 | (video_url, int_or_none(definition.get('quality')))) | |
ed8648a3 | 336 | else: |
79367a98 S |
337 | thumbnail, duration = [None] * 2 |
338 | ||
0164cd5d | 339 | def extract_js_vars(webpage, pattern, default=NO_DEFAULT): |
79367a98 | 340 | assignments = self._search_regex( |
0164cd5d | 341 | pattern, webpage, 'encoded url', default=default) |
f4134726 S |
342 | if not assignments: |
343 | return {} | |
344 | ||
345 | assignments = assignments.split(';') | |
79367a98 S |
346 | |
347 | js_vars = {} | |
348 | ||
349 | def parse_js_value(inp): | |
350 | inp = re.sub(r'/\*(?:(?!\*/).)*?\*/', '', inp) | |
351 | if '+' in inp: | |
352 | inps = inp.split('+') | |
353 | return functools.reduce( | |
354 | operator.concat, map(parse_js_value, inps)) | |
355 | inp = inp.strip() | |
356 | if inp in js_vars: | |
357 | return js_vars[inp] | |
358 | return remove_quotes(inp) | |
359 | ||
360 | for assn in assignments: | |
361 | assn = assn.strip() | |
362 | if not assn: | |
363 | continue | |
364 | assn = re.sub(r'var\s+', '', assn) | |
365 | vname, value = assn.split('=', 1) | |
366 | js_vars[vname] = parse_js_value(value) | |
f4134726 | 367 | return js_vars |
79367a98 | 368 | |
f4134726 S |
369 | def add_video_url(video_url): |
370 | v_url = url_or_none(video_url) | |
371 | if not v_url: | |
372 | return | |
373 | if v_url in video_urls_set: | |
374 | return | |
375 | video_urls.append((v_url, None)) | |
376 | video_urls_set.add(v_url) | |
377 | ||
29f7c58a | 378 | def parse_quality_items(quality_items): |
379 | q_items = self._parse_json(quality_items, video_id, fatal=False) | |
380 | if not isinstance(q_items, list): | |
381 | return | |
382 | for item in q_items: | |
383 | if isinstance(item, dict): | |
384 | add_video_url(item.get('url')) | |
385 | ||
f4134726 | 386 | if not video_urls: |
29f7c58a | 387 | FORMAT_PREFIXES = ('media', 'quality', 'qualityItems') |
f4134726 S |
388 | js_vars = extract_js_vars( |
389 | webpage, r'(var\s+(?:%s)_.+)' % '|'.join(FORMAT_PREFIXES), | |
0164cd5d | 390 | default=None) |
f4134726 S |
391 | if js_vars: |
392 | for key, format_url in js_vars.items(): | |
29f7c58a | 393 | if key.startswith(FORMAT_PREFIXES[-1]): |
394 | parse_quality_items(format_url) | |
395 | elif any(key.startswith(p) for p in FORMAT_PREFIXES[:2]): | |
f4134726 | 396 | add_video_url(format_url) |
0164cd5d S |
397 | if not video_urls and re.search( |
398 | r'<[^>]+\bid=["\']lockedPlayer', webpage): | |
399 | raise ExtractorError( | |
400 | 'Video %s is locked' % video_id, expected=True) | |
f4134726 S |
401 | |
402 | if not video_urls: | |
403 | js_vars = extract_js_vars( | |
404 | dl_webpage('tv'), r'(var.+?mediastring.+?)</script>') | |
405 | add_video_url(js_vars['mediastring']) | |
79367a98 S |
406 | |
407 | for mobj in re.finditer( | |
408 | r'<a[^>]+\bclass=["\']downloadBtn\b[^>]+\bhref=(["\'])(?P<url>(?:(?!\1).)+)\1', | |
409 | webpage): | |
410 | video_url = mobj.group('url') | |
411 | if video_url not in video_urls_set: | |
412 | video_urls.append((video_url, None)) | |
413 | video_urls_set.add(video_url) | |
414 | ||
3661ebf2 | 415 | upload_date = None |
79367a98 | 416 | formats = [] |
10db0d2f | 417 | |
418 | def add_format(format_url, height=None): | |
f7ad7160 | 419 | ext = determine_ext(format_url) |
420 | if ext == 'mpd': | |
421 | formats.extend(self._extract_mpd_formats( | |
422 | format_url, video_id, mpd_id='dash', fatal=False)) | |
423 | return | |
424 | if ext == 'm3u8': | |
425 | formats.extend(self._extract_m3u8_formats( | |
426 | format_url, video_id, 'mp4', entry_protocol='m3u8_native', | |
427 | m3u8_id='hls', fatal=False)) | |
428 | return | |
ed807c18 | 429 | if not height: |
430 | height = int_or_none(self._search_regex( | |
431 | r'(?P<height>\d+)[pP]?_\d+[kK]', format_url, 'height', | |
432 | default=None)) | |
10db0d2f | 433 | formats.append({ |
434 | 'url': format_url, | |
e0ddbd02 | 435 | 'format_id': format_field(height, template='%dp'), |
10db0d2f | 436 | 'height': height, |
10db0d2f | 437 | }) |
438 | ||
79367a98 | 439 | for video_url, height in video_urls: |
3661ebf2 S |
440 | if not upload_date: |
441 | upload_date = self._search_regex( | |
442 | r'/(\d{6}/\d{2})/', video_url, 'upload data', default=None) | |
443 | if upload_date: | |
444 | upload_date = upload_date.replace('/', '') | |
10db0d2f | 445 | if '/video/get_media' in video_url: |
446 | medias = self._download_json(video_url, video_id, fatal=False) | |
447 | if isinstance(medias, list): | |
448 | for media in medias: | |
449 | if not isinstance(media, dict): | |
450 | continue | |
451 | video_url = url_or_none(media.get('videoUrl')) | |
452 | if not video_url: | |
453 | continue | |
454 | height = int_or_none(media.get('quality')) | |
455 | add_format(video_url, height) | |
456 | continue | |
457 | add_format(video_url) | |
ed807c18 | 458 | |
459 | # field_preference is unnecessary here, but kept for code-similarity with youtube-dl | |
460 | self._sort_formats( | |
461 | formats, field_preference=('height', 'width', 'fps', 'format_id')) | |
ed8648a3 | 462 | |
0320ddc1 | 463 | video_uploader = self._html_search_regex( |
2d4fe594 | 464 | r'(?s)From: .+?<(?:a\b[^>]+\bhref=["\']/(?:(?:user|channel)s|model|pornstar)/|span\b[^>]+\bclass=["\']username)[^>]+>(.+?)<', |
cd85a1bb | 465 | webpage, 'uploader', default=None) |
125cfd78 | 466 | |
29f7c58a | 467 | def extract_vote_count(kind, name): |
468 | return self._extract_count( | |
469 | (r'<span[^>]+\bclass="votes%s"[^>]*>([\d,\.]+)</span>' % kind, | |
470 | r'<span[^>]+\bclass=["\']votes%s["\'][^>]*\bdata-rating=["\'](\d+)' % kind), | |
471 | webpage, name) | |
472 | ||
7700207e | 473 | view_count = self._extract_count( |
540b9f51 | 474 | r'<span class="count">([\d,\.]+)</span> [Vv]iews', webpage, 'view') |
29f7c58a | 475 | like_count = extract_vote_count('Up', 'like') |
476 | dislike_count = extract_vote_count('Down', 'dislike') | |
0320ddc1 | 477 | comment_count = self._extract_count( |
7700207e | 478 | r'All Comments\s*<span>\(([\d,.]+)\)', webpage, 'comment') |
0320ddc1 | 479 | |
5dda1ede | 480 | def extract_list(meta_key): |
d2d970d0 | 481 | div = self._search_regex( |
5dda1ede S |
482 | r'(?s)<div[^>]+\bclass=["\'].*?\b%sWrapper[^>]*>(.+?)</div>' |
483 | % meta_key, webpage, meta_key, default=None) | |
d2d970d0 | 484 | if div: |
d0fb4bd1 | 485 | return [clean_html(x).strip() for x in re.findall(r'(?s)<a[^>]+\bhref=[^>]+>.+?</a>', div)] |
6bb05b32 | 486 | |
cd85a1bb S |
487 | info = self._search_json_ld(webpage, video_id, default={}) |
488 | # description provided in JSON-LD is irrelevant | |
489 | info['description'] = None | |
490 | ||
491 | return merge_dicts({ | |
125cfd78 | 492 | 'id': video_id, |
493 | 'uploader': video_uploader, | |
3661ebf2 | 494 | 'upload_date': upload_date, |
6c376029 | 495 | 'title': title, |
125cfd78 | 496 | 'thumbnail': thumbnail, |
ed8648a3 | 497 | 'duration': duration, |
0320ddc1 S |
498 | 'view_count': view_count, |
499 | 'like_count': like_count, | |
500 | 'dislike_count': dislike_count, | |
501 | 'comment_count': comment_count, | |
79367a98 | 502 | 'formats': formats, |
750e9833 | 503 | 'age_limit': 18, |
5dda1ede S |
504 | 'tags': extract_list('tags'), |
505 | 'categories': extract_list('categories'), | |
d0fb4bd1 | 506 | 'cast': extract_list('pornstars'), |
4938c8d5 | 507 | 'subtitles': subtitles, |
cd85a1bb | 508 | }, info) |
e66e1a00 S |
509 | |
510 | ||
71a1f617 | 511 | class PornHubPlaylistBaseIE(PornHubBaseIE): |
2181983a | 512 | def _extract_page(self, url): |
513 | return int_or_none(self._search_regex( | |
514 | r'\bpage=(\d+)', url, 'page', default=None)) | |
515 | ||
3430ff9b | 516 | def _extract_entries(self, webpage, host): |
475bcb22 S |
517 | # Only process container div with main playlist content skipping |
518 | # drop-down menu that uses similar pattern for videos (see | |
067aa17e | 519 | # https://github.com/ytdl-org/youtube-dl/issues/11594). |
475bcb22 S |
520 | container = self._search_regex( |
521 | r'(?s)(<div[^>]+class=["\']container.+)', webpage, | |
522 | 'container', default=webpage) | |
523 | ||
40e146aa | 524 | return [ |
3a23bae9 | 525 | self.url_result( |
3430ff9b | 526 | 'http://www.%s/%s' % (host, video_url), |
3a23bae9 S |
527 | PornHubIE.ie_key(), video_title=title) |
528 | for video_url, title in orderedSet(re.findall( | |
529 | r'href="/?(view_video\.php\?.*\bviewkey=[\da-z]+[^"]*)"[^>]*\s+title="([^"]+)"', | |
475bcb22 | 530 | container)) |
40e146aa | 531 | ] |
e66e1a00 | 532 | |
40e146aa | 533 | |
21b08463 | 534 | class PornHubUserIE(PornHubPlaylistBaseIE): |
ed807c18 | 535 | _VALID_URL = r'(?P<url>https?://(?:[^/]+\.)?%s/(?:(?:user|channel)s|model|pornstar)/(?P<id>[^/?#&]+))(?:[?#&]|/(?!videos)|$)' % PornHubBaseIE._PORNHUB_HOST_RE |
40e146aa | 536 | _TESTS = [{ |
21b08463 S |
537 | 'url': 'https://www.pornhub.com/model/zoe_ph', |
538 | 'playlist_mincount': 118, | |
539 | }, { | |
540 | 'url': 'https://www.pornhub.com/pornstar/liz-vicious', | |
40e146aa | 541 | 'info_dict': { |
21b08463 | 542 | 'id': 'liz-vicious', |
40e146aa | 543 | }, |
21b08463 S |
544 | 'playlist_mincount': 118, |
545 | }, { | |
546 | 'url': 'https://www.pornhub.com/users/russianveet69', | |
9634de17 | 547 | 'only_matching': True, |
21b08463 S |
548 | }, { |
549 | 'url': 'https://www.pornhub.com/channels/povd', | |
9634de17 S |
550 | 'only_matching': True, |
551 | }, { | |
552 | 'url': 'https://www.pornhub.com/model/zoe_ph?abc=1', | |
553 | 'only_matching': True, | |
2181983a | 554 | }, { |
555 | # Unavailable via /videos page, but available with direct pagination | |
556 | # on pornstar page (see [1]), requires premium | |
557 | # 1. https://github.com/ytdl-org/youtube-dl/issues/27853 | |
558 | 'url': 'https://www.pornhubpremium.com/pornstar/sienna-west', | |
559 | 'only_matching': True, | |
560 | }, { | |
561 | # Same as before, multi page | |
562 | 'url': 'https://www.pornhubpremium.com/pornstar/lily-labeau', | |
563 | 'only_matching': True, | |
ed807c18 | 564 | }, { |
4c968755 | 565 | 'url': 'https://pornhubvybmsymdol4iibwgwtkpwmeyd6luq2gxajgjzfjvotyt5zhyd.onion/model/zoe_ph', |
ed807c18 | 566 | 'only_matching': True, |
21b08463 S |
567 | }] |
568 | ||
21b08463 | 569 | def _real_extract(self, url): |
5ad28e7f | 570 | mobj = self._match_valid_url(url) |
21b08463 | 571 | user_id = mobj.group('id') |
2181983a | 572 | videos_url = '%s/videos' % mobj.group('url') |
573 | page = self._extract_page(url) | |
574 | if page: | |
575 | videos_url = update_url_query(videos_url, {'page': page}) | |
21b08463 | 576 | return self.url_result( |
2181983a | 577 | videos_url, ie=PornHubPagedVideoListIE.ie_key(), video_id=user_id) |
21b08463 S |
578 | |
579 | ||
580 | class PornHubPagedPlaylistBaseIE(PornHubPlaylistBaseIE): | |
4bf568d3 S |
581 | @staticmethod |
582 | def _has_more(webpage): | |
583 | return re.search( | |
584 | r'''(?x) | |
585 | <li[^>]+\bclass=["\']page_next| | |
586 | <link[^>]+\brel=["\']next| | |
587 | <button[^>]+\bid=["\']moreDataBtn | |
588 | ''', webpage) is not None | |
589 | ||
2181983a | 590 | def _entries(self, url, host, item_id): |
591 | page = self._extract_page(url) | |
21b08463 | 592 | |
2181983a | 593 | VIDEOS = '/videos' |
594 | ||
595 | def download_page(base_url, num, fallback=False): | |
596 | note = 'Downloading page %d%s' % (num, ' (switch to fallback)' if fallback else '') | |
597 | return self._download_webpage( | |
598 | base_url, item_id, note, query={'page': num}) | |
1f7a563a | 599 | |
2181983a | 600 | def is_404(e): |
601 | return isinstance(e.cause, compat_HTTPError) and e.cause.code == 404 | |
602 | ||
603 | base_url = url | |
604 | has_page = page is not None | |
605 | first_page = page if has_page else 1 | |
606 | for page_num in (first_page, ) if has_page else itertools.count(first_page): | |
21b08463 | 607 | try: |
2181983a | 608 | try: |
609 | webpage = download_page(base_url, page_num) | |
610 | except ExtractorError as e: | |
611 | # Some sources may not be available via /videos page, | |
612 | # trying to fallback to main page pagination (see [1]) | |
613 | # 1. https://github.com/ytdl-org/youtube-dl/issues/27853 | |
614 | if is_404(e) and page_num == first_page and VIDEOS in base_url: | |
615 | base_url = base_url.replace(VIDEOS, '') | |
616 | webpage = download_page(base_url, page_num, fallback=True) | |
617 | else: | |
618 | raise | |
21b08463 | 619 | except ExtractorError as e: |
2181983a | 620 | if is_404(e) and page_num != first_page: |
21b08463 S |
621 | break |
622 | raise | |
623 | page_entries = self._extract_entries(webpage, host) | |
624 | if not page_entries: | |
625 | break | |
2181983a | 626 | for e in page_entries: |
627 | yield e | |
21b08463 S |
628 | if not self._has_more(webpage): |
629 | break | |
630 | ||
2181983a | 631 | def _real_extract(self, url): |
5ad28e7f | 632 | mobj = self._match_valid_url(url) |
2181983a | 633 | host = mobj.group('host') |
634 | item_id = mobj.group('id') | |
635 | ||
636 | self._login(host) | |
637 | ||
638 | return self.playlist_result(self._entries(url, host, item_id), item_id) | |
21b08463 S |
639 | |
640 | ||
9634de17 | 641 | class PornHubPagedVideoListIE(PornHubPagedPlaylistBaseIE): |
df2a5633 | 642 | _VALID_URL = r'https?://(?:[^/]+\.)?%s/(?!playlist/)(?P<id>(?:[^/]+/)*[^/?#&]+)' % PornHubBaseIE._PORNHUB_HOST_RE |
21b08463 | 643 | _TESTS = [{ |
1f7a563a | 644 | 'url': 'https://www.pornhub.com/model/zoe_ph/videos', |
21b08463 | 645 | 'only_matching': True, |
34541395 S |
646 | }, { |
647 | 'url': 'http://www.pornhub.com/users/rushandlia/videos', | |
648 | 'only_matching': True, | |
21b08463 S |
649 | }, { |
650 | 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos', | |
651 | 'info_dict': { | |
9634de17 | 652 | 'id': 'pornstar/jenny-blighe/videos', |
21b08463 S |
653 | }, |
654 | 'playlist_mincount': 149, | |
1f7a563a S |
655 | }, { |
656 | 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos?page=3', | |
657 | 'info_dict': { | |
9634de17 | 658 | 'id': 'pornstar/jenny-blighe/videos', |
1f7a563a S |
659 | }, |
660 | 'playlist_mincount': 40, | |
f66df20c PV |
661 | }, { |
662 | # default sorting as Top Rated Videos | |
663 | 'url': 'https://www.pornhub.com/channels/povd/videos', | |
664 | 'info_dict': { | |
9634de17 | 665 | 'id': 'channels/povd/videos', |
f66df20c PV |
666 | }, |
667 | 'playlist_mincount': 293, | |
668 | }, { | |
669 | # Top Rated Videos | |
670 | 'url': 'https://www.pornhub.com/channels/povd/videos?o=ra', | |
671 | 'only_matching': True, | |
672 | }, { | |
673 | # Most Recent Videos | |
674 | 'url': 'https://www.pornhub.com/channels/povd/videos?o=da', | |
675 | 'only_matching': True, | |
676 | }, { | |
677 | # Most Viewed Videos | |
678 | 'url': 'https://www.pornhub.com/channels/povd/videos?o=vi', | |
679 | 'only_matching': True, | |
92ded33a S |
680 | }, { |
681 | 'url': 'http://www.pornhub.com/users/zoe_ph/videos/public', | |
682 | 'only_matching': True, | |
21b08463 S |
683 | }, { |
684 | # Most Viewed Videos | |
685 | 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=mv', | |
686 | 'only_matching': True, | |
687 | }, { | |
688 | # Top Rated Videos | |
689 | 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=tr', | |
690 | 'only_matching': True, | |
691 | }, { | |
692 | # Longest Videos | |
693 | 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=lg', | |
694 | 'only_matching': True, | |
695 | }, { | |
696 | # Newest Videos | |
697 | 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=cm', | |
698 | 'only_matching': True, | |
21b08463 S |
699 | }, { |
700 | 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos/paid', | |
701 | 'only_matching': True, | |
702 | }, { | |
703 | 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos/fanonly', | |
704 | 'only_matching': True, | |
9634de17 S |
705 | }, { |
706 | 'url': 'https://www.pornhub.com/video', | |
707 | 'only_matching': True, | |
708 | }, { | |
709 | 'url': 'https://www.pornhub.com/video?page=3', | |
710 | 'only_matching': True, | |
711 | }, { | |
712 | 'url': 'https://www.pornhub.com/video/search?search=123', | |
713 | 'only_matching': True, | |
714 | }, { | |
715 | 'url': 'https://www.pornhub.com/categories/teen', | |
716 | 'only_matching': True, | |
717 | }, { | |
718 | 'url': 'https://www.pornhub.com/categories/teen?page=3', | |
719 | 'only_matching': True, | |
720 | }, { | |
721 | 'url': 'https://www.pornhub.com/hd', | |
722 | 'only_matching': True, | |
723 | }, { | |
724 | 'url': 'https://www.pornhub.com/hd?page=3', | |
725 | 'only_matching': True, | |
726 | }, { | |
727 | 'url': 'https://www.pornhub.com/described-video', | |
728 | 'only_matching': True, | |
729 | }, { | |
730 | 'url': 'https://www.pornhub.com/described-video?page=2', | |
731 | 'only_matching': True, | |
732 | }, { | |
733 | 'url': 'https://www.pornhub.com/video/incategories/60fps-1/hd-porn', | |
734 | 'only_matching': True, | |
ed807c18 | 735 | }, { |
4c968755 | 736 | 'url': 'https://pornhubvybmsymdol4iibwgwtkpwmeyd6luq2gxajgjzfjvotyt5zhyd.onion/model/zoe_ph/videos', |
ed807c18 | 737 | 'only_matching': True, |
40e146aa S |
738 | }] |
739 | ||
21b08463 S |
740 | @classmethod |
741 | def suitable(cls, url): | |
742 | return (False | |
9634de17 S |
743 | if PornHubIE.suitable(url) or PornHubUserIE.suitable(url) or PornHubUserVideosUploadIE.suitable(url) |
744 | else super(PornHubPagedVideoListIE, cls).suitable(url)) | |
21b08463 | 745 | |
34541395 | 746 | |
21b08463 | 747 | class PornHubUserVideosUploadIE(PornHubPagedPlaylistBaseIE): |
ed807c18 | 748 | _VALID_URL = r'(?P<url>https?://(?:[^/]+\.)?%s/(?:(?:user|channel)s|model|pornstar)/(?P<id>[^/]+)/videos/upload)' % PornHubBaseIE._PORNHUB_HOST_RE |
21b08463 S |
749 | _TESTS = [{ |
750 | 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos/upload', | |
751 | 'info_dict': { | |
752 | 'id': 'jenny-blighe', | |
753 | }, | |
754 | 'playlist_mincount': 129, | |
755 | }, { | |
756 | 'url': 'https://www.pornhub.com/model/zoe_ph/videos/upload', | |
757 | 'only_matching': True, | |
ed807c18 | 758 | }, { |
4c968755 | 759 | 'url': 'http://pornhubvybmsymdol4iibwgwtkpwmeyd6luq2gxajgjzfjvotyt5zhyd.onion/pornstar/jenny-blighe/videos/upload', |
ed807c18 | 760 | 'only_matching': True, |
21b08463 | 761 | }] |
df2a5633 | 762 | |
763 | ||
764 | class PornHubPlaylistIE(PornHubPlaylistBaseIE): | |
765 | _VALID_URL = r'(?P<url>https?://(?:[^/]+\.)?%s/playlist/(?P<id>[^/?#&]+))' % PornHubBaseIE._PORNHUB_HOST_RE | |
766 | _TESTS = [{ | |
767 | 'url': 'https://www.pornhub.com/playlist/44121572', | |
768 | 'info_dict': { | |
769 | 'id': '44121572', | |
770 | }, | |
771 | 'playlist_count': 77, | |
772 | }, { | |
773 | 'url': 'https://www.pornhub.com/playlist/4667351', | |
774 | 'only_matching': True, | |
775 | }, { | |
776 | 'url': 'https://de.pornhub.com/playlist/4667351', | |
777 | 'only_matching': True, | |
778 | }, { | |
779 | 'url': 'https://de.pornhub.com/playlist/4667351?page=2', | |
780 | 'only_matching': True, | |
781 | }] | |
782 | ||
783 | def _entries(self, url, host, item_id): | |
784 | webpage = self._download_webpage(url, item_id, 'Downloading page 1') | |
785 | playlist_id = self._search_regex(r'var\s+playlistId\s*=\s*"([^"]+)"', webpage, 'playlist_id') | |
786 | video_count = int_or_none( | |
787 | self._search_regex(r'var\s+itemsCount\s*=\s*([0-9]+)\s*\|\|', webpage, 'video_count')) | |
788 | token = self._search_regex(r'var\s+token\s*=\s*"([^"]+)"', webpage, 'token') | |
789 | page_count = math.ceil((video_count - 36) / 40.) + 1 | |
790 | page_entries = self._extract_entries(webpage, host) | |
791 | ||
792 | def download_page(page_num): | |
793 | note = 'Downloading page {}'.format(page_num) | |
794 | page_url = 'https://www.{}/playlist/viewChunked'.format(host) | |
795 | return self._download_webpage(page_url, item_id, note, query={ | |
796 | 'id': playlist_id, | |
797 | 'page': page_num, | |
798 | 'token': token, | |
799 | }) | |
800 | ||
801 | for page_num in range(1, page_count + 1): | |
802 | if page_num > 1: | |
803 | webpage = download_page(page_num) | |
804 | page_entries = self._extract_entries(webpage, host) | |
805 | if not page_entries: | |
806 | break | |
807 | for e in page_entries: | |
808 | yield e | |
809 | ||
810 | def _real_extract(self, url): | |
5ad28e7f | 811 | mobj = self._match_valid_url(url) |
df2a5633 | 812 | host = mobj.group('host') |
813 | item_id = mobj.group('id') | |
814 | ||
815 | self._login(host) | |
816 | ||
817 | return self.playlist_result(self._entries(mobj.group('url'), host, item_id), item_id) |