[bluesky] simplify file extraction code (#6183)
This commit is contained in:
@@ -87,9 +87,11 @@ class BlueskyExtractor(Extractor):
|
|||||||
if not files:
|
if not files:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
base = ("https://bsky.social/xrpc/com.atproto.sync.getBlob"
|
||||||
|
"?did={}&cid=".format(post["author"]["did"]))
|
||||||
for post["num"], file in enumerate(files, 1):
|
for post["num"], file in enumerate(files, 1):
|
||||||
post.update(file)
|
post.update(file)
|
||||||
yield Message.Url, file["url"], post
|
yield Message.Url, base + file["filename"], post
|
||||||
|
|
||||||
def posts(self):
|
def posts(self):
|
||||||
return ()
|
return ()
|
||||||
@@ -99,65 +101,40 @@ class BlueskyExtractor(Extractor):
|
|||||||
return ()
|
return ()
|
||||||
|
|
||||||
files = []
|
files = []
|
||||||
|
|
||||||
media = post["embed"]
|
media = post["embed"]
|
||||||
if "media" in media:
|
if "media" in media:
|
||||||
media = media["media"]
|
media = media["media"]
|
||||||
|
|
||||||
if "images" in 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"]:
|
for image in media["images"]:
|
||||||
try:
|
files.append(self._extract_media(image, "image"))
|
||||||
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,
|
|
||||||
})
|
|
||||||
|
|
||||||
if "video" in media and self.videos:
|
if "video" in media and self.videos:
|
||||||
base = ("https://bsky.social/xrpc/com.atproto.sync.getBlob"
|
files.append(self._extract_media(media, "video"))
|
||||||
"?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,
|
|
||||||
})
|
|
||||||
|
|
||||||
return files
|
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):
|
def _make_post(self, actor, kind):
|
||||||
did = self.api._did_from_actor(actor)
|
did = self.api._did_from_actor(actor)
|
||||||
profile = self.api.get_profile(did)
|
profile = self.api.get_profile(did)
|
||||||
|
|||||||
Reference in New Issue
Block a user