]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/pornhub.py
debian/patches: Remove unneeded patches.
[youtubedl] / youtube_dl / extractor / pornhub.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import itertools
5 import os
6 import re
7
8 from .common import InfoExtractor
9 from ..compat import (
10 compat_HTTPError,
11 compat_urllib_parse_unquote,
12 compat_urllib_parse_unquote_plus,
13 compat_urllib_parse_urlparse,
14 )
15 from ..utils import (
16 ExtractorError,
17 int_or_none,
18 orderedSet,
19 sanitized_Request,
20 str_to_int,
21 )
22 from ..aes import (
23 aes_decrypt_text
24 )
25
26
27 class PornHubIE(InfoExtractor):
28 IE_DESC = 'PornHub and Thumbzilla'
29 _VALID_URL = r'''(?x)
30 https?://
31 (?:
32 (?:[a-z]+\.)?pornhub\.com/(?:view_video\.php\?viewkey=|embed/)|
33 (?:www\.)?thumbzilla\.com/video/
34 )
35 (?P<id>[0-9a-z]+)
36 '''
37 _TESTS = [{
38 'url': 'http://www.pornhub.com/view_video.php?viewkey=648719015',
39 'md5': '1e19b41231a02eba417839222ac9d58e',
40 'info_dict': {
41 'id': '648719015',
42 'ext': 'mp4',
43 'title': 'Seductive Indian beauty strips down and fingers her pink pussy',
44 'uploader': 'Babes',
45 'duration': 361,
46 'view_count': int,
47 'like_count': int,
48 'dislike_count': int,
49 'comment_count': int,
50 'age_limit': 18,
51 },
52 }, {
53 # non-ASCII title
54 'url': 'http://www.pornhub.com/view_video.php?viewkey=1331683002',
55 'info_dict': {
56 'id': '1331683002',
57 'ext': 'mp4',
58 'title': '重庆婷婷女王足交',
59 'uploader': 'cj397186295',
60 'duration': 1753,
61 'view_count': int,
62 'like_count': int,
63 'dislike_count': int,
64 'comment_count': int,
65 'age_limit': 18,
66 },
67 'params': {
68 'skip_download': True,
69 },
70 }, {
71 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph557bbb6676d2d',
72 'only_matching': True,
73 }, {
74 # removed at the request of cam4.com
75 'url': 'http://fr.pornhub.com/view_video.php?viewkey=ph55ca2f9760862',
76 'only_matching': True,
77 }, {
78 # removed at the request of the copyright owner
79 'url': 'http://www.pornhub.com/view_video.php?viewkey=788152859',
80 'only_matching': True,
81 }, {
82 # removed by uploader
83 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph572716d15a111',
84 'only_matching': True,
85 }, {
86 # private video
87 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph56fd731fce6b7',
88 'only_matching': True,
89 }, {
90 'url': 'https://www.thumbzilla.com/video/ph56c6114abd99a/horny-girlfriend-sex',
91 'only_matching': True,
92 }]
93
94 @classmethod
95 def _extract_url(cls, webpage):
96 mobj = re.search(
97 r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?pornhub\.com/embed/\d+)\1', webpage)
98 if mobj:
99 return mobj.group('url')
100
101 def _extract_count(self, pattern, webpage, name):
102 return str_to_int(self._search_regex(
103 pattern, webpage, '%s count' % name, fatal=False))
104
105 def _real_extract(self, url):
106 video_id = self._match_id(url)
107
108 req = sanitized_Request(
109 'http://www.pornhub.com/view_video.php?viewkey=%s' % video_id)
110 req.add_header('Cookie', 'age_verified=1')
111 webpage = self._download_webpage(req, video_id)
112
113 error_msg = self._html_search_regex(
114 r'(?s)<div[^>]+class=(["\'])(?:(?!\1).)*\b(?:removed|userMessageSection)\b(?:(?!\1).)*\1[^>]*>(?P<error>.+?)</div>',
115 webpage, 'error message', default=None, group='error')
116 if error_msg:
117 error_msg = re.sub(r'\s+', ' ', error_msg)
118 raise ExtractorError(
119 'PornHub said: %s' % error_msg,
120 expected=True, video_id=video_id)
121
122 # video_title from flashvars contains whitespace instead of non-ASCII (see
123 # http://www.pornhub.com/view_video.php?viewkey=1331683002), not relying
124 # on that anymore.
125 title = self._html_search_meta(
126 'twitter:title', webpage, default=None) or self._search_regex(
127 (r'<h1[^>]+class=["\']title["\'][^>]*>(?P<title>[^<]+)',
128 r'<div[^>]+data-video-title=(["\'])(?P<title>.+?)\1',
129 r'shareTitle\s*=\s*(["\'])(?P<title>.+?)\1'),
130 webpage, 'title', group='title')
131
132 flashvars = self._parse_json(
133 self._search_regex(
134 r'var\s+flashvars_\d+\s*=\s*({.+?});', webpage, 'flashvars', default='{}'),
135 video_id)
136 if flashvars:
137 thumbnail = flashvars.get('image_url')
138 duration = int_or_none(flashvars.get('video_duration'))
139 else:
140 title, thumbnail, duration = [None] * 3
141
142 video_uploader = self._html_search_regex(
143 r'(?s)From:&nbsp;.+?<(?:a href="/users/|a href="/channels/|span class="username)[^>]+>(.+?)<',
144 webpage, 'uploader', fatal=False)
145
146 view_count = self._extract_count(
147 r'<span class="count">([\d,\.]+)</span> views', webpage, 'view')
148 like_count = self._extract_count(
149 r'<span class="votesUp">([\d,\.]+)</span>', webpage, 'like')
150 dislike_count = self._extract_count(
151 r'<span class="votesDown">([\d,\.]+)</span>', webpage, 'dislike')
152 comment_count = self._extract_count(
153 r'All Comments\s*<span>\(([\d,.]+)\)', webpage, 'comment')
154
155 video_urls = list(map(compat_urllib_parse_unquote, re.findall(r"player_quality_[0-9]{3}p\s*=\s*'([^']+)'", webpage)))
156 if webpage.find('"encrypted":true') != -1:
157 password = compat_urllib_parse_unquote_plus(
158 self._search_regex(r'"video_title":"([^"]+)', webpage, 'password'))
159 video_urls = list(map(lambda s: aes_decrypt_text(s, password, 32).decode('utf-8'), video_urls))
160
161 formats = []
162 for video_url in video_urls:
163 path = compat_urllib_parse_urlparse(video_url).path
164 extension = os.path.splitext(path)[1][1:]
165 format = path.split('/')[5].split('_')[:2]
166 format = '-'.join(format)
167
168 m = re.match(r'^(?P<height>[0-9]+)[pP]-(?P<tbr>[0-9]+)[kK]$', format)
169 if m is None:
170 height = None
171 tbr = None
172 else:
173 height = int(m.group('height'))
174 tbr = int(m.group('tbr'))
175
176 formats.append({
177 'url': video_url,
178 'ext': extension,
179 'format': format,
180 'format_id': format,
181 'tbr': tbr,
182 'height': height,
183 })
184 self._sort_formats(formats)
185
186 return {
187 'id': video_id,
188 'uploader': video_uploader,
189 'title': title,
190 'thumbnail': thumbnail,
191 'duration': duration,
192 'view_count': view_count,
193 'like_count': like_count,
194 'dislike_count': dislike_count,
195 'comment_count': comment_count,
196 'formats': formats,
197 'age_limit': 18,
198 }
199
200
201 class PornHubPlaylistBaseIE(InfoExtractor):
202 def _extract_entries(self, webpage):
203 return [
204 self.url_result(
205 'http://www.pornhub.com/%s' % video_url,
206 PornHubIE.ie_key(), video_title=title)
207 for video_url, title in orderedSet(re.findall(
208 r'href="/?(view_video\.php\?.*\bviewkey=[\da-z]+[^"]*)"[^>]*\s+title="([^"]+)"',
209 webpage))
210 ]
211
212 def _real_extract(self, url):
213 playlist_id = self._match_id(url)
214
215 webpage = self._download_webpage(url, playlist_id)
216
217 entries = self._extract_entries(webpage)
218
219 playlist = self._parse_json(
220 self._search_regex(
221 r'playlistObject\s*=\s*({.+?});', webpage, 'playlist'),
222 playlist_id)
223
224 return self.playlist_result(
225 entries, playlist_id, playlist.get('title'), playlist.get('description'))
226
227
228 class PornHubPlaylistIE(PornHubPlaylistBaseIE):
229 _VALID_URL = r'https?://(?:www\.)?pornhub\.com/playlist/(?P<id>\d+)'
230 _TESTS = [{
231 'url': 'http://www.pornhub.com/playlist/6201671',
232 'info_dict': {
233 'id': '6201671',
234 'title': 'P0p4',
235 },
236 'playlist_mincount': 35,
237 }]
238
239
240 class PornHubUserVideosIE(PornHubPlaylistBaseIE):
241 _VALID_URL = r'https?://(?:www\.)?pornhub\.com/users/(?P<id>[^/]+)/videos'
242 _TESTS = [{
243 'url': 'http://www.pornhub.com/users/zoe_ph/videos/public',
244 'info_dict': {
245 'id': 'zoe_ph',
246 },
247 'playlist_mincount': 171,
248 }, {
249 'url': 'http://www.pornhub.com/users/rushandlia/videos',
250 'only_matching': True,
251 }]
252
253 def _real_extract(self, url):
254 user_id = self._match_id(url)
255
256 entries = []
257 for page_num in itertools.count(1):
258 try:
259 webpage = self._download_webpage(
260 url, user_id, 'Downloading page %d' % page_num,
261 query={'page': page_num})
262 except ExtractorError as e:
263 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
264 break
265 page_entries = self._extract_entries(webpage)
266 if not page_entries:
267 break
268 entries.extend(page_entries)
269
270 return self.playlist_result(entries, user_id)