bsky-video-download-bot/index.mjs
2024-11-17 12:53:28 -08:00

51 lines
1.4 KiB
JavaScript

import "dotenv/config";
import {Bot, RichText} from "@skyware/bot";
var bot = new Bot();
await bot.login({
identifier: process.env.IDENTIFIER,
password: process.env.PASSWORD
});
bot.on("error", console.error);
bot.on("mention", async mention => {
//console.log({mention});
var parent = mention.replyRef?.parent;
if (!parent) return;
//console.log({parent});
try {
var parentPost = await bot.getPost(parent.uri);
//console.log({parentPost});
var embed = parentPost.embed;
if (!embed || !embed.isVideo()) {
//await mention.reply({text: "Sorry, it doesn't seem that this post has a video."})
return;
}
var did = parentPost.author.did;
var cid = embed.cid;
var url = `${await getServiceEndpoint(did)}/xrpc/com.atproto.sync.getBlob?did=${did}&cid=${cid}`;
url = `https://avps.owo69.me/download.html?filename=${cid}.mp4&url=${encodeURIComponent(url)}`;
var text = new RichText().addLink("Click or touch here to download this video", url);
await mention.reply({text});
} catch (error) {
console.error(error);
await mention.reply({text: "error"});
}
});
async function getServiceEndpoint(did) {
//try {
var doc = await fetch(`https://plc.directory/${did}`).then(res => res.json());
var {serviceEndpoint} = doc.service.find(s => s.id == "#atproto_pds");
return serviceEndpoint;
//} catch (error) {
// console.error(error);
// return `https://bsky.network`;
//}
}