]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/pornhub.py
[godtv] Remove extractor (closes #13175)
[yt-dlp.git] / youtube_dl / extractor / pornhub.py
CommitLineData
6c376029 1# coding: utf-8
9933b574
PH
2from __future__ import unicode_literals
3
21fbf0f9 4import functools
34541395 5import itertools
21fbf0f9 6import operator
9a372f14 7# import os
125cfd78 8import re
9
10from .common import InfoExtractor
1cc79574 11from ..compat import (
34541395 12 compat_HTTPError,
9a372f14
S
13 # compat_urllib_parse_unquote,
14 # compat_urllib_parse_unquote_plus,
15 # compat_urllib_parse_urlparse,
1cc79574
PH
16)
17from ..utils import (
50789175 18 ExtractorError,
ed8648a3 19 int_or_none,
6bb05b32 20 js_to_json,
8f9a477e 21 orderedSet,
9a372f14 22 # sanitized_Request,
e1e35d1a 23 remove_quotes,
0320ddc1 24 str_to_int,
125cfd78 25)
9a372f14
S
26# from ..aes import (
27# aes_decrypt_text
28# )
125cfd78 29
9933b574 30
125cfd78 31class PornHubIE(InfoExtractor):
bc4b2d75
S
32 IE_DESC = 'PornHub and Thumbzilla'
33 _VALID_URL = r'''(?x)
34 https?://
35 (?:
a99cc4ca 36 (?:[a-z]+\.)?pornhub\.com/(?:(?:view_video\.php|video/show)\?viewkey=|embed/)|
bc4b2d75
S
37 (?:www\.)?thumbzilla\.com/video/
38 )
b52c9ef1 39 (?P<id>[\da-z]+)
bc4b2d75 40 '''
360075e2 41 _TESTS = [{
9933b574 42 'url': 'http://www.pornhub.com/view_video.php?viewkey=648719015',
ed8648a3 43 'md5': '1e19b41231a02eba417839222ac9d58e',
9933b574 44 'info_dict': {
249efaf4
PH
45 'id': '648719015',
46 'ext': 'mp4',
611c1dd9 47 'title': 'Seductive Indian beauty strips down and fingers her pink pussy',
ed8648a3
S
48 'uploader': 'Babes',
49 'duration': 361,
50 'view_count': int,
51 'like_count': int,
52 'dislike_count': int,
53 'comment_count': int,
54 'age_limit': 18,
6bb05b32
YCH
55 'tags': list,
56 'categories': list,
6c376029
S
57 },
58 }, {
59 # non-ASCII title
60 'url': 'http://www.pornhub.com/view_video.php?viewkey=1331683002',
61 'info_dict': {
62 'id': '1331683002',
63 'ext': 'mp4',
64 'title': '重庆婷婷女王足交',
65 'uploader': 'cj397186295',
66 'duration': 1753,
67 'view_count': int,
68 'like_count': int,
69 'dislike_count': int,
70 'comment_count': int,
71 'age_limit': 18,
6bb05b32
YCH
72 'tags': list,
73 'categories': list,
6c376029
S
74 },
75 'params': {
76 'skip_download': True,
77 },
360075e2
S
78 }, {
79 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph557bbb6676d2d',
80 'only_matching': True,
272e4db5 81 }, {
eaaaaec0 82 # removed at the request of cam4.com
272e4db5
S
83 'url': 'http://fr.pornhub.com/view_video.php?viewkey=ph55ca2f9760862',
84 'only_matching': True,
eaaaaec0
S
85 }, {
86 # removed at the request of the copyright owner
87 'url': 'http://www.pornhub.com/view_video.php?viewkey=788152859',
88 'only_matching': True,
89 }, {
90 # removed by uploader
91 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph572716d15a111',
92 'only_matching': True,
195f0845
S
93 }, {
94 # private video
95 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph56fd731fce6b7',
96 'only_matching': True,
bc4b2d75
S
97 }, {
98 'url': 'https://www.thumbzilla.com/video/ph56c6114abd99a/horny-girlfriend-sex',
99 'only_matching': True,
a99cc4ca
S
100 }, {
101 'url': 'http://www.pornhub.com/video/show?viewkey=648719015',
102 'only_matching': True,
360075e2 103 }]
125cfd78 104
b52c9ef1
S
105 @staticmethod
106 def _extract_urls(webpage):
107 return re.findall(
108 r'<iframe[^>]+?src=["\'](?P<url>(?:https?:)?//(?:www\.)?pornhub\.com/embed/[\da-z]+)',
109 webpage)
65d161c4 110
0320ddc1 111 def _extract_count(self, pattern, webpage, name):
7700207e
S
112 return str_to_int(self._search_regex(
113 pattern, webpage, '%s count' % name, fatal=False))
0320ddc1 114
125cfd78 115 def _real_extract(self, url):
249efaf4 116 video_id = self._match_id(url)
125cfd78 117
9a372f14
S
118 def dl_webpage(platform):
119 return self._download_webpage(
120 'http://www.pornhub.com/view_video.php?viewkey=%s' % video_id,
121 video_id, headers={
122 'Cookie': 'age_verified=1; platform=%s' % platform,
123 })
124
125 webpage = dl_webpage('pc')
125cfd78 126
50789175 127 error_msg = self._html_search_regex(
add7d2a0 128 r'(?s)<div[^>]+class=(["\'])(?:(?!\1).)*\b(?:removed|userMessageSection)\b(?:(?!\1).)*\1[^>]*>(?P<error>.+?)</div>',
3cb3b600 129 webpage, 'error message', default=None, group='error')
50789175
PH
130 if error_msg:
131 error_msg = re.sub(r'\s+', ' ', error_msg)
132 raise ExtractorError(
133 'PornHub said: %s' % error_msg,
134 expected=True, video_id=video_id)
135
9a372f14
S
136 tv_webpage = dl_webpage('tv')
137
e1e35d1a
S
138 assignments = self._search_regex(
139 r'(var.+?mediastring.+?)</script>', tv_webpage,
140 'encoded url').split(';')
141
21fbf0f9
T
142 js_vars = {}
143
144 def parse_js_value(inp):
e1e35d1a
S
145 inp = re.sub(r'/\*(?:(?!\*/).)*?\*/', '', inp)
146 if '+' in inp:
147 inps = inp.split('+')
148 return functools.reduce(
149 operator.concat, map(parse_js_value, inps))
21fbf0f9
T
150 inp = inp.strip()
151 if inp in js_vars:
152 return js_vars[inp]
e1e35d1a 153 return remove_quotes(inp)
21fbf0f9
T
154
155 for assn in assignments:
156 assn = assn.strip()
e1e35d1a 157 if not assn:
21fbf0f9 158 continue
e1e35d1a
S
159 assn = re.sub(r'var\s+', '', assn)
160 vname, value = assn.split('=', 1)
21fbf0f9
T
161 js_vars[vname] = parse_js_value(value)
162
e1e35d1a 163 video_url = js_vars['mediastring']
9a372f14
S
164
165 title = self._search_regex(
166 r'<h1>([^>]+)</h1>', tv_webpage, 'title', default=None)
167
6c376029
S
168 # video_title from flashvars contains whitespace instead of non-ASCII (see
169 # http://www.pornhub.com/view_video.php?viewkey=1331683002), not relying
170 # on that anymore.
9a372f14 171 title = title or self._html_search_meta(
6c376029
S
172 'twitter:title', webpage, default=None) or self._search_regex(
173 (r'<h1[^>]+class=["\']title["\'][^>]*>(?P<title>[^<]+)',
174 r'<div[^>]+data-video-title=(["\'])(?P<title>.+?)\1',
175 r'shareTitle\s*=\s*(["\'])(?P<title>.+?)\1'),
176 webpage, 'title', group='title')
177
ed8648a3
S
178 flashvars = self._parse_json(
179 self._search_regex(
03442072 180 r'var\s+flashvars_\d+\s*=\s*({.+?});', webpage, 'flashvars', default='{}'),
ed8648a3
S
181 video_id)
182 if flashvars:
ed8648a3
S
183 thumbnail = flashvars.get('image_url')
184 duration = int_or_none(flashvars.get('video_duration'))
185 else:
6c376029 186 title, thumbnail, duration = [None] * 3
ed8648a3 187
0320ddc1 188 video_uploader = self._html_search_regex(
8fc642eb 189 r'(?s)From:&nbsp;.+?<(?:a href="/users/|a href="/channels/|span class="username)[^>]+>(.+?)<',
0320ddc1 190 webpage, 'uploader', fatal=False)
125cfd78 191
7700207e
S
192 view_count = self._extract_count(
193 r'<span class="count">([\d,\.]+)</span> views', webpage, 'view')
194 like_count = self._extract_count(
195 r'<span class="votesUp">([\d,\.]+)</span>', webpage, 'like')
196 dislike_count = self._extract_count(
197 r'<span class="votesDown">([\d,\.]+)</span>', webpage, 'dislike')
0320ddc1 198 comment_count = self._extract_count(
7700207e 199 r'All Comments\s*<span>\(([\d,.]+)\)', webpage, 'comment')
0320ddc1 200
6bb05b32
YCH
201 page_params = self._parse_json(self._search_regex(
202 r'page_params\.zoneDetails\[([\'"])[^\'"]+\1\]\s*=\s*(?P<data>{[^}]+})',
203 webpage, 'page parameters', group='data', default='{}'),
204 video_id, transform_source=js_to_json, fatal=False)
205 tags = categories = None
206 if page_params:
207 tags = page_params.get('tags', '').split(',')
208 categories = page_params.get('categories', '').split(',')
209
125cfd78 210 return {
211 'id': video_id,
9a372f14 212 'url': video_url,
125cfd78 213 'uploader': video_uploader,
6c376029 214 'title': title,
125cfd78 215 'thumbnail': thumbnail,
ed8648a3 216 'duration': duration,
0320ddc1
S
217 'view_count': view_count,
218 'like_count': like_count,
219 'dislike_count': dislike_count,
220 'comment_count': comment_count,
9a372f14 221 # 'formats': formats,
750e9833 222 'age_limit': 18,
6bb05b32
YCH
223 'tags': tags,
224 'categories': categories,
125cfd78 225 }
e66e1a00
S
226
227
40e146aa
S
228class PornHubPlaylistBaseIE(InfoExtractor):
229 def _extract_entries(self, webpage):
230 return [
3a23bae9
S
231 self.url_result(
232 'http://www.pornhub.com/%s' % video_url,
233 PornHubIE.ie_key(), video_title=title)
234 for video_url, title in orderedSet(re.findall(
235 r'href="/?(view_video\.php\?.*\bviewkey=[\da-z]+[^"]*)"[^>]*\s+title="([^"]+)"',
236 webpage))
40e146aa 237 ]
e66e1a00
S
238
239 def _real_extract(self, url):
240 playlist_id = self._match_id(url)
241
242 webpage = self._download_webpage(url, playlist_id)
243
96d315c2
S
244 # Only process container div with main playlist content skipping
245 # drop-down menu that uses similar pattern for videos (see
246 # https://github.com/rg3/youtube-dl/issues/11594).
247 container = self._search_regex(
248 r'(?s)(<div[^>]+class=["\']container.+)', webpage,
249 'container', default=webpage)
250
251 entries = self._extract_entries(container)
e66e1a00
S
252
253 playlist = self._parse_json(
254 self._search_regex(
255 r'playlistObject\s*=\s*({.+?});', webpage, 'playlist'),
256 playlist_id)
257
258 return self.playlist_result(
259 entries, playlist_id, playlist.get('title'), playlist.get('description'))
40e146aa
S
260
261
262class PornHubPlaylistIE(PornHubPlaylistBaseIE):
263 _VALID_URL = r'https?://(?:www\.)?pornhub\.com/playlist/(?P<id>\d+)'
264 _TESTS = [{
96d315c2 265 'url': 'http://www.pornhub.com/playlist/4667351',
40e146aa 266 'info_dict': {
96d315c2
S
267 'id': '4667351',
268 'title': 'Nataly Hot',
40e146aa 269 },
96d315c2 270 'playlist_mincount': 2,
40e146aa
S
271 }]
272
273
274class PornHubUserVideosIE(PornHubPlaylistBaseIE):
275 _VALID_URL = r'https?://(?:www\.)?pornhub\.com/users/(?P<id>[^/]+)/videos'
276 _TESTS = [{
34541395 277 'url': 'http://www.pornhub.com/users/zoe_ph/videos/public',
40e146aa 278 'info_dict': {
34541395 279 'id': 'zoe_ph',
40e146aa 280 },
34541395
S
281 'playlist_mincount': 171,
282 }, {
283 'url': 'http://www.pornhub.com/users/rushandlia/videos',
284 'only_matching': True,
40e146aa
S
285 }]
286
287 def _real_extract(self, url):
288 user_id = self._match_id(url)
289
34541395
S
290 entries = []
291 for page_num in itertools.count(1):
292 try:
293 webpage = self._download_webpage(
294 url, user_id, 'Downloading page %d' % page_num,
295 query={'page': page_num})
296 except ExtractorError as e:
297 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
298 break
299 page_entries = self._extract_entries(webpage)
300 if not page_entries:
301 break
302 entries.extend(page_entries)
303
304 return self.playlist_result(entries, user_id)