From 1b5462933d5dabb7489cc9084ca83ff50b017d7a Mon Sep 17 00:00:00 2001 From: BishopRed Date: Tue, 14 Jan 2025 22:42:30 -0600 Subject: [PATCH] Adding a type of caching mechanism for the metadata/user information. The current logic would work just fine if looking up for a singular user, however for the multiple posts via normal filtering would cause it to either: 1. Error out due to lack of service and/or creator id. 2. Produce no metadata This builds a local cache during the process so it should only make a call for the user info once during the process. --- gallery_dl/extractor/kemonoparty.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/gallery_dl/extractor/kemonoparty.py b/gallery_dl/extractor/kemonoparty.py index 41c6dd1e97..f87fb86f75 100644 --- a/gallery_dl/extractor/kemonoparty.py +++ b/gallery_dl/extractor/kemonoparty.py @@ -55,8 +55,7 @@ def _init(self): sort_keys=True, separators=(",", ":")).encode def items(self): - service = self.groups[2] - creator_id = self.groups[3] + _creator_info: dict = {} find_hash = re.compile(HASH_PATTERN).match generators = self._build_file_generators(self.config("files")) @@ -64,19 +63,12 @@ def items(self): comments = True if self.config("comments") else False duplicates = True if self.config("duplicates") else False dms = True if self.config("dms") else None - profile = username = None + max_posts = self.config("max-posts") # prevent files from being sent with gzip compression headers = {"Accept-Encoding": "identity"} - if self.config("metadata") and service and creator_id: - #TODO - not sure if we should make this call for every post for a - # /posts all as it may be expensive - profile = self.api.creator_profile(service, creator_id) - username = profile["name"] - posts = self.posts() - max_posts = self.config("max-posts") if max_posts: posts = itertools.islice(posts, max_posts) if self.revisions: @@ -88,10 +80,19 @@ def items(self): post["_http_headers"] = headers post["date"] = self._parse_datetime( post.get("published") or post.get("added") or "") + service = post["service"] + creator_id = post["user"] + + if self.config("metadata"): + if _creator_info.get(f"{service}_{creator_id}") is None: + _creator = self.api.creator_profile(service, creator_id) + _creator_info[f"{service}_{creator_id}"] = _creator + + post["user_profile"] = _creator_info[f"{service}_{creator_id}"] + post["username"] = _creator_info[f"{service}_{creator_id}"]['name'] + else: + post["user_profile"] = post["username"] = post["user"] - if profile is not None: - post["username"] = username - post["user_profile"] = profile if comments: try: post["comments"] = self.api.creator_post_comments(