| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://www.imagefap.com/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?(?:www\.|beta\.)?imagefap\.com" |
| 15 |
|
| 16 |
|
| 17 |
class ImagefapExtractor(Extractor): |
| 18 |
"""Base class for imagefap extractors""" |
| 19 |
category = "imagefap" |
| 20 |
root = "https://www.imagefap.com" |
| 21 |
directory_fmt = ("{category}", "{gallery_id} {title}") |
| 22 |
filename_fmt = ("{category}_{gallery_id}_{num:?/_/>04}" |
| 23 |
"{filename}.{extension}") |
| 24 |
archive_fmt = "{gallery_id}_{image_id}" |
| 25 |
request_interval = (2.0, 4.0) |
| 26 |
|
| 27 |
def request(self, url, **kwargs): |
| 28 |
response = Extractor.request(self, url, **kwargs) |
| 29 |
|
| 30 |
if response.history and response.url.endswith("/human-verification"): |
| 31 |
self.log.warning("HTTP redirect to '%s'", response.url) |
| 32 |
if msg := text.extr(response.text, '<div class="mt-4', '<'): |
| 33 |
msg = " ".join(msg.partition(">")[2].split()) |
| 34 |
raise self.exc.AbortExtraction(f"'{msg}'") |
| 35 |
|
| 36 |
return response |
| 37 |
|
| 38 |
|
| 39 |
class ImagefapGalleryExtractor(ImagefapExtractor): |
| 40 |
"""Extractor for image galleries from imagefap.com""" |
| 41 |
subcategory = "gallery" |
| 42 |
pattern = BASE_PATTERN + r"/(?:gallery\.php\?gid=|gallery/|pictures/)(\d+)" |
| 43 |
example = "https://www.imagefap.com/gallery/12345" |
| 44 |
|
| 45 |
def items(self): |
| 46 |
self.gid = self.groups[0] |
| 47 |
url = f"{self.root}/gallery/{self.gid}" |
| 48 |
page = self.request(url).text |
| 49 |
data = self.get_job_metadata(page) |
| 50 |
yield Message.Directory, "", data |
| 51 |
for url, image in self.get_images(): |
| 52 |
data.update(image) |
| 53 |
yield Message.Url, url, data |
| 54 |
|
| 55 |
def get_job_metadata(self, page): |
| 56 |
"""Collect metadata for extractor-job""" |
| 57 |
extr = text.extract_from(page) |
| 58 |
|
| 59 |
data = { |
| 60 |
"gallery_id": text.parse_int(self.gid), |
| 61 |
"uploader": extr("porn picture gallery by ", " to see hottest"), |
| 62 |
"title": text.unescape(extr("<title>", "<")), |
| 63 |
"description": text.unescape(extr( |
| 64 |
'id="gdesc_text"', '<').partition(">")[2]), |
| 65 |
"categories": text.split_html(extr( |
| 66 |
'id="cnt_cats"', '</div>'))[1::2], |
| 67 |
"tags": text.split_html(extr( |
| 68 |
'id="cnt_tags"', '</div>'))[1::2], |
| 69 |
"count": text.parse_int(extr(' 1 of ', ' pics"')), |
| 70 |
} |
| 71 |
|
| 72 |
self.image_id = extr('id="img_ed_', '"') |
| 73 |
self._count = data["count"] |
| 74 |
|
| 75 |
return data |
| 76 |
|
| 77 |
def get_images(self): |
| 78 |
"""Collect image-urls and -metadata""" |
| 79 |
url = f"{self.root}/photo/{self.image_id}/" |
| 80 |
params = {"gid": self.gid, "idx": 0, "partial": "true"} |
| 81 |
headers = { |
| 82 |
"Content-Type": "application/x-www-form-urlencoded", |
| 83 |
"X-Requested-With": "XMLHttpRequest", |
| 84 |
"Referer": f"{url}?pgid=&gid={self.image_id}&page=0" |
| 85 |
} |
| 86 |
|
| 87 |
num = 0 |
| 88 |
total = self._count |
| 89 |
while True: |
| 90 |
page = self.request(url, params=params, headers=headers).text |
| 91 |
|
| 92 |
cnt = 0 |
| 93 |
for image_url in text.extract_iter(page, '<a href="', '"'): |
| 94 |
num += 1 |
| 95 |
cnt += 1 |
| 96 |
data = text.nameext_from_url(image_url) |
| 97 |
data["num"] = num |
| 98 |
data["image_id"] = text.parse_int(data["filename"]) |
| 99 |
yield image_url, data |
| 100 |
|
| 101 |
if not cnt or cnt < 24 and num >= total: |
| 102 |
return |
| 103 |
params["idx"] += cnt |
| 104 |
|
| 105 |
|
| 106 |
class ImagefapImageExtractor(ImagefapExtractor): |
| 107 |
"""Extractor for single images from imagefap.com""" |
| 108 |
subcategory = "image" |
| 109 |
pattern = BASE_PATTERN + r"/photo/(\d+)" |
| 110 |
example = "https://www.imagefap.com/photo/12345" |
| 111 |
|
| 112 |
def items(self): |
| 113 |
url, data = self.get_image() |
| 114 |
yield Message.Directory, "", data |
| 115 |
yield Message.Url, url, data |
| 116 |
|
| 117 |
def get_image(self): |
| 118 |
url = f"{self.root}/photo/{self.groups[0]}/" |
| 119 |
page = self.request(url).text |
| 120 |
|
| 121 |
url, pos = text.extract( |
| 122 |
page, 'original="', '"') |
| 123 |
image_id, pos = text.extract( |
| 124 |
page, 'id="imageid_input" value="', '"', pos) |
| 125 |
gallery_id, pos = text.extract( |
| 126 |
page, 'id="galleryid_input" value="', '"', pos) |
| 127 |
info = self._extract_jsonld(page) |
| 128 |
|
| 129 |
return url, text.nameext_from_url(url, { |
| 130 |
"title": text.unescape(info["name"]), |
| 131 |
"uploader": info["author"], |
| 132 |
"date": info["datePublished"], |
| 133 |
"width": text.parse_int(info["width"]), |
| 134 |
"height": text.parse_int(info["height"]), |
| 135 |
"gallery_id": text.parse_int(gallery_id), |
| 136 |
"image_id": text.parse_int(image_id), |
| 137 |
}) |
| 138 |
|
| 139 |
|
| 140 |
class ImagefapFolderExtractor(ImagefapExtractor): |
| 141 |
"""Extractor for imagefap user folders""" |
| 142 |
subcategory = "folder" |
| 143 |
pattern = (BASE_PATTERN + r"/(?:organizer/|" |
| 144 |
r"(?:usergallery\.php\?user(id)?=([^&#]+)&" |
| 145 |
r"|profile/([^/?#]+)/galleries\?)folderid=(?!0\b))(\d+|-1)") |
| 146 |
example = "https://www.imagefap.com/organizer/12345" |
| 147 |
|
| 148 |
def items(self): |
| 149 |
for gallery_id, name, folder in self.galleries(): |
| 150 |
url = f"{self.root}/gallery/{gallery_id}" |
| 151 |
data = { |
| 152 |
"gallery_id": gallery_id, |
| 153 |
"title" : text.unescape(name), |
| 154 |
"folder" : text.unescape(folder), |
| 155 |
"_extractor": ImagefapGalleryExtractor, |
| 156 |
} |
| 157 |
yield Message.Queue, url, data |
| 158 |
|
| 159 |
def galleries(self): |
| 160 |
"""Yield gallery IDs and titles of a folder""" |
| 161 |
_id, user, profile, folder_id = self.groups |
| 162 |
|
| 163 |
if folder_id == "-1": |
| 164 |
folder_name = "Uncategorized" |
| 165 |
if _id: |
| 166 |
url = (f"{self.root}/usergallery.php" |
| 167 |
f"?userid={user}&folderid=-1") |
| 168 |
else: |
| 169 |
url = (f"{self.root}/profile/" |
| 170 |
f"{user or profile}/galleries?folderid=-1") |
| 171 |
else: |
| 172 |
folder_name = None |
| 173 |
url = f"{self.root}/organizer/{folder_id}/" |
| 174 |
|
| 175 |
params = {"page": 0} |
| 176 |
extr = text.extract_from(self.request(url, params=params).text) |
| 177 |
if folder_name is None: |
| 178 |
folder_name = extr("class'blk_galleries'><b>", "</b>") |
| 179 |
|
| 180 |
while True: |
| 181 |
cnt = 0 |
| 182 |
|
| 183 |
while True: |
| 184 |
gid = extr(' id="gid-', '"') |
| 185 |
if not gid: |
| 186 |
break |
| 187 |
yield gid, extr("<b>", "<"), folder_name |
| 188 |
cnt += 1 |
| 189 |
|
| 190 |
if cnt < 20: |
| 191 |
break |
| 192 |
params["page"] += 1 |
| 193 |
extr = text.extract_from(self.request(url, params=params).text) |
| 194 |
|
| 195 |
|
| 196 |
class ImagefapUserExtractor(ImagefapExtractor): |
| 197 |
"""Extractor for an imagefap user profile""" |
| 198 |
subcategory = "user" |
| 199 |
pattern = (BASE_PATTERN + |
| 200 |
r"/(?:profile(?:\.php\?user=|/)([^/?#]+)" |
| 201 |
r"(?:/galleries(?:\?folderid=0)?)?" |
| 202 |
r"|usergallery\.php\?userid=(\d+))(?:$|#)") |
| 203 |
example = "https://www.imagefap.com/profile/USER" |
| 204 |
|
| 205 |
def items(self): |
| 206 |
data = {"_extractor": ImagefapFolderExtractor} |
| 207 |
|
| 208 |
for folder_id in self.folders(): |
| 209 |
if folder_id == "-1": |
| 210 |
url = (f"{self.root}/profile/{self.user}/galleries" |
| 211 |
f"?folderid=-1") |
| 212 |
else: |
| 213 |
url = f"{self.root}/organizer/{folder_id}/" |
| 214 |
yield Message.Queue, url, data |
| 215 |
|
| 216 |
def folders(self): |
| 217 |
"""Return a list of folder IDs of a user""" |
| 218 |
user, user_id = self.groups |
| 219 |
if user: |
| 220 |
url = f"{self.root}/profile/{user}/galleries" |
| 221 |
else: |
| 222 |
url = f"{self.root}/usergallery.php?userid={user_id}" |
| 223 |
params = {"page": 0} |
| 224 |
pnum = 0 |
| 225 |
|
| 226 |
self.user = None |
| 227 |
while True: |
| 228 |
response = self.request(url, params=params) |
| 229 |
|
| 230 |
if self.user is None: |
| 231 |
url = response.url.partition("?")[0] |
| 232 |
self.user = url.rsplit("/", 2)[1] |
| 233 |
|
| 234 |
page = response.text |
| 235 |
folders = text.extr( |
| 236 |
page, ' id="tgl_all" value="', '"').rstrip("|").split("|") |
| 237 |
if folders[-1] == "-1": |
| 238 |
last = folders.pop() |
| 239 |
if not pnum: |
| 240 |
folders.insert(0, last) |
| 241 |
elif not folders[0]: |
| 242 |
break |
| 243 |
yield from folders |
| 244 |
|
| 245 |
params["page"] = pnum = pnum + 1 |
| 246 |
if f'href="?page={pnum}">{pnum+1}</a>' not in page: |
| 247 |
break |