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