api/bluesky: add support for saving images

one or multiple, everything works
This commit is contained in:
wukko 2024-09-01 16:37:24 +06:00
parent d9925f2233
commit 93f2a6b226
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
4 changed files with 60 additions and 2 deletions

View file

@ -78,6 +78,7 @@ export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disab
case "instagram": case "instagram":
case "twitter": case "twitter":
case "snapchat": case "snapchat":
case "bsky":
params = { picker: r.picker }; params = { picker: r.picker };
break; break;

View file

@ -239,7 +239,8 @@ export default async function(host, patternMatch, obj) {
case "bsky": case "bsky":
r = await bluesky({ r = await bluesky({
...patternMatch ...patternMatch,
alwaysProxy: obj.alwaysProxy
}); });
break; break;

View file

@ -1,5 +1,6 @@
import HLS from "hls-parser"; import HLS from "hls-parser";
import { cobaltUserAgent } from "../../config.js"; import { cobaltUserAgent } from "../../config.js";
import { createStream } from "../../stream/manage.js";
const extractVideo = async ({ getPost, filename }) => { const extractVideo = async ({ getPost, filename }) => {
const urlMasterHLS = getPost?.thread?.post?.embed?.playlist; const urlMasterHLS = getPost?.thread?.post?.embed?.playlist;
@ -24,7 +25,41 @@ const extractVideo = async ({ getPost, filename }) => {
} }
} }
export default async function ({ user, post }) { const extractImages = async ({ getPost, filename, alwaysProxy }) => {
const images = getPost?.thread?.post?.embed?.images;
if (!images || images.length === 0) {
return { error: "fetch.empty" };
}
if (images.length === 1) return {
urls: images[0].fullsize,
isPhoto: true,
filename: `${filename}.jpg`,
}
const picker = images.map((image, i) => {
let url = image.fullsize;
let proxiedImage = createStream({
service: "bluesky",
type: "proxy",
u: url,
filename: `${filename}_${i + 1}.jpg`,
});
if (alwaysProxy) url = proxiedImage;
return {
type: "photo",
url,
thumb: proxiedImage,
}
});
return { picker };
}
export default async function ({ user, post, alwaysProxy }) {
const apiEndpoint = new URL("https://public.api.bsky.app/xrpc/app.bsky.feed.getPostThread?depth=0&parentHeight=0"); const apiEndpoint = new URL("https://public.api.bsky.app/xrpc/app.bsky.feed.getPostThread?depth=0&parentHeight=0");
apiEndpoint.searchParams.set( apiEndpoint.searchParams.set(
"uri", "uri",
@ -47,6 +82,9 @@ export default async function ({ user, post }) {
if (embedType === "app.bsky.embed.video#view") { if (embedType === "app.bsky.embed.video#view") {
return await extractVideo({ getPost, filename }); return await extractVideo({ getPost, filename });
} }
if (embedType === "app.bsky.embed.images#view") {
return await extractImages({ getPost, filename, alwaysProxy });
}
return { error: "fetch.empty" }; return { error: "fetch.empty" };
} }

View file

@ -1438,6 +1438,24 @@
"code": 200, "code": 200,
"status": "stream" "status": "stream"
} }
},
{
"name": "single image",
"url": "https://bsky.app/profile/thehardyboycats.bsky.social/post/3l33flpoygt26",
"params": {},
"expected": {
"code": 200,
"status": "redirect"
}
},
{
"name": "several images",
"url": "https://bsky.app/profile/tracey-m.bsky.social/post/3kzxuxbiul626",
"params": {},
"expected": {
"code": 200,
"status": "picker"
}
} }
] ]
} }