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