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