| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://www.pornhub.com/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message, Dispatch |
| 12 |
from .. import text |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?(?:[\w-]+\.)?pornhub\.com" |
| 15 |
|
| 16 |
|
| 17 |
class PornhubExtractor(Extractor): |
| 18 |
"""Base class for pornhub extractors""" |
| 19 |
category = "pornhub" |
| 20 |
root = "https://www.pornhub.com" |
| 21 |
|
| 22 |
def _init(self): |
| 23 |
self.cookies.set( |
| 24 |
"accessAgeDisclaimerPH", "1", domain=".pornhub.com") |
| 25 |
|
| 26 |
def _pagination(self, user, path): |
| 27 |
if "/" not in path: |
| 28 |
path += "/public" |
| 29 |
|
| 30 |
url = f"{self.root}/{user}/{path}/ajax" |
| 31 |
params = {"page": 1} |
| 32 |
headers = { |
| 33 |
"Referer": url[:-5], |
| 34 |
"X-Requested-With": "XMLHttpRequest", |
| 35 |
} |
| 36 |
|
| 37 |
while True: |
| 38 |
response = self.request( |
| 39 |
url, method="POST", headers=headers, params=params, |
| 40 |
allow_redirects=False) |
| 41 |
|
| 42 |
if 300 <= response.status_code < 400: |
| 43 |
url = f"{self.root}{response.headers['location']}/{path}/ajax" |
| 44 |
continue |
| 45 |
|
| 46 |
yield response.text |
| 47 |
|
| 48 |
params["page"] += 1 |
| 49 |
|
| 50 |
|
| 51 |
class PornhubGalleryExtractor(PornhubExtractor): |
| 52 |
"""Extractor for image galleries on pornhub.com""" |
| 53 |
subcategory = "gallery" |
| 54 |
directory_fmt = ("{category}", "{user}", "{gallery[id]} {gallery[title]}") |
| 55 |
filename_fmt = "{num:>03}_{id}.{extension}" |
| 56 |
archive_fmt = "{id}" |
| 57 |
pattern = BASE_PATTERN + r"/album/(\d+)" |
| 58 |
example = "https://www.pornhub.com/album/12345" |
| 59 |
|
| 60 |
def __init__(self, match): |
| 61 |
PornhubExtractor.__init__(self, match) |
| 62 |
self.gallery_id = match[1] |
| 63 |
self._first = None |
| 64 |
|
| 65 |
def items(self): |
| 66 |
data = self.metadata() |
| 67 |
yield Message.Directory, "", data |
| 68 |
for num, img in enumerate(self.images(), 1): |
| 69 |
|
| 70 |
image = { |
| 71 |
"url" : img["img_large"], |
| 72 |
"caption": img["caption"], |
| 73 |
"id" : text.parse_int(img["id"]), |
| 74 |
"views" : text.parse_int(img["times_viewed"]), |
| 75 |
"score" : text.parse_int(img["vote_percent"]), |
| 76 |
"num" : num, |
| 77 |
} |
| 78 |
|
| 79 |
url = image["url"] |
| 80 |
image.update(data) |
| 81 |
yield Message.Url, url, text.nameext_from_url(url, image) |
| 82 |
|
| 83 |
def metadata(self): |
| 84 |
url = f"{self.root}/album/{self.gallery_id}" |
| 85 |
extr = text.extract_from(self.request(url).text) |
| 86 |
|
| 87 |
title = extr("<title>", "</title>") |
| 88 |
self._token = extr('data-token="', '"') |
| 89 |
score = extr('<div id="albumGreenBar" style="width:', '"') |
| 90 |
views = extr('<div id="viewsPhotAlbumCounter">', '<') |
| 91 |
tags = extr('<div id="photoTagsBox"', '<script') |
| 92 |
self._first = extr('<a href="/photo/', '"') |
| 93 |
title, _, user = title.rpartition(" - ") |
| 94 |
|
| 95 |
return { |
| 96 |
"user" : text.unescape(user[:-14]), |
| 97 |
"gallery": { |
| 98 |
"id" : text.parse_int(self.gallery_id), |
| 99 |
"title": text.unescape(title), |
| 100 |
"score": text.parse_int(score.partition("%")[0]), |
| 101 |
"views": text.parse_int(views.partition(" ")[0]), |
| 102 |
"tags" : text.split_html(tags)[2:], |
| 103 |
}, |
| 104 |
} |
| 105 |
|
| 106 |
def images(self): |
| 107 |
url = f"{self.root}/api/v1/album/{self.gallery_id}/show_album_json" |
| 108 |
params = {"token": self._token} |
| 109 |
data = self.request_json(url, params=params) |
| 110 |
|
| 111 |
if not (images := data.get("photos")): |
| 112 |
raise self.exc.AuthorizationError() |
| 113 |
key = end = self._first |
| 114 |
|
| 115 |
results = [] |
| 116 |
try: |
| 117 |
while True: |
| 118 |
img = images[key] |
| 119 |
results.append(img) |
| 120 |
key = str(img["next"]) |
| 121 |
if key == end: |
| 122 |
break |
| 123 |
except KeyError: |
| 124 |
self.log.warning("%s: Unable to ensure correct file order", |
| 125 |
self.gallery_id) |
| 126 |
return images.values() |
| 127 |
|
| 128 |
return results |
| 129 |
|
| 130 |
|
| 131 |
class PornhubGifExtractor(PornhubExtractor): |
| 132 |
"""Extractor for pornhub.com gifs""" |
| 133 |
subcategory = "gif" |
| 134 |
directory_fmt = ("{category}", "{user}", "gifs") |
| 135 |
filename_fmt = "{id} {title}.{extension}" |
| 136 |
archive_fmt = "{id}" |
| 137 |
pattern = BASE_PATTERN + r"/gif/(\d+)" |
| 138 |
example = "https://www.pornhub.com/gif/12345" |
| 139 |
|
| 140 |
def __init__(self, match): |
| 141 |
PornhubExtractor.__init__(self, match) |
| 142 |
self.gallery_id = match[1] |
| 143 |
|
| 144 |
def items(self): |
| 145 |
url = f"{self.root}/gif/{self.gallery_id}" |
| 146 |
extr = text.extract_from(self.request(url).text) |
| 147 |
|
| 148 |
gif = { |
| 149 |
"id" : self.gallery_id, |
| 150 |
"tags" : extr("data-context-tag='", "'").split(","), |
| 151 |
"title": extr('"name": "', '"'), |
| 152 |
"url" : extr('"contentUrl": "', '"'), |
| 153 |
"date" : self.parse_datetime_iso(extr('"uploadDate": "', '"')), |
| 154 |
"viewkey" : extr('From this video: ' |
| 155 |
'<a href="/view_video.php?viewkey=', '"'), |
| 156 |
"timestamp": extr('lass="directLink tstamp" rel="nofollow">', '<'), |
| 157 |
"user" : text.remove_html(extr("Created by:", "</div>")), |
| 158 |
} |
| 159 |
|
| 160 |
yield Message.Directory, "", gif |
| 161 |
yield Message.Url, gif["url"], text.nameext_from_url(gif["url"], gif) |
| 162 |
|
| 163 |
|
| 164 |
class PornhubUserExtractor(Dispatch, PornhubExtractor): |
| 165 |
"""Extractor for a pornhub user""" |
| 166 |
pattern = BASE_PATTERN + r"/((?:users|model|pornstar)/[^/?#]+)/?$" |
| 167 |
example = "https://www.pornhub.com/model/USER" |
| 168 |
|
| 169 |
def items(self): |
| 170 |
base = f"{self.root}/{self.groups[0]}/" |
| 171 |
return self._dispatch_extractors(( |
| 172 |
(PornhubPhotosExtractor, base + "photos"), |
| 173 |
(PornhubGifsExtractor , base + "gifs"), |
| 174 |
), ("photos",)) |
| 175 |
|
| 176 |
|
| 177 |
class PornhubPhotosExtractor(PornhubExtractor): |
| 178 |
"""Extractor for all galleries of a pornhub user""" |
| 179 |
subcategory = "photos" |
| 180 |
pattern = (BASE_PATTERN + r"/((?:users|model|pornstar)/[^/?#]+)" |
| 181 |
"/(photos(?:/[^/?#]+)?)") |
| 182 |
example = "https://www.pornhub.com/model/USER/photos" |
| 183 |
|
| 184 |
def __init__(self, match): |
| 185 |
PornhubExtractor.__init__(self, match) |
| 186 |
self.user, self.path = match.groups() |
| 187 |
|
| 188 |
def items(self): |
| 189 |
data = {"_extractor": PornhubGalleryExtractor} |
| 190 |
for page in self._pagination(self.user, self.path): |
| 191 |
gid = None |
| 192 |
for gid in text.extract_iter(page, 'id="albumphoto', '"'): |
| 193 |
yield Message.Queue, self.root + "/album/" + gid, data |
| 194 |
if gid is None: |
| 195 |
return |
| 196 |
|
| 197 |
|
| 198 |
class PornhubGifsExtractor(PornhubExtractor): |
| 199 |
"""Extractor for a pornhub user's gifs""" |
| 200 |
subcategory = "gifs" |
| 201 |
pattern = (BASE_PATTERN + r"/((?:users|model|pornstar)/[^/?#]+)" |
| 202 |
"/(gifs(?:/[^/?#]+)?)") |
| 203 |
example = "https://www.pornhub.com/model/USER/gifs" |
| 204 |
|
| 205 |
def __init__(self, match): |
| 206 |
PornhubExtractor.__init__(self, match) |
| 207 |
self.user, self.path = match.groups() |
| 208 |
|
| 209 |
def items(self): |
| 210 |
data = {"_extractor": PornhubGifExtractor} |
| 211 |
for page in self._pagination(self.user, self.path): |
| 212 |
gid = None |
| 213 |
for gid in text.extract_iter(page, 'id="gif', '"'): |
| 214 |
yield Message.Queue, self.root + "/gif/" + gid, data |
| 215 |
if gid is None: |
| 216 |
return |