[pornhub] Fix tags and categories extraction (closes #13720)
[youtube-dl] / youtube_dl / extractor / pornhub.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import functools
5 import itertools
6 import operator
7 import re
8
9 from .common import InfoExtractor
10 from ..compat import (
11     compat_HTTPError,
12     compat_str,
13     compat_urllib_request,
14 )
15 from .openload import PhantomJSwrapper
16 from ..utils import (
17     ExtractorError,
18     int_or_none,
19     orderedSet,
20     remove_quotes,
21     str_to_int,
22     url_or_none,
23 )
24
25
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):
49     IE_DESC = 'PornHub and Thumbzilla'
50     _VALID_URL = r'''(?x)
51                     https?://
52                         (?:
53                             (?:[^/]+\.)?(?P<host>pornhub\.(?:com|net))/(?:(?:view_video\.php|video/show)\?viewkey=|embed/)|
54                             (?:www\.)?thumbzilla\.com/video/
55                         )
56                         (?P<id>[\da-z]+)
57                     '''
58     _TESTS = [{
59         'url': 'http://www.pornhub.com/view_video.php?viewkey=648719015',
60         'md5': '1e19b41231a02eba417839222ac9d58e',
61         'info_dict': {
62             'id': '648719015',
63             'ext': 'mp4',
64             'title': 'Seductive Indian beauty strips down and fingers her pink pussy',
65             'uploader': 'Babes',
66             'upload_date': '20130628',
67             'duration': 361,
68             'view_count': int,
69             'like_count': int,
70             'dislike_count': int,
71             'comment_count': int,
72             'age_limit': 18,
73             'tags': list,
74             'categories': list,
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': '重庆婷婷女王足交',
83             'uploader': 'Unknown',
84             'upload_date': '20150213',
85             'duration': 1753,
86             'view_count': int,
87             'like_count': int,
88             'dislike_count': int,
89             'comment_count': int,
90             'age_limit': 18,
91             'tags': list,
92             'categories': list,
93         },
94         'params': {
95             'skip_download': True,
96         },
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         },
122     }, {
123         'url': 'http://www.pornhub.com/view_video.php?viewkey=ph557bbb6676d2d',
124         'only_matching': True,
125     }, {
126         # removed at the request of cam4.com
127         'url': 'http://fr.pornhub.com/view_video.php?viewkey=ph55ca2f9760862',
128         'only_matching': True,
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,
137     }, {
138         # private video
139         'url': 'http://www.pornhub.com/view_video.php?viewkey=ph56fd731fce6b7',
140         'only_matching': True,
141     }, {
142         'url': 'https://www.thumbzilla.com/video/ph56c6114abd99a/horny-girlfriend-sex',
143         'only_matching': True,
144     }, {
145         'url': 'http://www.pornhub.com/video/show?viewkey=648719015',
146         'only_matching': True,
147     }, {
148         'url': 'https://www.pornhub.net/view_video.php?viewkey=203640933',
149         'only_matching': True,
150     }]
151
152     @staticmethod
153     def _extract_urls(webpage):
154         return re.findall(
155             r'<iframe[^>]+?src=["\'](?P<url>(?:https?:)?//(?:www\.)?pornhub\.(?:com|net)/embed/[\da-z]+)',
156             webpage)
157
158     def _extract_count(self, pattern, webpage, name):
159         return str_to_int(self._search_regex(
160             pattern, webpage, '%s count' % name, fatal=False))
161
162     def _real_extract(self, url):
163         mobj = re.match(self._VALID_URL, url)
164         host = mobj.group('host') or 'pornhub.com'
165         video_id = mobj.group('id')
166
167         self._set_cookie(host, 'age_verified', '1')
168
169         def dl_webpage(platform):
170             self._set_cookie(host, 'platform', platform)
171             return self._download_webpage(
172                 'http://www.%s/view_video.php?viewkey=%s' % (host, video_id),
173                 video_id, 'Downloading %s webpage' % platform)
174
175         webpage = dl_webpage('pc')
176
177         error_msg = self._html_search_regex(
178             r'(?s)<div[^>]+class=(["\'])(?:(?!\1).)*\b(?:removed|userMessageSection)\b(?:(?!\1).)*\1[^>]*>(?P<error>.+?)</div>',
179             webpage, 'error message', default=None, group='error')
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
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.
189         title = self._html_search_meta(
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
196         video_urls = []
197         video_urls_set = set()
198         subtitles = {}
199
200         flashvars = self._parse_json(
201             self._search_regex(
202                 r'var\s+flashvars_\d+\s*=\s*({.+?});', webpage, 'flashvars', default='{}'),
203             video_id)
204         if flashvars:
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                 })
211             thumbnail = flashvars.get('image_url')
212             duration = int_or_none(flashvars.get('video_duration'))
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'))))
226         else:
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
270         upload_date = None
271         formats = []
272         for video_url, height in video_urls:
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('/', '')
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)
291
292         video_uploader = self._html_search_regex(
293             r'(?s)From:&nbsp;.+?<(?:a\b[^>]+\bhref=["\']/(?:(?:user|channel)s|model|pornstar)/|span\b[^>]+\bclass=["\']username)[^>]+>(.+?)<',
294             webpage, 'uploader', fatal=False)
295
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')
302         comment_count = self._extract_count(
303             r'All Comments\s*<span>\(([\d,.]+)\)', webpage, 'comment')
304
305         def _get_items(class_name):
306             div = self._search_regex(
307                 r'<div class="' + class_name + '">([\S\s]+?)</div>',
308                 webpage, class_name, default=None)
309             if div:
310                 return [a for a in re.findall(r'<a href=[^>]+>([^<]+)', div)]
311             else:
312                 return None
313
314         categories = _get_items('categoriesWrapper')
315         tags = _get_items('tagsWrapper')
316
317         return {
318             'id': video_id,
319             'uploader': video_uploader,
320             'upload_date': upload_date,
321             'title': title,
322             'thumbnail': thumbnail,
323             'duration': duration,
324             'view_count': view_count,
325             'like_count': like_count,
326             'dislike_count': dislike_count,
327             'comment_count': comment_count,
328             'formats': formats,
329             'age_limit': 18,
330             'tags': tags,
331             'categories': categories,
332             'subtitles': subtitles,
333         }
334
335
336 class PornHubPlaylistBaseIE(PornHubBaseIE):
337     def _extract_entries(self, webpage, host):
338         # Only process container div with main playlist content skipping
339         # drop-down menu that uses similar pattern for videos (see
340         # https://github.com/rg3/youtube-dl/issues/11594).
341         container = self._search_regex(
342             r'(?s)(<div[^>]+class=["\']container.+)', webpage,
343             'container', default=webpage)
344
345         return [
346             self.url_result(
347                 'http://www.%s/%s' % (host, video_url),
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="([^"]+)"',
351                 container))
352         ]
353
354     def _real_extract(self, url):
355         mobj = re.match(self._VALID_URL, url)
356         host = mobj.group('host')
357         playlist_id = mobj.group('id')
358
359         webpage = self._download_webpage(url, playlist_id)
360
361         entries = self._extract_entries(webpage, host)
362
363         playlist = self._parse_json(
364             self._search_regex(
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)
370
371         return self.playlist_result(
372             entries, playlist_id, title, playlist.get('description'))
373
374
375 class PornHubPlaylistIE(PornHubPlaylistBaseIE):
376     _VALID_URL = r'https?://(?:[^/]+\.)?(?P<host>pornhub\.(?:com|net))/playlist/(?P<id>\d+)'
377     _TESTS = [{
378         'url': 'http://www.pornhub.com/playlist/4667351',
379         'info_dict': {
380             'id': '4667351',
381             'title': 'Nataly Hot',
382         },
383         'playlist_mincount': 2,
384     }, {
385         'url': 'https://de.pornhub.com/playlist/4667351',
386         'only_matching': True,
387     }]
388
389
390 class PornHubUserVideosIE(PornHubPlaylistBaseIE):
391     _VALID_URL = r'https?://(?:[^/]+\.)?(?P<host>pornhub\.(?:com|net))/(?:(?:user|channel)s|model|pornstar)/(?P<id>[^/]+)/videos'
392     _TESTS = [{
393         'url': 'http://www.pornhub.com/users/zoe_ph/videos/public',
394         'info_dict': {
395             'id': 'zoe_ph',
396         },
397         'playlist_mincount': 171,
398     }, {
399         'url': 'http://www.pornhub.com/users/rushandlia/videos',
400         'only_matching': True,
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,
420     }, {
421         'url': 'http://www.pornhub.com/users/zoe_ph/videos/public',
422         'only_matching': True,
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,
429     }]
430
431     def _real_extract(self, url):
432         mobj = re.match(self._VALID_URL, url)
433         host = mobj.group('host')
434         user_id = mobj.group('id')
435
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
445                 raise
446             page_entries = self._extract_entries(webpage, host)
447             if not page_entries:
448                 break
449             entries.extend(page_entries)
450
451         return self.playlist_result(entries, user_id)