From 5888bcbcf86aaf224231a8ddc8156eae54bfb503 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Mon, 8 Sep 2025 22:18:52 +0200 Subject: [PATCH] [fansly] fix & improve format selection (#4401) rename 'format' option to 'formats' --- docs/configuration.rst | 10 ++++------ docs/gallery-dl.conf | 2 +- gallery_dl/extractor/fansly.py | 36 +++++++++++++++++++++------------- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index df0b48be..e14701c4 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -2811,16 +2811,14 @@ Description `fanbox.comments `__ -extractor.fansly.format ------------------------ +extractor.fansly.formats +------------------------ Type ``list`` of ``integers`` Default - ``[303, 302, 1, 2, 4]`` + ``[1, 2, 3, 4, 302, 303]`` Description - Selects the file format to extract. - - When more than one format is given, the first available one is selected. + List of file formats to consider during format selection. extractor.fansly.token diff --git a/docs/gallery-dl.conf b/docs/gallery-dl.conf index f3209429..f44d4429 100644 --- a/docs/gallery-dl.conf +++ b/docs/gallery-dl.conf @@ -332,7 +332,7 @@ { "token": "", - "format": [303, 302, 1, 2, 4] + "formats": [1, 2, 3, 4, 302, 303] }, "flickr": { diff --git a/gallery_dl/extractor/fansly.py b/gallery_dl/extractor/fansly.py index 6f4cc0a7..4e10adff 100644 --- a/gallery_dl/extractor/fansly.py +++ b/gallery_dl/extractor/fansly.py @@ -25,7 +25,11 @@ class FanslyExtractor(Extractor): def _init(self): self.api = FanslyAPI(self) - self.formats = self.config("format") or (303, 302, 1, 2, 4) + + if fmts := self.config("formats"): + self.formats = set(fmts) + else: + self.formats = {1, 2, 3, 4, 302, 303} def items(self): for post in self.posts(): @@ -54,19 +58,23 @@ class FanslyExtractor(Extractor): def _extract_attachment(self, files, post, attachment): media = attachment["media"] - variants = { - variant["type"]: variant - for variant in media.pop("variants", ()) - } - variants[media["type"]] = media - for fmt in self.formats: - if fmt in variants and (variant := variants[fmt]).get("locations"): - break - else: - return self.log.warning( - "%s/%s: Requested format not available", - post["id"], attachment["id"]) + variants = media.pop("variants") or [] + if media.get("locations"): + variants.append(media) + + formats = [ + (type > 256, variant["width"], type, variant) + for variant in variants + if variant.get("locations") and + (type := variant["type"]) in self.formats + ] + + try: + variant = max(formats)[-1] + except Exception: + return self.log.warning("%s/%s: No format available", + post["id"], attachment["id"]) mime = variant["mimetype"] location = variant.pop("locations")[0] @@ -78,7 +86,7 @@ class FanslyExtractor(Extractor): file = { **variant, - "format": fmt, + "format": variant["type"], "date": text.parse_timestamp(media["createdAt"]), "date_updated": text.parse_timestamp(media["updatedAt"]), }