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