From cff3d37551948b68dc8fff83b0fa5e0996249f5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Sun, 20 Oct 2024 14:56:56 +0200 Subject: [PATCH] [civitai] update "My Reactions" handling (#6263) - use "reactions" subcategory - raise exception when no api-key or cookies are present --- gallery_dl/extractor/civitai.py | 42 +++++++++++++++++++++------------ test/results/civitai.py | 18 ++++++++++---- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/gallery_dl/extractor/civitai.py b/gallery_dl/extractor/civitai.py index 0780dff1..0b1e44af 100644 --- a/gallery_dl/extractor/civitai.py +++ b/gallery_dl/extractor/civitai.py @@ -9,7 +9,7 @@ """Extractors for https://www.civitai.com/""" from .common import Extractor, Message -from .. import text, util +from .. import text, util, exception import itertools import time @@ -355,21 +355,33 @@ class CivitaiUserImagesExtractor(CivitaiExtractor): pattern = USER_PATTERN + r"/images/?(?:\?([^#]+))?" example = "https://civitai.com/user/USER/images" + def __init__(self, match): + self.params = text.parse_query_list(match.group(2)) + if self.params.get("section") == "reactions": + self.subcategory = "reactions" + self.images = self.images_reactions + CivitaiExtractor.__init__(self, match) + def images(self): - params = text.parse_query_list(self.groups[1]) + params = self.params + params["username"] = text.unquote(self.groups[0]) + return self.api.images(params) - if params.get("section") == "reactions": - params["authed"] = True - params["useIndex"] = False - if "reactions" in params: - if isinstance(params["reactions"], str): - params["reactions"] = (params["reactions"],) - else: - params["reactions"] = ( - "Like", "Dislike", "Heart", "Laugh", "Cry") + def images_reactions(self): + if "Authorization" not in self.api.headers and \ + not self.cookies.get( + "__Secure-civitai-token", domain=".civitai.com"): + raise exception.AuthorizationError("api-key or cookies required") + + params = self.params + params["authed"] = True + params["useIndex"] = False + if "reactions" in params: + if isinstance(params["reactions"], str): + params["reactions"] = (params["reactions"],) else: - params["username"] = text.unquote(self.groups[0]) - + params["reactions"] = ( + "Like", "Dislike", "Heart", "Laugh", "Cry") return self.api.images(params) @@ -450,14 +462,14 @@ class CivitaiRestAPI(): class CivitaiTrpcAPI(): - """Interface for the Civitai TRPC API""" + """Interface for the Civitai tRPC API""" def __init__(self, extractor): self.extractor = extractor self.root = extractor.root + "/api/trpc/" self.headers = { "content-type" : "application/json", - "x-client-version": "5.0.146", + "x-client-version": "5.0.185", "x-client-date" : "", "x-client" : "web", "x-fingerprint" : "undefined", diff --git a/test/results/civitai.py b/test/results/civitai.py index c5bb56cf..716d5ecd 100644 --- a/test/results/civitai.py +++ b/test/results/civitai.py @@ -5,6 +5,7 @@ # published by the Free Software Foundation. from gallery_dl.extractor import civitai +from gallery_dl import exception __tests__ = ( @@ -177,14 +178,23 @@ __tests__ = ( }, { - "#url" : "https://civitai.com/user/USER/images?section=reactions", - "#class": civitai.CivitaiUserImagesExtractor, - "#auth" : True, - "#urls" : ( + "#url" : "https://civitai.com/user/USER/images?section=reactions", + "#category": ("", "civitai", "reactions"), + "#class" : civitai.CivitaiUserImagesExtractor, + "#auth" : True, + "#urls" : ( "https://image.civitai.com/xG1nkqKTMzGDvpLrqFT7WA/dd29c97a-1e95-4186-8df5-632736cbae79/original=true/00012-2489035818.png", "https://image.civitai.com/xG1nkqKTMzGDvpLrqFT7WA/5c4efa68-bb58-47c5-a716-98cd0f51f047/original=true/00013-4238863814.png", "https://image.civitai.com/xG1nkqKTMzGDvpLrqFT7WA/69bf3279-df2c-4ec8-b795-479e9cd3db1b/original=true/00014-3150861441.png", ), }, +{ + "#url" : "https://civitai.com/user/USER/images?section=reactions", + "#category": ("", "civitai", "reactions"), + "#class" : civitai.CivitaiUserImagesExtractor, + "#auth" : False, + "#exception": exception.AuthorizationError, +}, + )