Skip to content

Commit

Permalink
merge #6475: [imagechest] fix extractors
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Nov 16, 2024
2 parents f7246f0 + 7561299 commit f041957
Showing 1 changed file with 28 additions and 38 deletions.
66 changes: 28 additions & 38 deletions gallery_dl/extractor/imagechest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""Extractors for https://imgchest.com/"""

from .common import GalleryExtractor, Extractor, Message
from .. import text, exception
from .. import text, util, exception

BASE_PATTERN = r"(?:https?://)?(?:www\.)?imgchest\.com"

Expand All @@ -33,35 +33,23 @@ def _init(self):
self.api = ImagechestAPI(self, access_token)
self.gallery_url = None
self.metadata = self._metadata_api
self.images = self._images_api

def metadata(self, page):
if "Sorry, but the page you requested could not be found." in page:
raise exception.NotFoundError("gallery")

return {
"gallery_id": self.gallery_id,
"title": text.unescape(text.extr(
page, 'property="og:title" content="', '"').strip())
}
try:
data = util.json_loads(text.unescape(text.extr(
page, 'data-page="', '"')))
post = data["props"]["post"]
except Exception:
if "<title>Not Found</title>" in page:
raise exception.NotFoundError("gallery")
self.files = ()
return {}

self.files = post.pop("files", ())
post["gallery_id"] = self.gallery_id
post["tags"] = [tag["name"] for tag in post["tags"]]

def images(self, page):
if ' load-all">' in page:
url = "{}/p/{}/loadAll".format(self.root, self.gallery_id)
headers = {
"X-Requested-With": "XMLHttpRequest",
"Origin" : self.root,
"Referer" : self.gallery_url,
}
csrf_token = text.extr(page, 'name="csrf-token" content="', '"')
data = {"_token": csrf_token}
page += self.request(
url, method="POST", headers=headers, data=data).text

return [
(url, None)
for url in text.extract_iter(page, 'data-url="', '"')
]
return post

def _metadata_api(self, page):
post = self.api.post(self.gallery_id)
Expand All @@ -74,15 +62,18 @@ def _metadata_api(self, page):

post["gallery_id"] = self.gallery_id
post.pop("image_count", None)
self._image_list = post.pop("images")
self.files = post.pop("images")

return post

def _images_api(self, page):
return [
(img["link"], img)
for img in self._image_list
]
def images(self, page):
try:
return [
(file["link"], file)
for file in self.files
]
except Exception:
return ()


class ImagechestUserExtractor(Extractor):
Expand All @@ -93,18 +84,14 @@ class ImagechestUserExtractor(Extractor):
pattern = BASE_PATTERN + r"/u/([^/?#]+)"
example = "https://imgchest.com/u/USER"

def __init__(self, match):
Extractor.__init__(self, match)
self.user = match.group(1)

def items(self):
url = self.root + "/api/posts"
params = {
"page" : 1,
"sort" : "new",
"tag" : "",
"q" : "",
"username": text.unquote(self.user),
"username": text.unquote(self.groups[0]),
"nsfw" : "true",
}

Expand All @@ -114,6 +101,9 @@ def items(self):
except (TypeError, KeyError):
return

if not data:
return

for gallery in data:
gallery["_extractor"] = ImagechestGalleryExtractor
yield Message.Queue, gallery["link"], gallery
Expand Down

0 comments on commit f041957

Please sign in to comment.