]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/pornhub.py
[pornhub:playlistbase] Do not include videos not from playlist
[yt-dlp.git] / youtube_dl / extractor / pornhub.py
CommitLineData
9933b574
PH
1from __future__ import unicode_literals
2
125cfd78 3import os
4import re
5
6from .common import InfoExtractor
1cc79574 7from ..compat import (
605cbef6
S
8 compat_urllib_parse_unquote,
9 compat_urllib_parse_unquote_plus,
125cfd78 10 compat_urllib_parse_urlparse,
1cc79574
PH
11)
12from ..utils import (
50789175 13 ExtractorError,
ed8648a3 14 int_or_none,
8f9a477e 15 orderedSet,
5c2266df 16 sanitized_Request,
0320ddc1 17 str_to_int,
125cfd78 18)
19from ..aes import (
20 aes_decrypt_text
21)
22
9933b574 23
125cfd78 24class PornHubIE(InfoExtractor):
272e4db5 25 _VALID_URL = r'https?://(?:[a-z]+\.)?pornhub\.com/(?:view_video\.php\?viewkey=|embed/)(?P<id>[0-9a-z]+)'
360075e2 26 _TESTS = [{
9933b574 27 'url': 'http://www.pornhub.com/view_video.php?viewkey=648719015',
ed8648a3 28 'md5': '1e19b41231a02eba417839222ac9d58e',
9933b574 29 'info_dict': {
249efaf4
PH
30 'id': '648719015',
31 'ext': 'mp4',
611c1dd9 32 'title': 'Seductive Indian beauty strips down and fingers her pink pussy',
ed8648a3
S
33 'uploader': 'Babes',
34 'duration': 361,
35 'view_count': int,
36 'like_count': int,
37 'dislike_count': int,
38 'comment_count': int,
39 'age_limit': 18,
125cfd78 40 }
360075e2
S
41 }, {
42 'url': 'http://www.pornhub.com/view_video.php?viewkey=ph557bbb6676d2d',
43 'only_matching': True,
272e4db5
S
44 }, {
45 'url': 'http://fr.pornhub.com/view_video.php?viewkey=ph55ca2f9760862',
46 'only_matching': True,
360075e2 47 }]
125cfd78 48
65d161c4
S
49 @classmethod
50 def _extract_url(cls, webpage):
51 mobj = re.search(
52 r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?pornhub\.com/embed/\d+)\1', webpage)
53 if mobj:
54 return mobj.group('url')
55
0320ddc1 56 def _extract_count(self, pattern, webpage, name):
7700207e
S
57 return str_to_int(self._search_regex(
58 pattern, webpage, '%s count' % name, fatal=False))
0320ddc1 59
125cfd78 60 def _real_extract(self, url):
249efaf4 61 video_id = self._match_id(url)
125cfd78 62
5c2266df 63 req = sanitized_Request(
9fcbd5db 64 'http://www.pornhub.com/view_video.php?viewkey=%s' % video_id)
125cfd78 65 req.add_header('Cookie', 'age_verified=1')
66 webpage = self._download_webpage(req, video_id)
67
50789175
PH
68 error_msg = self._html_search_regex(
69 r'(?s)<div class="userMessageSection[^"]*".*?>(.*?)</div>',
70 webpage, 'error message', default=None)
71 if error_msg:
72 error_msg = re.sub(r'\s+', ' ', error_msg)
73 raise ExtractorError(
74 'PornHub said: %s' % error_msg,
75 expected=True, video_id=video_id)
76
ed8648a3
S
77 flashvars = self._parse_json(
78 self._search_regex(
79 r'var\s+flashv1ars_\d+\s*=\s*({.+?});', webpage, 'flashvars', default='{}'),
80 video_id)
81 if flashvars:
82 video_title = flashvars.get('video_title')
83 thumbnail = flashvars.get('image_url')
84 duration = int_or_none(flashvars.get('video_duration'))
85 else:
86 video_title, thumbnail, duration = [None] * 3
87
88 if not video_title:
89 video_title = self._html_search_regex(r'<h1 [^>]+>([^<]+)', webpage, 'title')
90
0320ddc1 91 video_uploader = self._html_search_regex(
8fc642eb 92 r'(?s)From:&nbsp;.+?<(?:a href="/users/|a href="/channels/|span class="username)[^>]+>(.+?)<',
0320ddc1 93 webpage, 'uploader', fatal=False)
125cfd78 94
7700207e
S
95 view_count = self._extract_count(
96 r'<span class="count">([\d,\.]+)</span> views', webpage, 'view')
97 like_count = self._extract_count(
98 r'<span class="votesUp">([\d,\.]+)</span>', webpage, 'like')
99 dislike_count = self._extract_count(
100 r'<span class="votesDown">([\d,\.]+)</span>', webpage, 'dislike')
0320ddc1 101 comment_count = self._extract_count(
7700207e 102 r'All Comments\s*<span>\(([\d,.]+)\)', webpage, 'comment')
0320ddc1 103
524229a2 104 video_urls = list(map(compat_urllib_parse_unquote, re.findall(r"player_quality_[0-9]{3}p\s*=\s*'([^']+)'", webpage)))
125cfd78 105 if webpage.find('"encrypted":true') != -1:
605cbef6 106 password = compat_urllib_parse_unquote_plus(
7a372b64 107 self._search_regex(r'"video_title":"([^"]+)', webpage, 'password'))
125cfd78 108 video_urls = list(map(lambda s: aes_decrypt_text(s, password, 32).decode('utf-8'), video_urls))
109
110 formats = []
111 for video_url in video_urls:
a56f9de1
JMF
112 path = compat_urllib_parse_urlparse(video_url).path
113 extension = os.path.splitext(path)[1][1:]
125cfd78 114 format = path.split('/')[5].split('_')[:2]
611c1dd9 115 format = '-'.join(format)
9933b574 116
8f5639af 117 m = re.match(r'^(?P<height>[0-9]+)[pP]-(?P<tbr>[0-9]+)[kK]$', format)
9933b574
PH
118 if m is None:
119 height = None
120 tbr = None
121 else:
122 height = int(m.group('height'))
123 tbr = int(m.group('tbr'))
124
125cfd78 125 formats.append({
126 'url': video_url,
127 'ext': extension,
128 'format': format,
129 'format_id': format,
9933b574
PH
130 'tbr': tbr,
131 'height': height,
125cfd78 132 })
9933b574 133 self._sort_formats(formats)
125cfd78 134
135 return {
136 'id': video_id,
137 'uploader': video_uploader,
138 'title': video_title,
139 'thumbnail': thumbnail,
ed8648a3 140 'duration': duration,
0320ddc1
S
141 'view_count': view_count,
142 'like_count': like_count,
143 'dislike_count': dislike_count,
144 'comment_count': comment_count,
125cfd78 145 'formats': formats,
750e9833 146 'age_limit': 18,
125cfd78 147 }
e66e1a00
S
148
149
40e146aa
S
150class PornHubPlaylistBaseIE(InfoExtractor):
151 def _extract_entries(self, webpage):
152 return [
3a23bae9
S
153 self.url_result(
154 'http://www.pornhub.com/%s' % video_url,
155 PornHubIE.ie_key(), video_title=title)
156 for video_url, title in orderedSet(re.findall(
157 r'href="/?(view_video\.php\?.*\bviewkey=[\da-z]+[^"]*)"[^>]*\s+title="([^"]+)"',
158 webpage))
40e146aa 159 ]
e66e1a00
S
160
161 def _real_extract(self, url):
162 playlist_id = self._match_id(url)
163
164 webpage = self._download_webpage(url, playlist_id)
165
40e146aa 166 entries = self._extract_entries(webpage)
e66e1a00
S
167
168 playlist = self._parse_json(
169 self._search_regex(
170 r'playlistObject\s*=\s*({.+?});', webpage, 'playlist'),
171 playlist_id)
172
173 return self.playlist_result(
174 entries, playlist_id, playlist.get('title'), playlist.get('description'))
40e146aa
S
175
176
177class PornHubPlaylistIE(PornHubPlaylistBaseIE):
178 _VALID_URL = r'https?://(?:www\.)?pornhub\.com/playlist/(?P<id>\d+)'
179 _TESTS = [{
180 'url': 'http://www.pornhub.com/playlist/6201671',
181 'info_dict': {
182 'id': '6201671',
183 'title': 'P0p4',
184 },
185 'playlist_mincount': 35,
186 }]
187
188
189class PornHubUserVideosIE(PornHubPlaylistBaseIE):
190 _VALID_URL = r'https?://(?:www\.)?pornhub\.com/users/(?P<id>[^/]+)/videos'
191 _TESTS = [{
192 'url': 'http://www.pornhub.com/users/rushandlia/videos',
193 'info_dict': {
194 'id': 'rushandlia',
195 },
196 'playlist_mincount': 13,
197 }]
198
199 def _real_extract(self, url):
200 user_id = self._match_id(url)
201
202 webpage = self._download_webpage(url, user_id)
203
204 return self.playlist_result(self._extract_entries(webpage), user_id)