2025-04-08 15:40:58 -07:00

66 lines
2.1 KiB
JavaScript

import "dotenv/config";
import {Bot, RichText, Post} from "@skyware/bot";
import {DidResolver} from "@atproto/identity";
var didres = new DidResolver({});
var getServiceEndpoint = async did => (await didres.resolve(did)).service.find(s => s.id == "#atproto_pds")?.serviceEndpoint;
var bot = new Bot();
await bot.login({
identifier: process.env.IDENTIFIER,
password: process.env.PASSWORD
});
bot.on("error", console.error);
bot.on("mention", async post => {
try {
// video in mentioning post or quoted post
var vid = await findVideoInPost(post);
// ignore original posts without video
if (!vid && !post.replyRef) return;
// video in replied post or quoted in replied post
if (!vid) {
var parentPost = await bot.getPost(post.replyRef.parent.uri);
vid = await findVideoInPost(parentPost);
}
// video in original post or quoted in original post
if (!vid) {
var rootPost = await bot.getPost(post.replyRef.root.uri);
vid = await findVideoInPost(rootPost);
}
if (!vid) {
await post.reply({text: "Could not find Bluesky video in replied post, quoted post, or parent post"});
return;
}
var url = `${await getServiceEndpoint(vid.did)}/xrpc/com.atproto.sync.getBlob?did=${vid.did}&cid=${vid.cid}`;
url = `https://avps.owo69.me/download.html?filename=${vid.cid}.mp4&url=${encodeURIComponent(url)}`;
var text = new RichText().addLink("Click or touch here to download this video", url);
await post.reply({text});
} catch (error) {
console.error(error);
await post.reply({text: String(error.message?.substring(0,300))});
}
});
async function findVideoInPost(post, b) {
if (!post.embed) return;
if (post.embed.isVideo())
return {did: post.author.did, cid: post.embed.cid};
if (post.embed.isRecordWithMedia() && post.embed.media.isVideo())
return {did: post.author.did, cid: post.embed.media.cid};
if (post.embed.isRecord() && post.embed.record instanceof Post && !b) {
// post.embed.record.embed is undefined
let quotedPost = await bot.getPost(post.embed.record.uri);
return await findVideoInPost(quotedPost, true);
}
}