1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 04:02:32 +01:00

[imagechest] simplify

and fix user pagination end condition
This commit is contained in:
Mike Fährmann 2024-11-16 09:17:13 +01:00
parent 917e873c63
commit 75612997fe
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

View File

@ -10,8 +10,7 @@
"""Extractors for https://imgchest.com/""" """Extractors for https://imgchest.com/"""
from .common import GalleryExtractor, Extractor, Message from .common import GalleryExtractor, Extractor, Message
from .. import text, exception from .. import text, util, exception
import json
BASE_PATTERN = r"(?:https?://)?(?:www\.)?imgchest\.com" BASE_PATTERN = r"(?:https?://)?(?:www\.)?imgchest\.com"
@ -34,65 +33,23 @@ class ImagechestGalleryExtractor(GalleryExtractor):
self.api = ImagechestAPI(self, access_token) self.api = ImagechestAPI(self, access_token)
self.gallery_url = None self.gallery_url = None
self.metadata = self._metadata_api self.metadata = self._metadata_api
self.images = self._images_api
def metadata(self, page): def metadata(self, page):
if "Not Found" in page: 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") raise exception.NotFoundError("gallery")
self.files = ()
return {}
page_data = self._retrieve_page_data(page) self.files = post.pop("files", ())
post["gallery_id"] = self.gallery_id
post["tags"] = [tag["name"] for tag in post["tags"]]
metadata = { return post
"gallery_id": self.gallery_id
}
for attribute in [
"id",
"slug",
"status",
"title",
"nsfw",
"score",
"comments",
"upvotes",
"downvotes",
"favorites",
"views",
"created"
]:
try:
metadata[attribute] = page_data["props"]["post"][attribute]
except Exception:
pass
try:
metadata["tags"] = ",".join(page_data["props"]["post"]["tags"])
except Exception:
pass
return metadata
def images(self, page):
page_data = self._retrieve_page_data(page)
try:
return [
(file["link"], None)
for file in page_data["props"]["post"]["files"]
]
except Exception:
return []
def _retrieve_page_data(self, page):
return json.loads(
text.unescape(
text.extr(
page,
begin='data-page="',
end='"',
default='{}')
)
)
def _metadata_api(self, page): def _metadata_api(self, page):
post = self.api.post(self.gallery_id) post = self.api.post(self.gallery_id)
@ -105,15 +62,18 @@ class ImagechestGalleryExtractor(GalleryExtractor):
post["gallery_id"] = self.gallery_id post["gallery_id"] = self.gallery_id
post.pop("image_count", None) post.pop("image_count", None)
self._image_list = post.pop("images") self.files = post.pop("images")
return post return post
def _images_api(self, page): def images(self, page):
try:
return [ return [
(img["link"], img) (file["link"], file)
for img in self._image_list for file in self.files
] ]
except Exception:
return ()
class ImagechestUserExtractor(Extractor): class ImagechestUserExtractor(Extractor):
@ -124,10 +84,6 @@ class ImagechestUserExtractor(Extractor):
pattern = BASE_PATTERN + r"/u/([^/?#]+)" pattern = BASE_PATTERN + r"/u/([^/?#]+)"
example = "https://imgchest.com/u/USER" example = "https://imgchest.com/u/USER"
def __init__(self, match):
Extractor.__init__(self, match)
self.user = match.group(1)
def items(self): def items(self):
url = self.root + "/api/posts" url = self.root + "/api/posts"
params = { params = {
@ -135,7 +91,7 @@ class ImagechestUserExtractor(Extractor):
"sort" : "new", "sort" : "new",
"tag" : "", "tag" : "",
"q" : "", "q" : "",
"username": text.unquote(self.user), "username": text.unquote(self.groups[0]),
"nsfw" : "true", "nsfw" : "true",
} }
@ -145,6 +101,9 @@ class ImagechestUserExtractor(Extractor):
except (TypeError, KeyError): except (TypeError, KeyError):
return return
if not data:
return
for gallery in data: for gallery in data:
gallery["_extractor"] = ImagechestGalleryExtractor gallery["_extractor"] = ImagechestGalleryExtractor
yield Message.Queue, gallery["link"], gallery yield Message.Queue, gallery["link"], gallery