2 from __future__
import unicode_literals
9 from .common
import InfoExtractor
10 from ..compat
import (
13 compat_urllib_request
,
15 from .openload
import PhantomJSwrapper
31 class PornHubBaseIE(InfoExtractor
):
32 _NETRC_MACHINE
= 'pornhub'
34 def _download_webpage_handle(self
, *args
, **kwargs
):
35 def dl(*args
, **kwargs
):
36 return super(PornHubBaseIE
, self
)._download
_webpage
_handle
(*args
, **kwargs
)
38 ret
= dl(*args
, **kwargs
)
45 if any(re
.search(p
, webpage
) for p
in (
46 r
'<body\b[^>]+\bonload=["\']go\
(\
)',
47 r'document\
.cookie\s
*=\s
*["\']RNKEY=',
48 r'document\.location\.reload\(true\)')):
49 url_or_request = args[0]
50 url = (url_or_request.get_full_url()
51 if isinstance(url_or_request, compat_urllib_request.Request)
53 phantom = PhantomJSwrapper(self, required_version='2.0')
54 phantom.get(url, html=webpage)
55 webpage, urlh = dl(*args, **kwargs)
59 def _real_initialize(self):
60 self._logged_in = False
62 def _login(self, host):
66 site = host.split('.')[0]
68 # Both sites pornhub and pornhubpremium have separate accounts
69 # so there should be an option to provide credentials for both.
70 # At the same time some videos are available under the same video id
71 # on both sites so that we have to identify them as the same video.
72 # For that purpose we have to keep both in the same extractor
73 # but under different netrc machines.
74 username, password = self._get_login_info(netrc_machine=site)
78 login_url = 'https://www.%s/%slogin' % (host, 'premium/' if 'premium' in host else '')
79 login_page = self._download_webpage(
80 login_url, None, 'Downloading %s login page' % site)
82 def is_logged(webpage):
83 return any(re.search(p, webpage) for p in (
84 r'class=["\']signOut
',
85 r'>Sign\s
+[Oo
]ut\s
*<'))
87 if is_logged(login_page):
88 self._logged_in = True
91 login_form = self._hidden_inputs(login_page)
98 response = self._download_json(
99 'https
://www
.%s/front
/authenticate
' % host, None,
100 'Logging
in to
%s' % site,
101 data=urlencode_postdata(login_form),
103 'Content
-Type
': 'application
/x
-www
-form
-urlencoded
; charset
=UTF
-8',
104 'Referer
': login_url,
105 'X
-Requested
-With
': 'XMLHttpRequest
',
108 if response.get('success
') == '1':
109 self._logged_in = True
112 message = response.get('message
')
113 if message is not None:
114 raise ExtractorError(
115 'Unable to login
: %s' % message, expected=True)
117 raise ExtractorError('Unable to log
in')
120 class PornHubIE(PornHubBaseIE):
121 IE_DESC = 'PornHub
and Thumbzilla
'
122 _VALID_URL = r'''(?x)
125 (?:[^/]+\.)?(?P<host>pornhub(?:premium)?\.(?:com|net|org))/(?:(?:view_video\.php|video/show)\?viewkey=|embed/)|
126 (?:www\.)?thumbzilla\.com/video/
131 'url
': 'http
://www
.pornhub
.com
/view_video
.php?viewkey
=648719015',
132 'md5
': 'a6391306d050e4547f62b3f485dd9ba9
',
136 'title
': 'Seductive Indian beauty strips down
and fingers her pink pussy
',
138 'upload_date
': '20130628',
139 'timestamp
': 1372447216,
143 'dislike_count
': int,
144 'comment_count
': int,
151 'url
': 'http
://www
.pornhub
.com
/view_video
.php?viewkey
=1331683002',
156 'upload_date
': '20150213',
157 'timestamp
': 1423804862,
161 'dislike_count
': int,
162 'comment_count
': int,
168 'skip_download
': True,
170 'skip
': 'Video has been flagged
for verification
in accordance
with our trust
and safety policy
',
173 'url
': 'https
://www
.pornhub
.com
/view_video
.php?viewkey
=ph5af5fef7c2aa7
',
175 'id': 'ph5af5fef7c2aa7
',
177 'title
': 'BFFS
- Cute Teen Girls Share Cock On the Floor
',
182 'dislike_count
': int,
183 'comment_count
': int,
194 'skip_download
': True,
196 'skip
': 'This video has been disabled
',
198 'url
': 'http
://www
.pornhub
.com
/view_video
.php?viewkey
=ph557bbb6676d2d
',
199 'only_matching
': True,
201 # removed at the request of cam4.com
202 'url
': 'http
://fr
.pornhub
.com
/view_video
.php?viewkey
=ph55ca2f9760862
',
203 'only_matching
': True,
205 # removed at the request of the copyright owner
206 'url
': 'http
://www
.pornhub
.com
/view_video
.php?viewkey
=788152859',
207 'only_matching
': True,
209 # removed by uploader
210 'url
': 'http
://www
.pornhub
.com
/view_video
.php?viewkey
=ph572716d15a111
',
211 'only_matching
': True,
214 'url
': 'http
://www
.pornhub
.com
/view_video
.php?viewkey
=ph56fd731fce6b7
',
215 'only_matching
': True,
217 'url
': 'https
://www
.thumbzilla
.com
/video
/ph56c6114abd99a
/horny
-girlfriend
-sex
',
218 'only_matching
': True,
220 'url
': 'http
://www
.pornhub
.com
/video
/show?viewkey
=648719015',
221 'only_matching
': True,
223 'url
': 'https
://www
.pornhub
.net
/view_video
.php?viewkey
=203640933',
224 'only_matching
': True,
226 'url
': 'https
://www
.pornhub
.org
/view_video
.php?viewkey
=203640933',
227 'only_matching
': True,
229 'url
': 'https
://www
.pornhubpremium
.com
/view_video
.php?viewkey
=ph5e4acdae54a82
',
230 'only_matching
': True,
232 # Some videos are available with the same id on both premium
233 # and non-premium sites (e.g. this and the following test)
234 'url
': 'https
://www
.pornhub
.com
/view_video
.php?viewkey
=ph5f75b0f4b18e3
',
235 'only_matching
': True,
237 'url
': 'https
://www
.pornhubpremium
.com
/view_video
.php?viewkey
=ph5f75b0f4b18e3
',
238 'only_matching
': True,
242 def _extract_urls(webpage):
244 r'<iframe
[^
>]+?src
=["\'](?P<url>(?:https?:)?//(?:www\.)?pornhub(?:premium)?\.(?:com|net|org)/embed/[\da-z]+)',
247 def _extract_count(self, pattern, webpage, name):
248 return str_to_int(self._search_regex(
249 pattern, webpage, '%s count' % name, fatal=False))
251 def _real_extract(self, url):
252 mobj = re.match(self._VALID_URL, url)
253 host = mobj.group('host') or 'pornhub.com'
254 video_id = mobj.group('id')
258 self._set_cookie(host, 'age_verified', '1')
260 def dl_webpage(platform):
261 self._set_cookie(host, 'platform', platform)
262 return self._download_webpage(
263 'https://www.%s/view_video.php?viewkey=%s' % (host, video_id),
264 video_id, 'Downloading %s webpage' % platform)
266 webpage = dl_webpage('pc')
268 error_msg = self._html_search_regex(
269 (r'(?s)<div[^>]+class=(["\'])(?
:(?
!\
1).)*\b(?
:removed|userMessageSection
)\b(?
:(?
!\
1).)*\
1[^
>]*>(?P
<error
>.+?
)</div
>',
270 r'(?s
)<section
[^
>]+class=["\']noVideo["\'][^
>]*>(?P
<error
>.+?
)</section
>'),
271 webpage, 'error message
', default=None, group='error
')
273 error_msg = re.sub(r'\s
+', ' ', error_msg)
274 raise ExtractorError(
275 'PornHub said
: %s' % error_msg,
276 expected=True, video_id=video_id)
278 # video_title from flashvars contains whitespace instead of non-ASCII (see
279 # http://www.pornhub.com/view_video.php?viewkey=1331683002), not relying
281 title = self._html_search_meta(
282 'twitter
:title
', webpage, default=None) or self._html_search_regex(
283 (r'(?s
)<h1
[^
>]+class=["\']title["\'][^
>]*>(?P
<title
>.+?
)</h1
>',
284 r'<div
[^
>]+data
-video
-title
=(["\'])(?P<title>(?:(?!\1).)+)\1',
285 r'shareTitle["\']\s
*[=:]\s
*(["\'])(?P<title>(?:(?!\1).)+)\1'),
286 webpage, 'title', group='title')
289 video_urls_set = set()
292 flashvars = self._parse_json(
294 r'var\s+flashvars_\d+\s*=\s*({.+?});', webpage, 'flashvars', default='{}'),
297 subtitle_url = url_or_none(flashvars.get('closedCaptionsFile'))
299 subtitles.setdefault('en', []).append({
303 thumbnail = flashvars.get('image_url')
304 duration = int_or_none(flashvars.get('video_duration'))
305 media_definitions = flashvars.get('mediaDefinitions')
306 if isinstance(media_definitions, list):
307 for definition in media_definitions:
308 if not isinstance(definition, dict):
310 video_url = definition.get('videoUrl')
311 if not video_url or not isinstance(video_url, compat_str):
313 if video_url in video_urls_set:
315 video_urls_set.add(video_url)
317 (video_url, int_or_none(definition.get('quality'))))
319 thumbnail, duration = [None] * 2
321 def extract_js_vars(webpage, pattern, default=NO_DEFAULT):
322 assignments = self._search_regex(
323 pattern, webpage, 'encoded url', default=default)
327 assignments = assignments.split(';')
331 def parse_js_value(inp):
332 inp = re.sub(r'/\*(?:(?!\*/).)*?\*/', '', inp)
334 inps = inp.split('+')
335 return functools.reduce(
336 operator.concat, map(parse_js_value, inps))
340 return remove_quotes(inp)
342 for assn in assignments:
346 assn = re.sub(r'var\s+', '', assn)
347 vname, value = assn.split('=', 1)
348 js_vars[vname] = parse_js_value(value)
351 def add_video_url(video_url):
352 v_url = url_or_none(video_url)
355 if v_url in video_urls_set:
357 video_urls.append((v_url, None))
358 video_urls_set.add(v_url)
360 def parse_quality_items(quality_items):
361 q_items = self._parse_json(quality_items, video_id, fatal=False)
362 if not isinstance(q_items, list):
365 if isinstance(item, dict):
366 add_video_url(item.get('url'))
369 FORMAT_PREFIXES = ('media', 'quality', 'qualityItems')
370 js_vars = extract_js_vars(
371 webpage, r'(var\s+(?:%s)_.+)' % '|'.join(FORMAT_PREFIXES),
374 for key, format_url in js_vars.items():
375 if key.startswith(FORMAT_PREFIXES[-1]):
376 parse_quality_items(format_url)
377 elif any(key.startswith(p) for p in FORMAT_PREFIXES[:2]):
378 add_video_url(format_url)
379 if not video_urls and re.search(
380 r'<[^>]+\bid=["\']lockedPlayer
', webpage):
381 raise ExtractorError(
382 'Video
%s is locked
' % video_id, expected=True)
385 js_vars = extract_js_vars(
386 dl_webpage('tv
'), r'(var
.+?mediastring
.+?
)</script
>')
387 add_video_url(js_vars['mediastring
'])
389 for mobj in re.finditer(
390 r'<a
[^
>]+\bclass
=["\']downloadBtn\b[^>]+\bhref=(["\'])(?P
<url
>(?
:(?
!\
1).)+)\
1',
392 video_url = mobj.group('url
')
393 if video_url not in video_urls_set:
394 video_urls.append((video_url, None))
395 video_urls_set.add(video_url)
400 def add_format(format_url, height=None):
401 ext = determine_ext(format_url)
403 formats.extend(self._extract_mpd_formats(
404 format_url, video_id, mpd_id='dash
', fatal=False))
407 formats.extend(self._extract_m3u8_formats(
408 format_url, video_id, 'mp4
', entry_protocol='m3u8_native
',
409 m3u8_id='hls
', fatal=False))
412 mobj = re.search(r'(?P
<height
>\d
+)[pP
]?
_(?P
<tbr
>\d
+)[kK
]', format_url)
415 height = int(mobj.group('height
'))
416 tbr = int(mobj.group('tbr
'))
419 'format_id
': '%dp
' % height if height else None,
424 for video_url, height in video_urls:
426 upload_date = self._search_regex(
427 r'/(\d{6}
/\d{2}
)/', video_url, 'upload data
', default=None)
429 upload_date = upload_date.replace('/', '')
430 if '/video
/get_media
' in video_url:
431 medias = self._download_json(video_url, video_id, fatal=False)
432 if isinstance(medias, list):
434 if not isinstance(media, dict):
436 video_url = url_or_none(media.get('videoUrl
'))
439 height = int_or_none(media.get('quality
'))
440 add_format(video_url, height)
442 add_format(video_url)
443 self._sort_formats(formats)
445 video_uploader = self._html_search_regex(
446 r'(?s
)From
: 
;.+?
<(?
:a
\b[^
>]+\bhref
=["\']/(?:(?:user|channel)s|model|pornstar)/|span\b[^>]+\bclass=["\']username
)[^
>]+>(.+?
)<',
447 webpage, 'uploader
', default=None)
449 def extract_vote_count(kind, name):
450 return self._extract_count(
451 (r'<span
[^
>]+\bclass
="votes%s"[^
>]*>([\d
,\
.]+)</span
>' % kind,
452 r'<span
[^
>]+\bclass
=["\']votes%s["\'][^
>]*\bdata
-rating
=["\'](\d+)' % kind),
455 view_count = self._extract_count(
456 r'<span class="count
">([\d,\.]+)</span> [Vv]iews', webpage, 'view')
457 like_count = extract_vote_count('Up', 'like')
458 dislike_count = extract_vote_count('Down', 'dislike')
459 comment_count = self._extract_count(
460 r'All Comments\s*<span>\(([\d,.]+)\)', webpage, 'comment')
462 def extract_list(meta_key):
463 div = self._search_regex(
464 r'(?s)<div[^>]+\bclass=["\'].*?
\b%sWrapper
[^
>]*>(.+?
)</div
>'
465 % meta_key, webpage, meta_key, default=None)
467 return re.findall(r'<a
[^
>]+\bhref
=[^
>]+>([^
<]+)', div)
469 info = self._search_json_ld(webpage, video_id, default={})
470 # description provided in JSON-LD is irrelevant
471 info['description
'] = None
475 'uploader
': video_uploader,
476 'upload_date
': upload_date,
478 'thumbnail
': thumbnail,
479 'duration
': duration,
480 'view_count
': view_count,
481 'like_count
': like_count,
482 'dislike_count
': dislike_count,
483 'comment_count
': comment_count,
486 'tags
': extract_list('tags
'),
487 'categories
': extract_list('categories
'),
488 'subtitles
': subtitles,
492 class PornHubPlaylistBaseIE(PornHubBaseIE):
493 def _extract_page(self, url):
494 return int_or_none(self._search_regex(
495 r'\bpage
=(\d
+)', url, 'page
', default=None))
497 def _extract_entries(self, webpage, host):
498 # Only process container div with main playlist content skipping
499 # drop-down menu that uses similar pattern for videos (see
500 # https://github.com/ytdl-org/youtube-dl/issues/11594).
501 container = self._search_regex(
502 r'(?s
)(<div
[^
>]+class=["\']container.+)', webpage,
503 'container', default=webpage)
507 'http://www.%s/%s' % (host, video_url),
508 PornHubIE.ie_key(), video_title=title)
509 for video_url, title in orderedSet(re.findall(
510 r'href="/?
(view_video\
.php
\?.*\bviewkey
=[\da
-z
]+[^
"]*)"[^
>]*\s
+title
="([^"]+)"',
515 class PornHubUserIE(PornHubPlaylistBaseIE):
516 _VALID_URL = r'(?P<url>https?://(?:[^/]+\.)?(?P<host>pornhub(?:premium)?\.(?:com|net|org))/(?:(?:user|channel)s|model|pornstar)/(?P<id>[^/?#&]+))(?:[?#&]|/(?!videos)|$)'
518 'url': 'https://www.pornhub.com/model/zoe_ph',
519 'playlist_mincount': 118,
521 'url': 'https://www.pornhub.com/pornstar/liz-vicious',
525 'playlist_mincount': 118,
527 'url': 'https://www.pornhub.com/users/russianveet69',
528 'only_matching': True,
530 'url': 'https://www.pornhub.com/channels/povd',
531 'only_matching': True,
533 'url': 'https://www.pornhub.com/model/zoe_ph?abc=1',
534 'only_matching': True,
536 # Unavailable via /videos page, but available with direct pagination
537 # on pornstar page (see [1]), requires premium
538 # 1. https://github.com/ytdl-org/youtube-dl/issues/27853
539 'url': 'https://www.pornhubpremium.com/pornstar/sienna-west',
540 'only_matching': True,
542 # Same as before, multi page
543 'url': 'https://www.pornhubpremium.com/pornstar/lily-labeau',
544 'only_matching': True,
547 def _real_extract(self, url):
548 mobj = re.match(self._VALID_URL, url)
549 user_id = mobj.group('id')
550 videos_url = '%s/videos' % mobj.group('url')
551 page = self._extract_page(url)
553 videos_url = update_url_query(videos_url, {'page': page})
554 return self.url_result(
555 videos_url, ie=PornHubPagedVideoListIE.ie_key(), video_id=user_id)
558 class PornHubPagedPlaylistBaseIE(PornHubPlaylistBaseIE):
560 def _has_more(webpage):
563 <li[^>]+\bclass=["\']page_next|
564 <link
[^
>]+\brel
=["\']next|
565 <button[^>]+\bid=["\']moreDataBtn
566 ''', webpage) is not None
568 def _entries(self, url, host, item_id):
569 page = self._extract_page(url)
573 def download_page(base_url, num, fallback=False):
574 note = 'Downloading page %d%s' % (num, ' (switch to fallback)' if fallback else '')
575 return self._download_webpage(
576 base_url, item_id, note, query={'page': num})
579 return isinstance(e.cause, compat_HTTPError) and e.cause.code == 404
582 has_page = page is not None
583 first_page = page if has_page else 1
584 for page_num in (first_page, ) if has_page else itertools.count(first_page):
587 webpage = download_page(base_url, page_num)
588 except ExtractorError as e:
589 # Some sources may not be available via /videos page,
590 # trying to fallback to main page pagination (see [1])
591 # 1. https://github.com/ytdl-org/youtube-dl/issues/27853
592 if is_404(e) and page_num == first_page and VIDEOS in base_url:
593 base_url = base_url.replace(VIDEOS, '')
594 webpage = download_page(base_url, page_num, fallback=True)
597 except ExtractorError as e:
598 if is_404(e) and page_num != first_page:
601 page_entries = self._extract_entries(webpage, host)
604 for e in page_entries:
606 if not self._has_more(webpage):
609 def _real_extract(self, url):
610 mobj = re.match(self._VALID_URL, url)
611 host = mobj.group('host')
612 item_id = mobj.group('id')
616 return self.playlist_result(self._entries(url, host, item_id), item_id)
619 class PornHubPagedVideoListIE(PornHubPagedPlaylistBaseIE):
620 _VALID_URL = r'https?://(?:[^/]+\.)?(?P<host>pornhub(?:premium)?\.(?:com|net|org))/(?P<id>(?:[^/]+/)*[^/?#&]+)'
622 'url': 'https://www.pornhub.com/model/zoe_ph/videos',
623 'only_matching': True,
625 'url': 'http://www.pornhub.com/users/rushandlia/videos',
626 'only_matching': True,
628 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos',
630 'id': 'pornstar/jenny-blighe/videos',
632 'playlist_mincount': 149,
634 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos?page=3',
636 'id': 'pornstar/jenny-blighe/videos',
638 'playlist_mincount': 40,
640 # default sorting as Top Rated Videos
641 'url': 'https://www.pornhub.com/channels/povd/videos',
643 'id': 'channels/povd/videos',
645 'playlist_mincount': 293,
648 'url': 'https://www.pornhub.com/channels/povd/videos?o=ra',
649 'only_matching': True,
652 'url': 'https://www.pornhub.com/channels/povd/videos?o=da',
653 'only_matching': True,
656 'url': 'https://www.pornhub.com/channels/povd/videos?o=vi',
657 'only_matching': True,
659 'url': 'http://www.pornhub.com/users/zoe_ph/videos/public',
660 'only_matching': True,
663 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=mv',
664 'only_matching': True,
667 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=tr',
668 'only_matching': True,
671 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=lg',
672 'only_matching': True,
675 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos?o=cm',
676 'only_matching': True,
678 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos/paid',
679 'only_matching': True,
681 'url': 'https://www.pornhub.com/pornstar/liz-vicious/videos/fanonly',
682 'only_matching': True,
684 'url': 'https://www.pornhub.com/video',
685 'only_matching': True,
687 'url': 'https://www.pornhub.com/video?page=3',
688 'only_matching': True,
690 'url': 'https://www.pornhub.com/video/search?search=123',
691 'only_matching': True,
693 'url': 'https://www.pornhub.com/categories/teen',
694 'only_matching': True,
696 'url': 'https://www.pornhub.com/categories/teen?page=3',
697 'only_matching': True,
699 'url': 'https://www.pornhub.com/hd',
700 'only_matching': True,
702 'url': 'https://www.pornhub.com/hd?page=3',
703 'only_matching': True,
705 'url': 'https://www.pornhub.com/described-video',
706 'only_matching': True,
708 'url': 'https://www.pornhub.com/described-video?page=2',
709 'only_matching': True,
711 'url': 'https://www.pornhub.com/video/incategories/60fps-1/hd-porn',
712 'only_matching': True,
714 'url': 'https://www.pornhub.com/playlist/44121572',
716 'id': 'playlist/44121572',
718 'playlist_mincount': 132,
720 'url': 'https://www.pornhub.com/playlist/4667351',
721 'only_matching': True,
723 'url': 'https://de.pornhub.com/playlist/4667351',
724 'only_matching': True,
728 def suitable(cls, url):
730 if PornHubIE.suitable(url) or PornHubUserIE.suitable(url) or PornHubUserVideosUploadIE.suitable(url)
731 else super(PornHubPagedVideoListIE, cls).suitable(url))
734 class PornHubUserVideosUploadIE(PornHubPagedPlaylistBaseIE):
735 _VALID_URL = r'(?P<url>https?://(?:[^/]+\.)?(?P<host>pornhub(?:premium)?\.(?:com|net|org))/(?:(?:user|channel)s|model|pornstar)/(?P<id>[^/]+)/videos/upload)'
737 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos/upload',
739 'id': 'jenny-blighe',
741 'playlist_mincount': 129,
743 'url': 'https://www.pornhub.com/model/zoe_ph/videos/upload',
744 'only_matching': True,