2 from __future__
import unicode_literals
9 from .common
import InfoExtractor
10 from ..compat
import (
13 compat_urllib_request
,
15 from .openload
import PhantomJSwrapper
32 class PornHubBaseIE(InfoExtractor
):
33 _NETRC_MACHINE
= 'pornhub'
34 _PORNHUB_HOST_RE
= r
'(?:(?P<host>pornhub(?:premium)?\.(?:com|net|org))|pornhubthbh7ap3u\.onion)'
36 def _download_webpage_handle(self
, *args
, **kwargs
):
37 def dl(*args
, **kwargs
):
38 return super(PornHubBaseIE
, self
)._download
_webpage
_handle
(*args
, **kwargs
)
40 ret
= dl(*args
, **kwargs
)
47 if any(re
.search(p
, webpage
) for p
in (
48 r
'<body\b[^>]+\bonload=["\']go\
(\
)',
49 r'document\
.cookie\s
*=\s
*["\']RNKEY=',
50 r'document\.location\.reload\(true\)')):
51 url_or_request = args[0]
52 url = (url_or_request.get_full_url()
53 if isinstance(url_or_request, compat_urllib_request.Request)
55 phantom = PhantomJSwrapper(self, required_version='2.0')
56 phantom.get(url, html=webpage)
57 webpage, urlh = dl(*args, **kwargs)
61 def _real_initialize(self):
62 self._logged_in = False
64 def _login(self, host):
68 site = host.split('.')[0]
70 # Both sites pornhub and pornhubpremium have separate accounts
71 # so there should be an option to provide credentials for both.
72 # At the same time some videos are available under the same video id
73 # on both sites so that we have to identify them as the same video.
74 # For that purpose we have to keep both in the same extractor
75 # but under different netrc machines.
76 username, password = self._get_login_info(netrc_machine=site)
80 login_url = 'https://www.%s/%slogin' % (host, 'premium/' if 'premium' in host else '')
81 login_page = self._download_webpage(
82 login_url, None, 'Downloading %s login page' % site)
84 def is_logged(webpage):
85 return any(re.search(p, webpage) for p in (
86 r'class=["\']signOut
',
87 r'>Sign\s
+[Oo
]ut\s
*<'))
89 if is_logged(login_page):
90 self._logged_in = True
93 login_form = self._hidden_inputs(login_page)
100 response = self._download_json(
101 'https
://www
.%s/front
/authenticate
' % host, None,
102 'Logging
in to
%s' % site,
103 data=urlencode_postdata(login_form),
105 'Content
-Type
': 'application
/x
-www
-form
-urlencoded
; charset
=UTF
-8',
106 'Referer
': login_url,
107 'X
-Requested
-With
': 'XMLHttpRequest
',
110 if response.get('success
') == '1':
111 self._logged_in = True
114 message = response.get('message
')
115 if message is not None:
116 raise ExtractorError(
117 'Unable to login
: %s' % message, expected=True)
119 raise ExtractorError('Unable to log
in')
122 class PornHubIE(PornHubBaseIE):
123 IE_DESC = 'PornHub
and Thumbzilla
'
124 _VALID_URL = r'''(?x)
129 /(?:(?:view_video\.php|video/show)\?viewkey=|embed/)|
130 (?:www\.)?thumbzilla\.com/video/
133 ''' % PornHubBaseIE._PORNHUB_HOST_RE
135 'url
': 'http
://www
.pornhub
.com
/view_video
.php?viewkey
=648719015',
136 'md5
': 'a6391306d050e4547f62b3f485dd9ba9
',
140 'title
': 'Seductive Indian beauty strips down
and fingers her pink pussy
',
142 'upload_date
': '20130628',
143 'timestamp
': 1372447216,
147 'dislike_count
': int,
148 'comment_count
': int,
156 'url
': 'http
://www
.pornhub
.com
/view_video
.php?viewkey
=1331683002',
161 'upload_date
': '20150213',
162 'timestamp
': 1423804862,
166 'dislike_count
': int,
167 'comment_count
': int,
173 'skip_download
': True,
175 'skip
': 'Video has been flagged
for verification
in accordance
with our trust
and safety policy
',
178 'url
': 'https
://www
.pornhub
.com
/view_video
.php?viewkey
=ph5af5fef7c2aa7
',
180 'id': 'ph5af5fef7c2aa7
',
182 'title
': 'BFFS
- Cute Teen Girls Share Cock On the Floor
',
187 'dislike_count
': int,
188 'comment_count
': int,
199 'skip_download
': True,
201 'skip
': 'This video has been disabled
',
203 'url
': 'http
://www
.pornhub
.com
/view_video
.php?viewkey
=ph557bbb6676d2d
',
204 'only_matching
': True,
206 # removed at the request of cam4.com
207 'url
': 'http
://fr
.pornhub
.com
/view_video
.php?viewkey
=ph55ca2f9760862
',
208 'only_matching
': True,
210 # removed at the request of the copyright owner
211 'url
': 'http
://www
.pornhub
.com
/view_video
.php?viewkey
=788152859',
212 'only_matching
': True,
214 # removed by uploader
215 'url
': 'http
://www
.pornhub
.com
/view_video
.php?viewkey
=ph572716d15a111
',
216 'only_matching
': True,
219 'url
': 'http
://www
.pornhub
.com
/view_video
.php?viewkey
=ph56fd731fce6b7
',
220 'only_matching
': True,
222 'url
': 'https
://www
.thumbzilla
.com
/video
/ph56c6114abd99a
/horny
-girlfriend
-sex
',
223 'only_matching
': True,
225 'url
': 'http
://www
.pornhub
.com
/video
/show?viewkey
=648719015',
226 'only_matching
': True,
228 'url
': 'https
://www
.pornhub
.net
/view_video
.php?viewkey
=203640933',
229 'only_matching
': True,
231 'url
': 'https
://www
.pornhub
.org
/view_video
.php?viewkey
=203640933',
232 'only_matching
': True,
234 'url
': 'https
://www
.pornhubpremium
.com
/view_video
.php?viewkey
=ph5e4acdae54a82
',
235 'only_matching
': True,
237 # Some videos are available with the same id on both premium
238 # and non-premium sites (e.g. this and the following test)
239 'url
': 'https
://www
.pornhub
.com
/view_video
.php?viewkey
=ph5f75b0f4b18e3
',
240 'only_matching
': True,
242 'url
': 'https
://www
.pornhubpremium
.com
/view_video
.php?viewkey
=ph5f75b0f4b18e3
',
243 'only_matching
': True,
246 'url
': 'https
://www
.pornhub
.com
/view_video
.php?viewkey
=ph5a9813bfa7156
',
247 'only_matching
': True,
249 'url
': 'http
://pornhubthbh7ap3u
.onion
/view_video
.php?viewkey
=ph5a9813bfa7156
',
250 'only_matching
': True,
254 def _extract_urls(webpage):
256 r'<iframe
[^
>]+?src
=["\'](?P<url>(?:https?:)?//(?:www\.)?pornhub(?:premium)?\.(?:com|net|org)/embed/[\da-z]+)',
259 def _extract_count(self, pattern, webpage, name):
260 return str_to_int(self._search_regex(
261 pattern, webpage, '%s count' % name, fatal=False))
263 def _real_extract(self, url):
264 mobj = re.match(self._VALID_URL, url)
265 host = mobj.group('host') or 'pornhub.com'
266 video_id = mobj.group('id')
270 self._set_cookie(host, 'age_verified', '1')
272 def dl_webpage(platform):
273 self._set_cookie(host, 'platform', platform)
274 return self._download_webpage(
275 'https://www.%s/view_video.php?viewkey=%s' % (host, video_id),
276 video_id, 'Downloading %s webpage' % platform)
278 webpage = dl_webpage('pc')
280 error_msg = self._html_search_regex(
281 (r'(?s)<div[^>]+class=(["\'])(?
:(?
!\
1).)*\b(?
:removed|userMessageSection
)\b(?
:(?
!\
1).)*\
1[^
>]*>(?P
<error
>.+?
)</div
>',
282 r'(?s
)<section
[^
>]+class=["\']noVideo["\'][^
>]*>(?P
<error
>.+?
)</section
>'),
283 webpage, 'error message
', default=None, group='error
')
285 error_msg = re.sub(r'\s
+', ' ', error_msg)
286 raise ExtractorError(
287 'PornHub said
: %s' % error_msg,
288 expected=True, video_id=video_id)
290 if any(re.search(p, webpage) for p in (
291 r'class=["\']geoBlocked["\']',
292 r'>\s
*This content
is unavailable
in your country
')):
293 self.raise_geo_restricted()
295 # video_title from flashvars contains whitespace instead of non-ASCII (see
296 # http://www.pornhub.com/view_video.php?viewkey=1331683002), not relying
298 title = self._html_search_meta(
299 'twitter
:title
', webpage, default=None) or self._html_search_regex(
300 (r'(?s
)<h1
[^
>]+class=["\']title["\'][^
>]*>(?P
<title
>.+?
)</h1
>',
301 r'<div
[^
>]+data
-video
-title
=(["\'])(?P<title>(?:(?!\1).)+)\1',
302 r'shareTitle["\']\s
*[=:]\s
*(["\'])(?P<title>(?:(?!\1).)+)\1'),
303 webpage, 'title', group='title')
306 video_urls_set = set()
309 flashvars = self._parse_json(
311 r'var\s+flashvars_\d+\s*=\s*({.+?});', webpage, 'flashvars', default='{}'),
314 subtitle_url = url_or_none(flashvars.get('closedCaptionsFile'))
316 subtitles.setdefault('en', []).append({
320 thumbnail = flashvars.get('image_url')
321 duration = int_or_none(flashvars.get('video_duration'))
322 media_definitions = flashvars.get('mediaDefinitions')
323 if isinstance(media_definitions, list):
324 for definition in media_definitions:
325 if not isinstance(definition, dict):
327 video_url = definition.get('videoUrl')
328 if not video_url or not isinstance(video_url, compat_str):
330 if video_url in video_urls_set:
332 video_urls_set.add(video_url)
334 (video_url, int_or_none(definition.get('quality'))))
336 thumbnail, duration = [None] * 2
338 def extract_js_vars(webpage, pattern, default=NO_DEFAULT):
339 assignments = self._search_regex(
340 pattern, webpage, 'encoded url', default=default)
344 assignments = assignments.split(';')
348 def parse_js_value(inp):
349 inp = re.sub(r'/\*(?:(?!\*/).)*?\*/', '', inp)
351 inps = inp.split('+')
352 return functools.reduce(
353 operator.concat, map(parse_js_value, inps))
357 return remove_quotes(inp)
359 for assn in assignments:
363 assn = re.sub(r'var\s+', '', assn)
364 vname, value = assn.split('=', 1)
365 js_vars[vname] = parse_js_value(value)
368 def add_video_url(video_url):
369 v_url = url_or_none(video_url)
372 if v_url in video_urls_set:
374 video_urls.append((v_url, None))
375 video_urls_set.add(v_url)
377 def parse_quality_items(quality_items):
378 q_items = self._parse_json(quality_items, video_id, fatal=False)
379 if not isinstance(q_items, list):
382 if isinstance(item, dict):
383 add_video_url(item.get('url'))
386 FORMAT_PREFIXES = ('media', 'quality', 'qualityItems')
387 js_vars = extract_js_vars(
388 webpage, r'(var\s+(?:%s)_.+)' % '|'.join(FORMAT_PREFIXES),
391 for key, format_url in js_vars.items():
392 if key.startswith(FORMAT_PREFIXES[-1]):
393 parse_quality_items(format_url)
394 elif any(key.startswith(p) for p in FORMAT_PREFIXES[:2]):
395 add_video_url(format_url)
396 if not video_urls and re.search(
397 r'<[^>]+\bid=["\']lockedPlayer
', webpage):
398 raise ExtractorError(
399 'Video
%s is locked
' % video_id, expected=True)
402 js_vars = extract_js_vars(
403 dl_webpage('tv
'), r'(var
.+?mediastring
.+?
)</script
>')
404 add_video_url(js_vars['mediastring
'])
406 for mobj in re.finditer(
407 r'<a
[^
>]+\bclass
=["\']downloadBtn\b[^>]+\bhref=(["\'])(?P
<url
>(?
:(?
!\
1).)+)\
1',
409 video_url = mobj.group('url
')
410 if video_url not in video_urls_set:
411 video_urls.append((video_url, None))
412 video_urls_set.add(video_url)
417 def add_format(format_url, height=None):
418 ext = determine_ext(format_url)
420 formats.extend(self._extract_mpd_formats(
421 format_url, video_id, mpd_id='dash
', fatal=False))
424 formats.extend(self._extract_m3u8_formats(
425 format_url, video_id, 'mp4
', entry_protocol='m3u8_native
',
426 m3u8_id='hls
', fatal=False))
429 height = int_or_none(self._search_regex(
430 r'(?P
<height
>\d
+)[pP
]?_\d
+[kK
]', format_url, 'height
',
434 'format_id
': '%dp
' % height if height else None,
438 for video_url, height in video_urls:
440 upload_date = self._search_regex(
441 r'/(\d{6}
/\d{2}
)/', video_url, 'upload data
', default=None)
443 upload_date = upload_date.replace('/', '')
444 if '/video
/get_media
' in video_url:
445 medias = self._download_json(video_url, video_id, fatal=False)
446 if isinstance(medias, list):
448 if not isinstance(media, dict):
450 video_url = url_or_none(media.get('videoUrl
'))
453 height = int_or_none(media.get('quality
'))
454 add_format(video_url, height)
456 add_format(video_url)
458 # field_preference is unnecessary here, but kept for code-similarity with youtube-dl
460 formats, field_preference=('height
', 'width
', 'fps
', 'format_id
'))
462 video_uploader = self._html_search_regex(
463 r'(?s
)From
: 
;.+?
<(?
:a
\b[^
>]+\bhref
=["\']/(?:(?:user|channel)s|model|pornstar)/|span\b[^>]+\bclass=["\']username
)[^
>]+>(.+?
)<',
464 webpage, 'uploader
', default=None)
466 def extract_vote_count(kind, name):
467 return self._extract_count(
468 (r'<span
[^
>]+\bclass
="votes%s"[^
>]*>([\d
,\
.]+)</span
>' % kind,
469 r'<span
[^
>]+\bclass
=["\']votes%s["\'][^
>]*\bdata
-rating
=["\'](\d+)' % kind),
472 view_count = self._extract_count(
473 r'<span class="count
">([\d,\.]+)</span> [Vv]iews', webpage, 'view')
474 like_count = extract_vote_count('Up', 'like')
475 dislike_count = extract_vote_count('Down', 'dislike')
476 comment_count = self._extract_count(
477 r'All Comments\s*<span>\(([\d,.]+)\)', webpage, 'comment')
479 def extract_list(meta_key):
480 div = self._search_regex(
481 r'(?s)<div[^>]+\bclass=["\'].*?
\b%sWrapper
[^
>]*>(.+?
)</div
>'
482 % meta_key, webpage, meta_key, default=None)
484 return [clean_html(x).strip() for x in re.findall(r'(?s
)<a
[^
>]+\bhref
=[^
>]+>.+?
</a
>', div)]
486 info = self._search_json_ld(webpage, video_id, default={})
487 # description provided in JSON-LD is irrelevant
488 info['description
'] = None
492 'uploader
': video_uploader,
493 'upload_date
': upload_date,
495 'thumbnail
': thumbnail,
496 'duration
': duration,
497 'view_count
': view_count,
498 'like_count
': like_count,
499 'dislike_count
': dislike_count,
500 'comment_count
': comment_count,
503 'tags
': extract_list('tags
'),
504 'categories
': extract_list('categories
'),
505 'cast
': extract_list('pornstars
'),
506 'subtitles
': subtitles,
510 class PornHubPlaylistBaseIE(PornHubBaseIE):
511 def _extract_page(self, url):
512 return int_or_none(self._search_regex(
513 r'\bpage
=(\d
+)', url, 'page
', default=None))
515 def _extract_entries(self, webpage, host):
516 # Only process container div with main playlist content skipping
517 # drop-down menu that uses similar pattern for videos (see
518 # https://github.com/ytdl-org/youtube-dl/issues/11594).
519 container = self._search_regex(
520 r'(?s
)(<div
[^
>]+class=["\']container.+)', webpage,
521 'container', default=webpage)
525 'http://www.%s/%s' % (host, video_url),
526 PornHubIE.ie_key(), video_title=title)
527 for video_url, title in orderedSet(re.findall(
528 r'href="/?
(view_video\
.php
\?.*\bviewkey
=[\da
-z
]+[^
"]*)"[^
>]*\s
+title
="([^"]+)"',
533 class PornHubUserIE(PornHubPlaylistBaseIE):
534 _VALID_URL = r'(?P<url>https?://(?:[^/]+\.)?%s/(?:(?:user|channel)s|model|pornstar)/(?P<id>[^/?#&]+))(?:[?#&]|/(?!videos)|$)' % PornHubBaseIE._PORNHUB_HOST_RE
536 'url': 'https://www.pornhub.com/model/zoe_ph',
537 'playlist_mincount': 118,
539 'url': 'https://www.pornhub.com/pornstar/liz-vicious',
543 'playlist_mincount': 118,
545 'url': 'https://www.pornhub.com/users/russianveet69',
546 'only_matching': True,
548 'url': 'https://www.pornhub.com/channels/povd',
549 'only_matching': True,
551 'url': 'https://www.pornhub.com/model/zoe_ph?abc=1',
552 'only_matching': True,
554 # Unavailable via /videos page, but available with direct pagination
555 # on pornstar page (see [1]), requires premium
556 # 1. https://github.com/ytdl-org/youtube-dl/issues/27853
557 'url': 'https://www.pornhubpremium.com/pornstar/sienna-west',
558 'only_matching': True,
560 # Same as before, multi page
561 'url': 'https://www.pornhubpremium.com/pornstar/lily-labeau',
562 'only_matching': True,
564 'url': 'https://pornhubthbh7ap3u.onion/model/zoe_ph',
565 'only_matching': True,
568 def _real_extract(self, url):
569 mobj = re.match(self._VALID_URL, url)
570 user_id = mobj.group('id')
571 videos_url = '%s/videos' % mobj.group('url')
572 page = self._extract_page(url)
574 videos_url = update_url_query(videos_url, {'page': page})
575 return self.url_result(
576 videos_url, ie=PornHubPagedVideoListIE.ie_key(), video_id=user_id)
579 class PornHubPagedPlaylistBaseIE(PornHubPlaylistBaseIE):
581 def _has_more(webpage):
584 <li[^>]+\bclass=["\']page_next|
585 <link
[^
>]+\brel
=["\']next|
586 <button[^>]+\bid=["\']moreDataBtn
587 ''', webpage) is not None
589 def _entries(self, url, host, item_id):
590 page = self._extract_page(url)
594 def download_page(base_url, num, fallback=False):
595 note = 'Downloading page %d%s' % (num, ' (switch to fallback)' if fallback else '')
596 return self._download_webpage(
597 base_url, item_id, note, query={'page': num})
600 return isinstance(e.cause, compat_HTTPError) and e.cause.code == 404
603 has_page = page is not None
604 first_page = page if has_page else 1
605 for page_num in (first_page, ) if has_page else itertools.count(first_page):
608 webpage = download_page(base_url, page_num)
609 except ExtractorError as e:
610 # Some sources may not be available via /videos page,
611 # trying to fallback to main page pagination (see [1])
612 # 1. https://github.com/ytdl-org/youtube-dl/issues/27853
613 if is_404(e) and page_num == first_page and VIDEOS in base_url:
614 base_url = base_url.replace(VIDEOS, '')
615 webpage = download_page(base_url, page_num, fallback=True)
618 except ExtractorError as e:
619 if is_404(e) and page_num != first_page:
622 page_entries = self._extract_entries(webpage, host)
625 for e in page_entries:
627 if not self._has_more(webpage):
630 def _real_extract(self, url):
631 mobj = re.match(self._VALID_URL, url)
632 host = mobj.group('host')
633 item_id = mobj.group('id')
637 return self.playlist_result(self._entries(url, host, item_id), item_id)
640 class PornHubPagedVideoListIE(PornHubPagedPlaylistBaseIE):
641 _VALID_URL = r'https?://(?:[^/]+\.)?%s/(?P<id>(?:[^/]+/)*[^/?#&]+)' % PornHubBaseIE._PORNHUB_HOST_RE
643 'url': 'https://www.pornhub.com/model/zoe_ph/videos',
644 'only_matching': True,
646 'url': 'http://www.pornhub.com/users/rushandlia/videos',
647 'only_matching': True,
649 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos',
651 'id': 'pornstar/jenny-blighe/videos',
653 'playlist_mincount': 149,
655 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos?page=3',
657 'id': 'pornstar/jenny-blighe/videos',
659 'playlist_mincount': 40,
661 # default sorting as Top Rated Videos
662 'url': 'https://www.pornhub.com/channels/povd/videos',
664 'id': 'channels/povd/videos',
666 'playlist_mincount': 293,
669 'url': 'https://www.pornhub.com/channels/povd/videos?o=ra',
670 'only_matching': True,
673 'url': 'https://www.pornhub.com/channels/povd/videos?o=da',
674 'only_matching': True,
677 'url': 'https://www.pornhub.com/channels/povd/videos?o=vi',
678 'only_matching': True,
680 'url': 'http://www.pornhub.com/users/zoe_ph/videos/public',
681 'only_matching': True,
684 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=mv',
685 'only_matching': True,
688 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=tr',
689 'only_matching': True,
692 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=lg',
693 'only_matching': True,
696 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=cm',
697 'only_matching': True,
699 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos/paid',
700 'only_matching': True,
702 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos/fanonly',
703 'only_matching': True,
705 'url': 'https://www.pornhub.com/video',
706 'only_matching': True,
708 'url': 'https://www.pornhub.com/video?page=3',
709 'only_matching': True,
711 'url': 'https://www.pornhub.com/video/search?search=123',
712 'only_matching': True,
714 'url': 'https://www.pornhub.com/categories/teen',
715 'only_matching': True,
717 'url': 'https://www.pornhub.com/categories/teen?page=3',
718 'only_matching': True,
720 'url': 'https://www.pornhub.com/hd',
721 'only_matching': True,
723 'url': 'https://www.pornhub.com/hd?page=3',
724 'only_matching': True,
726 'url': 'https://www.pornhub.com/described-video',
727 'only_matching': True,
729 'url': 'https://www.pornhub.com/described-video?page=2',
730 'only_matching': True,
732 'url': 'https://www.pornhub.com/video/incategories/60fps-1/hd-porn',
733 'only_matching': True,
735 'url': 'https://www.pornhub.com/playlist/44121572',
737 'id': 'playlist/44121572',
739 'playlist_mincount': 132,
741 'url': 'https://www.pornhub.com/playlist/4667351',
742 'only_matching': True,
744 'url': 'https://de.pornhub.com/playlist/4667351',
745 'only_matching': True,
747 'url': 'https://pornhubthbh7ap3u.onion/model/zoe_ph/videos',
748 'only_matching': True,
752 def suitable(cls, url):
754 if PornHubIE.suitable(url) or PornHubUserIE.suitable(url) or PornHubUserVideosUploadIE.suitable(url)
755 else super(PornHubPagedVideoListIE, cls).suitable(url))
758 class PornHubUserVideosUploadIE(PornHubPagedPlaylistBaseIE):
759 _VALID_URL = r'(?P<url>https?://(?:[^/]+\.)?%s/(?:(?:user|channel)s|model|pornstar)/(?P<id>[^/]+)/videos/upload)' % PornHubBaseIE._PORNHUB_HOST_RE
761 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos/upload',
763 'id': 'jenny-blighe',
765 'playlist_mincount': 129,
767 'url': 'https://www.pornhub.com/model/zoe_ph/videos/upload',
768 'only_matching': True,
770 'url': 'http://pornhubthbh7ap3u.onion/pornstar/jenny-blighe/videos/upload',
771 'only_matching': True,