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