From 4c391805de49c7b7f1b98357c2bd0096a5e88fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Mon, 16 Sep 2024 09:37:18 +0200 Subject: [PATCH] [bluesky] simplify file extraction code (#6183) --- gallery_dl/extractor/bluesky.py | 77 ++++++++++++--------------------- 1 file changed, 27 insertions(+), 50 deletions(-) diff --git a/gallery_dl/extractor/bluesky.py b/gallery_dl/extractor/bluesky.py index 787d5b06..39c56359 100644 --- a/gallery_dl/extractor/bluesky.py +++ b/gallery_dl/extractor/bluesky.py @@ -87,9 +87,11 @@ class BlueskyExtractor(Extractor): if not files: continue + base = ("https://bsky.social/xrpc/com.atproto.sync.getBlob" + "?did={}&cid=".format(post["author"]["did"])) for post["num"], file in enumerate(files, 1): post.update(file) - yield Message.Url, file["url"], post + yield Message.Url, base + file["filename"], post def posts(self): return () @@ -99,65 +101,40 @@ class BlueskyExtractor(Extractor): return () files = [] - media = post["embed"] if "media" in media: media = media["media"] if "images" in media: - base = ("https://bsky.social/xrpc/com.atproto.sync.getBlob" - "?did={}&cid=".format(post["author"]["did"])) - for image in media["images"]: - try: - aspect = image["aspectRatio"] - width = aspect["width"] - height = aspect["height"] - except KeyError: - width = height = 0 - - data = image["image"] - try: - cid = data["ref"]["$link"] - except KeyError: - cid = data["cid"] - - files.append({ - "description": image.get("alt"), - "width" : width, - "height" : height, - "filename" : cid, - "extension" : data["mimeType"].rpartition("/")[2], - "url" : base + cid, - }) - + files.append(self._extract_media(image, "image")) if "video" in media and self.videos: - base = ("https://bsky.social/xrpc/com.atproto.sync.getBlob" - "?did={}&cid=".format(post["author"]["did"])) - try: - aspect = media["aspectRatio"] - width = aspect["width"] - height = aspect["height"] - except KeyError: - width = height = 0 - - video = media["video"] - try: - cid = video["ref"]["$link"] - except KeyError: - cid = video["cid"] - - files.append({ - "description": media.get("alt") or "", - "width" : width, - "height" : height, - "filename" : cid, - "extension" : video["mimeType"].rpartition("/")[2], - "url" : base + cid, - }) + files.append(self._extract_media(media, "video")) return files + def _extract_media(self, media, key): + try: + aspect = media["aspectRatio"] + width = aspect["width"] + height = aspect["height"] + except KeyError: + width = height = 0 + + data = media[key] + try: + cid = data["ref"]["$link"] + except KeyError: + cid = data["cid"] + + return { + "description": media.get("alt") or "", + "width" : width, + "height" : height, + "filename" : cid, + "extension" : data["mimeType"].rpartition("/")[2], + } + def _make_post(self, actor, kind): did = self.api._did_from_actor(actor) profile = self.api.get_profile(did)