[fansly] fix & improve format selection (#4401)

rename 'format' option to 'formats'
This commit is contained in:
Mike Fährmann
2025-09-08 22:18:52 +02:00
parent f6fcba4040
commit 5888bcbcf8
3 changed files with 27 additions and 21 deletions

View File

@@ -2811,16 +2811,14 @@ Description
`fanbox.comments <extractor.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

View File

@@ -332,7 +332,7 @@
{
"token": "",
"format": [303, 302, 1, 2, 4]
"formats": [1, 2, 3, 4, 302, 303]
},
"flickr":
{

View File

@@ -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"]),
}