]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/pornhub.py
[xtube] Fix extraction (Closes #9953, closes #9961)
[yt-dlp.git] / 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 'url': 'https://www.thumbzilla.com/video/ph56c6114abd99a/horny-girlfriend-sex',
87 'only_matching': True,
88 }]
89
90 @classmethod
91 def _extract_url(cls, webpage):
92 mobj = re.search(
93 r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?pornhub\.com/embed/\d+)\1', webpage)
94 if mobj:
95 return mobj.group('url')
96
97 def _extract_count(self, pattern, webpage, name):
98 return str_to_int(self._search_regex(
99 pattern, webpage, '%s count' % name, fatal=False))
100
101 def _real_extract(self, url):
102 video_id = self._match_id(url)
103
104 req = sanitized_Request(
105 'http://www.pornhub.com/view_video.php?viewkey=%s' % video_id)
106 req.add_header('Cookie', 'age_verified=1')
107 webpage = self._download_webpage(req, video_id)
108
109 error_msg = self._html_search_regex(
110 r'(?s)<div[^>]+class=(["\']).*?\bremoved\b.*?\1[^>]*>(?P<error>.+?)</div>',
111 webpage, 'error message', default=None, group='error')
112 if error_msg:
113 error_msg = re.sub(r'\s+', ' ', error_msg)
114 raise ExtractorError(
115 'PornHub said: %s' % error_msg,
116 expected=True, video_id=video_id)
117
118 # video_title from flashvars contains whitespace instead of non-ASCII (see
119 # http://www.pornhub.com/view_video.php?viewkey=1331683002), not relying
120 # on that anymore.
121 title = self._html_search_meta(
122 'twitter:title', webpage, default=None) or self._search_regex(
123 (r'<h1[^>]+class=["\']title["\'][^>]*>(?P<title>[^<]+)',
124 r'<div[^>]+data-video-title=(["\'])(?P<title>.+?)\1',
125 r'shareTitle\s*=\s*(["\'])(?P<title>.+?)\1'),
126 webpage, 'title', group='title')
127
128 flashvars = self._parse_json(
129 self._search_regex(
130 r'var\s+flashvars_\d+\s*=\s*({.+?});', webpage, 'flashvars', default='{}'),
131 video_id)
132 if flashvars:
133 thumbnail = flashvars.get('image_url')
134 duration = int_or_none(flashvars.get('video_duration'))
135 else:
136 title, thumbnail, duration = [None] * 3
137
138 video_uploader = self._html_search_regex(
139 r'(?s)From:&nbsp;.+?<(?:a href="/users/|a href="/channels/|span class="username)[^>]+>(.+?)<',
140 webpage, 'uploader', fatal=False)
141
142 view_count = self._extract_count(
143 r'<span class="count">([\d,\.]+)</span> views', webpage, 'view')
144 like_count = self._extract_count(
145 r'<span class="votesUp">([\d,\.]+)</span>', webpage, 'like')
146 dislike_count = self._extract_count(
147 r'<span class="votesDown">([\d,\.]+)</span>', webpage, 'dislike')
148 comment_count = self._extract_count(
149 r'All Comments\s*<span>\(([\d,.]+)\)', webpage, 'comment')
150
151 video_urls = list(map(compat_urllib_parse_unquote, re.findall(r"player_quality_[0-9]{3}p\s*=\s*'([^']+)'", webpage)))
152 if webpage.find('"encrypted":true') != -1:
153 password = compat_urllib_parse_unquote_plus(
154 self._search_regex(r'"video_title":"([^"]+)', webpage, 'password'))
155 video_urls = list(map(lambda s: aes_decrypt_text(s, password, 32).decode('utf-8'), video_urls))
156
157 formats = []
158 for video_url in video_urls:
159 path = compat_urllib_parse_urlparse(video_url).path
160 extension = os.path.splitext(path)[1][1:]
161 format = path.split('/')[5].split('_')[:2]
162 format = '-'.join(format)
163
164 m = re.match(r'^(?P<height>[0-9]+)[pP]-(?P<tbr>[0-9]+)[kK]$', format)
165 if m is None:
166 height = None
167 tbr = None
168 else:
169 height = int(m.group('height'))
170 tbr = int(m.group('tbr'))
171
172 formats.append({
173 'url': video_url,
174 'ext': extension,
175 'format': format,
176 'format_id': format,
177 'tbr': tbr,
178 'height': height,
179 })
180 self._sort_formats(formats)
181
182 return {
183 'id': video_id,
184 'uploader': video_uploader,
185 'title': title,
186 'thumbnail': thumbnail,
187 'duration': duration,
188 'view_count': view_count,
189 'like_count': like_count,
190 'dislike_count': dislike_count,
191 'comment_count': comment_count,
192 'formats': formats,
193 'age_limit': 18,
194 }
195
196
197 class PornHubPlaylistBaseIE(InfoExtractor):
198 def _extract_entries(self, webpage):
199 return [
200 self.url_result(
201 'http://www.pornhub.com/%s' % video_url,
202 PornHubIE.ie_key(), video_title=title)
203 for video_url, title in orderedSet(re.findall(
204 r'href="/?(view_video\.php\?.*\bviewkey=[\da-z]+[^"]*)"[^>]*\s+title="([^"]+)"',
205 webpage))
206 ]
207
208 def _real_extract(self, url):
209 playlist_id = self._match_id(url)
210
211 webpage = self._download_webpage(url, playlist_id)
212
213 entries = self._extract_entries(webpage)
214
215 playlist = self._parse_json(
216 self._search_regex(
217 r'playlistObject\s*=\s*({.+?});', webpage, 'playlist'),
218 playlist_id)
219
220 return self.playlist_result(
221 entries, playlist_id, playlist.get('title'), playlist.get('description'))
222
223
224 class PornHubPlaylistIE(PornHubPlaylistBaseIE):
225 _VALID_URL = r'https?://(?:www\.)?pornhub\.com/playlist/(?P<id>\d+)'
226 _TESTS = [{
227 'url': 'http://www.pornhub.com/playlist/6201671',
228 'info_dict': {
229 'id': '6201671',
230 'title': 'P0p4',
231 },
232 'playlist_mincount': 35,
233 }]
234
235
236 class PornHubUserVideosIE(PornHubPlaylistBaseIE):
237 _VALID_URL = r'https?://(?:www\.)?pornhub\.com/users/(?P<id>[^/]+)/videos'
238 _TESTS = [{
239 'url': 'http://www.pornhub.com/users/zoe_ph/videos/public',
240 'info_dict': {
241 'id': 'zoe_ph',
242 },
243 'playlist_mincount': 171,
244 }, {
245 'url': 'http://www.pornhub.com/users/rushandlia/videos',
246 'only_matching': True,
247 }]
248
249 def _real_extract(self, url):
250 user_id = self._match_id(url)
251
252 entries = []
253 for page_num in itertools.count(1):
254 try:
255 webpage = self._download_webpage(
256 url, user_id, 'Downloading page %d' % page_num,
257 query={'page': page_num})
258 except ExtractorError as e:
259 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
260 break
261 page_entries = self._extract_entries(webpage)
262 if not page_entries:
263 break
264 entries.extend(page_entries)
265
266 return self.playlist_result(entries, user_id)