]>
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 |
21fbf0f9 | 6 | import operator |
125cfd78 | 7 | import re |
8 | ||
9 | from .common import InfoExtractor | |
1cc79574 | 10 | from ..compat import ( |
34541395 | 11 | compat_HTTPError, |
79367a98 | 12 | compat_str, |
278d061a | 13 | compat_urllib_request, |
1cc79574 | 14 | ) |
278d061a | 15 | from .openload import PhantomJSwrapper |
1cc79574 | 16 | from ..utils import ( |
50789175 | 17 | ExtractorError, |
ed8648a3 | 18 | int_or_none, |
8f9a477e | 19 | orderedSet, |
e1e35d1a | 20 | remove_quotes, |
0320ddc1 | 21 | str_to_int, |
4938c8d5 | 22 | url_or_none, |
125cfd78 | 23 | ) |
125cfd78 | 24 | |
9933b574 | 25 | |
71a1f617 S |
26 | class PornHubBaseIE(InfoExtractor): |
27 | def _download_webpage_handle(self, *args, **kwargs): | |
28 | def dl(*args, **kwargs): | |
29 | return super(PornHubBaseIE, self)._download_webpage_handle(*args, **kwargs) | |
30 | ||
31 | webpage, urlh = dl(*args, **kwargs) | |
32 | ||
33 | if any(re.search(p, webpage) for p in ( | |
34 | r'<body\b[^>]+\bonload=["\']go\(\)', | |
35 | r'document\.cookie\s*=\s*["\']RNKEY=', | |
36 | r'document\.location\.reload\(true\)')): | |
37 | url_or_request = args[0] | |
38 | url = (url_or_request.get_full_url() | |
39 | if isinstance(url_or_request, compat_urllib_request.Request) | |
40 | else url_or_request) | |
41 | phantom = PhantomJSwrapper(self, required_version='2.0') | |
42 | phantom.get(url, html=webpage) | |
43 | webpage, urlh = dl(*args, **kwargs) | |
44 | ||
45 | return webpage, urlh | |
46 | ||
47 | ||
48 | class PornHubIE(PornHubBaseIE): | |
bc4b2d75 S |
49 | IE_DESC = 'PornHub and Thumbzilla' |
50 | _VALID_URL = r'''(?x) | |
51 | https?:// | |
52 | (?: | |
3430ff9b | 53 | (?:[^/]+\.)?(?P<host>pornhub\.(?:com|net))/(?:(?:view_video\.php|video/show)\?viewkey=|embed/)| |
bc4b2d75 S |
54 | (?:www\.)?thumbzilla\.com/video/ |
55 | ) | |
b52c9ef1 | 56 | (?P<id>[\da-z]+) |
bc4b2d75 | 57 | ''' |
360075e2 | 58 | _TESTS = [{ |
9933b574 | 59 | 'url': 'http://www.pornhub.com/view_video.php?viewkey=648719015', |
ed8648a3 | 60 | 'md5': '1e19b41231a02eba417839222ac9d58e', |
9933b574 | 61 | 'info_dict': { |
249efaf4 PH |
62 | 'id': '648719015', |
63 | 'ext': 'mp4', | |
611c1dd9 | 64 | 'title': 'Seductive Indian beauty strips down and fingers her pink pussy', |
ed8648a3 | 65 | 'uploader': 'Babes', |
3661ebf2 | 66 | 'upload_date': '20130628', |
ed8648a3 S |
67 | 'duration': 361, |
68 | 'view_count': int, | |
69 | 'like_count': int, | |
70 | 'dislike_count': int, | |
71 | 'comment_count': int, | |
72 | 'age_limit': 18, | |
6bb05b32 YCH |
73 | 'tags': list, |
74 | 'categories': list, | |
6c376029 S |
75 | }, |
76 | }, { | |
77 | # non-ASCII title | |
78 | 'url': 'http://www.pornhub.com/view_video.php?viewkey=1331683002', | |
79 | 'info_dict': { | |
80 | 'id': '1331683002', | |
81 | 'ext': 'mp4', | |
82 | 'title': '重庆婷婷女王足交', | |
79367a98 | 83 | 'uploader': 'Unknown', |
3661ebf2 | 84 | 'upload_date': '20150213', |
6c376029 S |
85 | 'duration': 1753, |
86 | 'view_count': int, | |
87 | 'like_count': int, | |
88 | 'dislike_count': int, | |
89 | 'comment_count': int, | |
90 | 'age_limit': 18, | |
6bb05b32 YCH |
91 | 'tags': list, |
92 | 'categories': list, | |
6c376029 S |
93 | }, |
94 | 'params': { | |
95 | 'skip_download': True, | |
96 | }, | |
4938c8d5 GF |
97 | }, { |
98 | # subtitles | |
99 | 'url': 'https://www.pornhub.com/view_video.php?viewkey=ph5af5fef7c2aa7', | |
100 | 'info_dict': { | |
101 | 'id': 'ph5af5fef7c2aa7', | |
102 | 'ext': 'mp4', | |
103 | 'title': 'BFFS - Cute Teen Girls Share Cock On the Floor', | |
104 | 'uploader': 'BFFs', | |
105 | 'duration': 622, | |
106 | 'view_count': int, | |
107 | 'like_count': int, | |
108 | 'dislike_count': int, | |
109 | 'comment_count': int, | |
110 | 'age_limit': 18, | |
111 | 'tags': list, | |
112 | 'categories': list, | |
113 | 'subtitles': { | |
114 | 'en': [{ | |
115 | "ext": 'srt' | |
116 | }] | |
117 | }, | |
118 | }, | |
119 | 'params': { | |
120 | 'skip_download': True, | |
121 | }, | |
360075e2 S |
122 | }, { |
123 | 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph557bbb6676d2d', | |
124 | 'only_matching': True, | |
272e4db5 | 125 | }, { |
eaaaaec0 | 126 | # removed at the request of cam4.com |
272e4db5 S |
127 | 'url': 'http://fr.pornhub.com/view_video.php?viewkey=ph55ca2f9760862', |
128 | 'only_matching': True, | |
eaaaaec0 S |
129 | }, { |
130 | # removed at the request of the copyright owner | |
131 | 'url': 'http://www.pornhub.com/view_video.php?viewkey=788152859', | |
132 | 'only_matching': True, | |
133 | }, { | |
134 | # removed by uploader | |
135 | 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph572716d15a111', | |
136 | 'only_matching': True, | |
195f0845 S |
137 | }, { |
138 | # private video | |
139 | 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph56fd731fce6b7', | |
140 | 'only_matching': True, | |
bc4b2d75 S |
141 | }, { |
142 | 'url': 'https://www.thumbzilla.com/video/ph56c6114abd99a/horny-girlfriend-sex', | |
143 | 'only_matching': True, | |
a99cc4ca S |
144 | }, { |
145 | 'url': 'http://www.pornhub.com/video/show?viewkey=648719015', | |
146 | 'only_matching': True, | |
f97c0991 S |
147 | }, { |
148 | 'url': 'https://www.pornhub.net/view_video.php?viewkey=203640933', | |
149 | 'only_matching': True, | |
360075e2 | 150 | }] |
125cfd78 | 151 | |
b52c9ef1 S |
152 | @staticmethod |
153 | def _extract_urls(webpage): | |
154 | return re.findall( | |
3430ff9b | 155 | r'<iframe[^>]+?src=["\'](?P<url>(?:https?:)?//(?:www\.)?pornhub\.(?:com|net)/embed/[\da-z]+)', |
b52c9ef1 | 156 | webpage) |
65d161c4 | 157 | |
0320ddc1 | 158 | def _extract_count(self, pattern, webpage, name): |
7700207e S |
159 | return str_to_int(self._search_regex( |
160 | pattern, webpage, '%s count' % name, fatal=False)) | |
0320ddc1 | 161 | |
125cfd78 | 162 | def _real_extract(self, url): |
3430ff9b S |
163 | mobj = re.match(self._VALID_URL, url) |
164 | host = mobj.group('host') or 'pornhub.com' | |
165 | video_id = mobj.group('id') | |
7399ca1f | 166 | |
3430ff9b | 167 | self._set_cookie(host, 'age_verified', '1') |
125cfd78 | 168 | |
9a372f14 | 169 | def dl_webpage(platform): |
3430ff9b | 170 | self._set_cookie(host, 'platform', platform) |
9a372f14 | 171 | return self._download_webpage( |
3430ff9b | 172 | 'http://www.%s/view_video.php?viewkey=%s' % (host, video_id), |
79367a98 | 173 | video_id, 'Downloading %s webpage' % platform) |
9a372f14 S |
174 | |
175 | webpage = dl_webpage('pc') | |
125cfd78 | 176 | |
50789175 | 177 | error_msg = self._html_search_regex( |
add7d2a0 | 178 | r'(?s)<div[^>]+class=(["\'])(?:(?!\1).)*\b(?:removed|userMessageSection)\b(?:(?!\1).)*\1[^>]*>(?P<error>.+?)</div>', |
3cb3b600 | 179 | webpage, 'error message', default=None, group='error') |
50789175 PH |
180 | if error_msg: |
181 | error_msg = re.sub(r'\s+', ' ', error_msg) | |
182 | raise ExtractorError( | |
183 | 'PornHub said: %s' % error_msg, | |
184 | expected=True, video_id=video_id) | |
185 | ||
6c376029 S |
186 | # video_title from flashvars contains whitespace instead of non-ASCII (see |
187 | # http://www.pornhub.com/view_video.php?viewkey=1331683002), not relying | |
188 | # on that anymore. | |
79367a98 | 189 | title = self._html_search_meta( |
6c376029 S |
190 | 'twitter:title', webpage, default=None) or self._search_regex( |
191 | (r'<h1[^>]+class=["\']title["\'][^>]*>(?P<title>[^<]+)', | |
192 | r'<div[^>]+data-video-title=(["\'])(?P<title>.+?)\1', | |
193 | r'shareTitle\s*=\s*(["\'])(?P<title>.+?)\1'), | |
194 | webpage, 'title', group='title') | |
195 | ||
79367a98 S |
196 | video_urls = [] |
197 | video_urls_set = set() | |
4938c8d5 | 198 | subtitles = {} |
79367a98 | 199 | |
ed8648a3 S |
200 | flashvars = self._parse_json( |
201 | self._search_regex( | |
03442072 | 202 | r'var\s+flashvars_\d+\s*=\s*({.+?});', webpage, 'flashvars', default='{}'), |
ed8648a3 S |
203 | video_id) |
204 | if flashvars: | |
4938c8d5 GF |
205 | subtitle_url = url_or_none(flashvars.get('closedCaptionsFile')) |
206 | if subtitle_url: | |
207 | subtitles.setdefault('en', []).append({ | |
208 | 'url': subtitle_url, | |
209 | 'ext': 'srt', | |
210 | }) | |
ed8648a3 S |
211 | thumbnail = flashvars.get('image_url') |
212 | duration = int_or_none(flashvars.get('video_duration')) | |
79367a98 S |
213 | media_definitions = flashvars.get('mediaDefinitions') |
214 | if isinstance(media_definitions, list): | |
215 | for definition in media_definitions: | |
216 | if not isinstance(definition, dict): | |
217 | continue | |
218 | video_url = definition.get('videoUrl') | |
219 | if not video_url or not isinstance(video_url, compat_str): | |
220 | continue | |
221 | if video_url in video_urls_set: | |
222 | continue | |
223 | video_urls_set.add(video_url) | |
224 | video_urls.append( | |
225 | (video_url, int_or_none(definition.get('quality')))) | |
ed8648a3 | 226 | else: |
79367a98 S |
227 | thumbnail, duration = [None] * 2 |
228 | ||
229 | if not video_urls: | |
230 | tv_webpage = dl_webpage('tv') | |
231 | ||
232 | assignments = self._search_regex( | |
233 | r'(var.+?mediastring.+?)</script>', tv_webpage, | |
234 | 'encoded url').split(';') | |
235 | ||
236 | js_vars = {} | |
237 | ||
238 | def parse_js_value(inp): | |
239 | inp = re.sub(r'/\*(?:(?!\*/).)*?\*/', '', inp) | |
240 | if '+' in inp: | |
241 | inps = inp.split('+') | |
242 | return functools.reduce( | |
243 | operator.concat, map(parse_js_value, inps)) | |
244 | inp = inp.strip() | |
245 | if inp in js_vars: | |
246 | return js_vars[inp] | |
247 | return remove_quotes(inp) | |
248 | ||
249 | for assn in assignments: | |
250 | assn = assn.strip() | |
251 | if not assn: | |
252 | continue | |
253 | assn = re.sub(r'var\s+', '', assn) | |
254 | vname, value = assn.split('=', 1) | |
255 | js_vars[vname] = parse_js_value(value) | |
256 | ||
257 | video_url = js_vars['mediastring'] | |
258 | if video_url not in video_urls_set: | |
259 | video_urls.append((video_url, None)) | |
260 | video_urls_set.add(video_url) | |
261 | ||
262 | for mobj in re.finditer( | |
263 | r'<a[^>]+\bclass=["\']downloadBtn\b[^>]+\bhref=(["\'])(?P<url>(?:(?!\1).)+)\1', | |
264 | webpage): | |
265 | video_url = mobj.group('url') | |
266 | if video_url not in video_urls_set: | |
267 | video_urls.append((video_url, None)) | |
268 | video_urls_set.add(video_url) | |
269 | ||
3661ebf2 | 270 | upload_date = None |
79367a98 S |
271 | formats = [] |
272 | for video_url, height in video_urls: | |
3661ebf2 S |
273 | if not upload_date: |
274 | upload_date = self._search_regex( | |
275 | r'/(\d{6}/\d{2})/', video_url, 'upload data', default=None) | |
276 | if upload_date: | |
277 | upload_date = upload_date.replace('/', '') | |
79367a98 S |
278 | tbr = None |
279 | mobj = re.search(r'(?P<height>\d+)[pP]?_(?P<tbr>\d+)[kK]', video_url) | |
280 | if mobj: | |
281 | if not height: | |
282 | height = int(mobj.group('height')) | |
283 | tbr = int(mobj.group('tbr')) | |
284 | formats.append({ | |
285 | 'url': video_url, | |
286 | 'format_id': '%dp' % height if height else None, | |
287 | 'height': height, | |
288 | 'tbr': tbr, | |
289 | }) | |
290 | self._sort_formats(formats) | |
ed8648a3 | 291 | |
0320ddc1 | 292 | video_uploader = self._html_search_regex( |
2d4fe594 | 293 | r'(?s)From: .+?<(?:a\b[^>]+\bhref=["\']/(?:(?:user|channel)s|model|pornstar)/|span\b[^>]+\bclass=["\']username)[^>]+>(.+?)<', |
0320ddc1 | 294 | webpage, 'uploader', fatal=False) |
125cfd78 | 295 | |
7700207e S |
296 | view_count = self._extract_count( |
297 | r'<span class="count">([\d,\.]+)</span> views', webpage, 'view') | |
298 | like_count = self._extract_count( | |
299 | r'<span class="votesUp">([\d,\.]+)</span>', webpage, 'like') | |
300 | dislike_count = self._extract_count( | |
301 | r'<span class="votesDown">([\d,\.]+)</span>', webpage, 'dislike') | |
0320ddc1 | 302 | comment_count = self._extract_count( |
7700207e | 303 | r'All Comments\s*<span>\(([\d,.]+)\)', webpage, 'comment') |
0320ddc1 | 304 | |
5dda1ede | 305 | def extract_list(meta_key): |
d2d970d0 | 306 | div = self._search_regex( |
5dda1ede S |
307 | r'(?s)<div[^>]+\bclass=["\'].*?\b%sWrapper[^>]*>(.+?)</div>' |
308 | % meta_key, webpage, meta_key, default=None) | |
d2d970d0 | 309 | if div: |
5dda1ede | 310 | return re.findall(r'<a[^>]+\bhref=[^>]+>([^<]+)', div) |
6bb05b32 | 311 | |
125cfd78 | 312 | return { |
313 | 'id': video_id, | |
314 | 'uploader': video_uploader, | |
3661ebf2 | 315 | 'upload_date': upload_date, |
6c376029 | 316 | 'title': title, |
125cfd78 | 317 | 'thumbnail': thumbnail, |
ed8648a3 | 318 | 'duration': duration, |
0320ddc1 S |
319 | 'view_count': view_count, |
320 | 'like_count': like_count, | |
321 | 'dislike_count': dislike_count, | |
322 | 'comment_count': comment_count, | |
79367a98 | 323 | 'formats': formats, |
750e9833 | 324 | 'age_limit': 18, |
5dda1ede S |
325 | 'tags': extract_list('tags'), |
326 | 'categories': extract_list('categories'), | |
4938c8d5 | 327 | 'subtitles': subtitles, |
125cfd78 | 328 | } |
e66e1a00 S |
329 | |
330 | ||
71a1f617 | 331 | class PornHubPlaylistBaseIE(PornHubBaseIE): |
3430ff9b | 332 | def _extract_entries(self, webpage, host): |
475bcb22 S |
333 | # Only process container div with main playlist content skipping |
334 | # drop-down menu that uses similar pattern for videos (see | |
067aa17e | 335 | # https://github.com/ytdl-org/youtube-dl/issues/11594). |
475bcb22 S |
336 | container = self._search_regex( |
337 | r'(?s)(<div[^>]+class=["\']container.+)', webpage, | |
338 | 'container', default=webpage) | |
339 | ||
40e146aa | 340 | return [ |
3a23bae9 | 341 | self.url_result( |
3430ff9b | 342 | 'http://www.%s/%s' % (host, video_url), |
3a23bae9 S |
343 | PornHubIE.ie_key(), video_title=title) |
344 | for video_url, title in orderedSet(re.findall( | |
345 | r'href="/?(view_video\.php\?.*\bviewkey=[\da-z]+[^"]*)"[^>]*\s+title="([^"]+)"', | |
475bcb22 | 346 | container)) |
40e146aa | 347 | ] |
e66e1a00 S |
348 | |
349 | def _real_extract(self, url): | |
3430ff9b S |
350 | mobj = re.match(self._VALID_URL, url) |
351 | host = mobj.group('host') | |
352 | playlist_id = mobj.group('id') | |
e66e1a00 S |
353 | |
354 | webpage = self._download_webpage(url, playlist_id) | |
355 | ||
3430ff9b | 356 | entries = self._extract_entries(webpage, host) |
e66e1a00 S |
357 | |
358 | playlist = self._parse_json( | |
359 | self._search_regex( | |
cd138d8b S |
360 | r'(?:playlistObject|PLAYLIST_VIEW)\s*=\s*({.+?});', webpage, |
361 | 'playlist', default='{}'), | |
362 | playlist_id, fatal=False) | |
363 | title = playlist.get('title') or self._search_regex( | |
364 | r'>Videos\s+in\s+(.+?)\s+[Pp]laylist<', webpage, 'title', fatal=False) | |
e66e1a00 S |
365 | |
366 | return self.playlist_result( | |
cd138d8b | 367 | entries, playlist_id, title, playlist.get('description')) |
40e146aa S |
368 | |
369 | ||
370 | class PornHubPlaylistIE(PornHubPlaylistBaseIE): | |
3430ff9b | 371 | _VALID_URL = r'https?://(?:[^/]+\.)?(?P<host>pornhub\.(?:com|net))/playlist/(?P<id>\d+)' |
40e146aa | 372 | _TESTS = [{ |
96d315c2 | 373 | 'url': 'http://www.pornhub.com/playlist/4667351', |
40e146aa | 374 | 'info_dict': { |
96d315c2 S |
375 | 'id': '4667351', |
376 | 'title': 'Nataly Hot', | |
40e146aa | 377 | }, |
96d315c2 | 378 | 'playlist_mincount': 2, |
92ded33a S |
379 | }, { |
380 | 'url': 'https://de.pornhub.com/playlist/4667351', | |
381 | 'only_matching': True, | |
40e146aa S |
382 | }] |
383 | ||
384 | ||
385 | class PornHubUserVideosIE(PornHubPlaylistBaseIE): | |
3430ff9b | 386 | _VALID_URL = r'https?://(?:[^/]+\.)?(?P<host>pornhub\.(?:com|net))/(?:(?:user|channel)s|model|pornstar)/(?P<id>[^/]+)/videos' |
40e146aa | 387 | _TESTS = [{ |
34541395 | 388 | 'url': 'http://www.pornhub.com/users/zoe_ph/videos/public', |
40e146aa | 389 | 'info_dict': { |
34541395 | 390 | 'id': 'zoe_ph', |
40e146aa | 391 | }, |
34541395 S |
392 | 'playlist_mincount': 171, |
393 | }, { | |
394 | 'url': 'http://www.pornhub.com/users/rushandlia/videos', | |
395 | 'only_matching': True, | |
f66df20c PV |
396 | }, { |
397 | # default sorting as Top Rated Videos | |
398 | 'url': 'https://www.pornhub.com/channels/povd/videos', | |
399 | 'info_dict': { | |
400 | 'id': 'povd', | |
401 | }, | |
402 | 'playlist_mincount': 293, | |
403 | }, { | |
404 | # Top Rated Videos | |
405 | 'url': 'https://www.pornhub.com/channels/povd/videos?o=ra', | |
406 | 'only_matching': True, | |
407 | }, { | |
408 | # Most Recent Videos | |
409 | 'url': 'https://www.pornhub.com/channels/povd/videos?o=da', | |
410 | 'only_matching': True, | |
411 | }, { | |
412 | # Most Viewed Videos | |
413 | 'url': 'https://www.pornhub.com/channels/povd/videos?o=vi', | |
414 | 'only_matching': True, | |
92ded33a S |
415 | }, { |
416 | 'url': 'http://www.pornhub.com/users/zoe_ph/videos/public', | |
417 | 'only_matching': True, | |
2d4fe594 S |
418 | }, { |
419 | 'url': 'https://www.pornhub.com/model/jayndrea/videos/upload', | |
420 | 'only_matching': True, | |
421 | }, { | |
422 | 'url': 'https://www.pornhub.com/pornstar/jenny-blighe/videos/upload', | |
423 | 'only_matching': True, | |
40e146aa S |
424 | }] |
425 | ||
426 | def _real_extract(self, url): | |
3430ff9b S |
427 | mobj = re.match(self._VALID_URL, url) |
428 | host = mobj.group('host') | |
429 | user_id = mobj.group('id') | |
40e146aa | 430 | |
34541395 S |
431 | entries = [] |
432 | for page_num in itertools.count(1): | |
433 | try: | |
434 | webpage = self._download_webpage( | |
435 | url, user_id, 'Downloading page %d' % page_num, | |
436 | query={'page': page_num}) | |
437 | except ExtractorError as e: | |
438 | if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404: | |
439 | break | |
9edcdac9 | 440 | raise |
3430ff9b | 441 | page_entries = self._extract_entries(webpage, host) |
34541395 S |
442 | if not page_entries: |
443 | break | |
444 | entries.extend(page_entries) | |
445 | ||
446 | return self.playlist_result(entries, user_id) |