]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/pornhub.py
[youtube] Fix authentication (Closes #10140)
[yt-dlp.git] / youtube_dl / extractor / pornhub.py
CommitLineData
6c376029 1# coding: utf-8
9933b574
PH
2from __future__ import unicode_literals
3
34541395 4import itertools
125cfd78 5import os
6import re
7
8from .common import InfoExtractor
1cc79574 9from ..compat import (
34541395 10 compat_HTTPError,
605cbef6
S
11 compat_urllib_parse_unquote,
12 compat_urllib_parse_unquote_plus,
125cfd78 13 compat_urllib_parse_urlparse,
1cc79574
PH
14)
15from ..utils import (
50789175 16 ExtractorError,
ed8648a3 17 int_or_none,
8f9a477e 18 orderedSet,
5c2266df 19 sanitized_Request,
0320ddc1 20 str_to_int,
125cfd78 21)
22from ..aes import (
23 aes_decrypt_text
24)
25
9933b574 26
125cfd78 27class PornHubIE(InfoExtractor):
bc4b2d75
S
28 IE_DESC = 'PornHub and Thumbzilla'
29 _VALID_URL = r'''(?x)
30 https?://
31 (?:
32 (?:[a-z]+\.)?pornhub\.com/(?:view_video\.php\?viewkey=|embed/)|
33 (?:www\.)?thumbzilla\.com/video/
34 )
35 (?P<id>[0-9a-z]+)
36 '''
360075e2 37 _TESTS = [{
9933b574 38 'url': 'http://www.pornhub.com/view_video.php?viewkey=648719015',
ed8648a3 39 'md5': '1e19b41231a02eba417839222ac9d58e',
9933b574 40 'info_dict': {
249efaf4
PH
41 'id': '648719015',
42 'ext': 'mp4',
611c1dd9 43 'title': 'Seductive Indian beauty strips down and fingers her pink pussy',
ed8648a3
S
44 'uploader': 'Babes',
45 'duration': 361,
46 'view_count': int,
47 'like_count': int,
48 'dislike_count': int,
49 'comment_count': int,
50 'age_limit': 18,
6c376029
S
51 },
52 }, {
53 # non-ASCII title
54 'url': 'http://www.pornhub.com/view_video.php?viewkey=1331683002',
55 'info_dict': {
56 'id': '1331683002',
57 'ext': 'mp4',
58 'title': '重庆婷婷女王足交',
59 'uploader': 'cj397186295',
60 'duration': 1753,
61 'view_count': int,
62 'like_count': int,
63 'dislike_count': int,
64 'comment_count': int,
65 'age_limit': 18,
66 },
67 'params': {
68 'skip_download': True,
69 },
360075e2
S
70 }, {
71 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph557bbb6676d2d',
72 'only_matching': True,
272e4db5 73 }, {
eaaaaec0 74 # removed at the request of cam4.com
272e4db5
S
75 'url': 'http://fr.pornhub.com/view_video.php?viewkey=ph55ca2f9760862',
76 'only_matching': True,
eaaaaec0
S
77 }, {
78 # removed at the request of the copyright owner
79 'url': 'http://www.pornhub.com/view_video.php?viewkey=788152859',
80 'only_matching': True,
81 }, {
82 # removed by uploader
83 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph572716d15a111',
84 'only_matching': True,
195f0845
S
85 }, {
86 # private video
87 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph56fd731fce6b7',
88 'only_matching': True,
bc4b2d75
S
89 }, {
90 'url': 'https://www.thumbzilla.com/video/ph56c6114abd99a/horny-girlfriend-sex',
91 'only_matching': True,
360075e2 92 }]
125cfd78 93
65d161c4
S
94 @classmethod
95 def _extract_url(cls, webpage):
96 mobj = re.search(
97 r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?pornhub\.com/embed/\d+)\1', webpage)
98 if mobj:
99 return mobj.group('url')
100
0320ddc1 101 def _extract_count(self, pattern, webpage, name):
7700207e
S
102 return str_to_int(self._search_regex(
103 pattern, webpage, '%s count' % name, fatal=False))
0320ddc1 104
125cfd78 105 def _real_extract(self, url):
249efaf4 106 video_id = self._match_id(url)
125cfd78 107
5c2266df 108 req = sanitized_Request(
9fcbd5db 109 'http://www.pornhub.com/view_video.php?viewkey=%s' % video_id)
125cfd78 110 req.add_header('Cookie', 'age_verified=1')
111 webpage = self._download_webpage(req, video_id)
112
50789175 113 error_msg = self._html_search_regex(
195f0845 114 r'(?s)<div[^>]+class=(["\']).*?\b(?:removed|userMessageSection)\b.*?\1[^>]*>(?P<error>.+?)</div>',
3cb3b600 115 webpage, 'error message', default=None, group='error')
50789175
PH
116 if error_msg:
117 error_msg = re.sub(r'\s+', ' ', error_msg)
118 raise ExtractorError(
119 'PornHub said: %s' % error_msg,
120 expected=True, video_id=video_id)
121
6c376029
S
122 # video_title from flashvars contains whitespace instead of non-ASCII (see
123 # http://www.pornhub.com/view_video.php?viewkey=1331683002), not relying
124 # on that anymore.
125 title = self._html_search_meta(
126 'twitter:title', webpage, default=None) or self._search_regex(
127 (r'<h1[^>]+class=["\']title["\'][^>]*>(?P<title>[^<]+)',
128 r'<div[^>]+data-video-title=(["\'])(?P<title>.+?)\1',
129 r'shareTitle\s*=\s*(["\'])(?P<title>.+?)\1'),
130 webpage, 'title', group='title')
131
ed8648a3
S
132 flashvars = self._parse_json(
133 self._search_regex(
03442072 134 r'var\s+flashvars_\d+\s*=\s*({.+?});', webpage, 'flashvars', default='{}'),
ed8648a3
S
135 video_id)
136 if flashvars:
ed8648a3
S
137 thumbnail = flashvars.get('image_url')
138 duration = int_or_none(flashvars.get('video_duration'))
139 else:
6c376029 140 title, thumbnail, duration = [None] * 3
ed8648a3 141
0320ddc1 142 video_uploader = self._html_search_regex(
8fc642eb 143 r'(?s)From:&nbsp;.+?<(?:a href="/users/|a href="/channels/|span class="username)[^>]+>(.+?)<',
0320ddc1 144 webpage, 'uploader', fatal=False)
125cfd78 145
7700207e
S
146 view_count = self._extract_count(
147 r'<span class="count">([\d,\.]+)</span> views', webpage, 'view')
148 like_count = self._extract_count(
149 r'<span class="votesUp">([\d,\.]+)</span>', webpage, 'like')
150 dislike_count = self._extract_count(
151 r'<span class="votesDown">([\d,\.]+)</span>', webpage, 'dislike')
0320ddc1 152 comment_count = self._extract_count(
7700207e 153 r'All Comments\s*<span>\(([\d,.]+)\)', webpage, 'comment')
0320ddc1 154
524229a2 155 video_urls = list(map(compat_urllib_parse_unquote, re.findall(r"player_quality_[0-9]{3}p\s*=\s*'([^']+)'", webpage)))
125cfd78 156 if webpage.find('"encrypted":true') != -1:
605cbef6 157 password = compat_urllib_parse_unquote_plus(
7a372b64 158 self._search_regex(r'"video_title":"([^"]+)', webpage, 'password'))
125cfd78 159 video_urls = list(map(lambda s: aes_decrypt_text(s, password, 32).decode('utf-8'), video_urls))
160
161 formats = []
162 for video_url in video_urls:
a56f9de1
JMF
163 path = compat_urllib_parse_urlparse(video_url).path
164 extension = os.path.splitext(path)[1][1:]
125cfd78 165 format = path.split('/')[5].split('_')[:2]
611c1dd9 166 format = '-'.join(format)
9933b574 167
8f5639af 168 m = re.match(r'^(?P<height>[0-9]+)[pP]-(?P<tbr>[0-9]+)[kK]$', format)
9933b574
PH
169 if m is None:
170 height = None
171 tbr = None
172 else:
173 height = int(m.group('height'))
174 tbr = int(m.group('tbr'))
175
125cfd78 176 formats.append({
177 'url': video_url,
178 'ext': extension,
179 'format': format,
180 'format_id': format,
9933b574
PH
181 'tbr': tbr,
182 'height': height,
125cfd78 183 })
9933b574 184 self._sort_formats(formats)
125cfd78 185
186 return {
187 'id': video_id,
188 'uploader': video_uploader,
6c376029 189 'title': title,
125cfd78 190 'thumbnail': thumbnail,
ed8648a3 191 'duration': duration,
0320ddc1
S
192 'view_count': view_count,
193 'like_count': like_count,
194 'dislike_count': dislike_count,
195 'comment_count': comment_count,
125cfd78 196 'formats': formats,
750e9833 197 'age_limit': 18,
125cfd78 198 }
e66e1a00
S
199
200
40e146aa
S
201class PornHubPlaylistBaseIE(InfoExtractor):
202 def _extract_entries(self, webpage):
203 return [
3a23bae9
S
204 self.url_result(
205 'http://www.pornhub.com/%s' % video_url,
206 PornHubIE.ie_key(), video_title=title)
207 for video_url, title in orderedSet(re.findall(
208 r'href="/?(view_video\.php\?.*\bviewkey=[\da-z]+[^"]*)"[^>]*\s+title="([^"]+)"',
209 webpage))
40e146aa 210 ]
e66e1a00
S
211
212 def _real_extract(self, url):
213 playlist_id = self._match_id(url)
214
215 webpage = self._download_webpage(url, playlist_id)
216
40e146aa 217 entries = self._extract_entries(webpage)
e66e1a00
S
218
219 playlist = self._parse_json(
220 self._search_regex(
221 r'playlistObject\s*=\s*({.+?});', webpage, 'playlist'),
222 playlist_id)
223
224 return self.playlist_result(
225 entries, playlist_id, playlist.get('title'), playlist.get('description'))
40e146aa
S
226
227
228class PornHubPlaylistIE(PornHubPlaylistBaseIE):
229 _VALID_URL = r'https?://(?:www\.)?pornhub\.com/playlist/(?P<id>\d+)'
230 _TESTS = [{
231 'url': 'http://www.pornhub.com/playlist/6201671',
232 'info_dict': {
233 'id': '6201671',
234 'title': 'P0p4',
235 },
236 'playlist_mincount': 35,
237 }]
238
239
240class PornHubUserVideosIE(PornHubPlaylistBaseIE):
241 _VALID_URL = r'https?://(?:www\.)?pornhub\.com/users/(?P<id>[^/]+)/videos'
242 _TESTS = [{
34541395 243 'url': 'http://www.pornhub.com/users/zoe_ph/videos/public',
40e146aa 244 'info_dict': {
34541395 245 'id': 'zoe_ph',
40e146aa 246 },
34541395
S
247 'playlist_mincount': 171,
248 }, {
249 'url': 'http://www.pornhub.com/users/rushandlia/videos',
250 'only_matching': True,
40e146aa
S
251 }]
252
253 def _real_extract(self, url):
254 user_id = self._match_id(url)
255
34541395
S
256 entries = []
257 for page_num in itertools.count(1):
258 try:
259 webpage = self._download_webpage(
260 url, user_id, 'Downloading page %d' % page_num,
261 query={'page': page_num})
262 except ExtractorError as e:
263 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
264 break
265 page_entries = self._extract_entries(webpage)
266 if not page_entries:
267 break
268 entries.extend(page_entries)
269
270 return self.playlist_result(entries, user_id)