import { AtpAgent } from "@atproto/api"; import * as dotenv from "dotenv"; dotenv.config(); const IDENTIFIER = process.env.IDENTIFIER || ""; const PASSWORD = process.env.PASSWORD || ""; const PDS_URL = process.env.PDS_URL || "https://bsky.social"; const main = async () => { const agent = new AtpAgent({ service: PDS_URL }); await agent.login({ identifier: IDENTIFIER, password: PASSWORD }); const handle = agent.session?.handle; if (handle) { const profile = await agent.app.bsky.actor.getProfile({ actor: handle }); if (profile) { let bio: string = profile.data.description; if (bio) { const bookhiveResponse = await agent.com.atproto.repo.listRecords({ repo: handle, collection: "buzz.bookhive.book" }); if (bookhiveResponse.success) { const currentlyReading = bookhiveResponse.data.records .filter( (record: { value: { status: string } }) => record.value.status === "buzz.bookhive.defs#reading" ) .sort((a, b) => b.value.startedAt.localeCompare(a.value.startedAt) ); const formattedCurrentlyReading = currentlyReading.map( (cr: { value: { title: any; authors: any } }) => `${cr.value.title} by ${cr.value.authors}` ); const regex = /(\nCurrently Reading\nšŸ“š)([\s\S]*?)(\nšŸ“š\n)/; let newBio = ""; if (regex.test(bio)) { const [_, prefix, __, suffix] = bio.match(regex)!; let newMiddle: string[] = []; for (const cr of formattedCurrentlyReading) { if ( bio.replace( regex, [ prefix, newMiddle.join(""), `\n${cr}`, suffix ].join("") ).length <= 256 ) { newMiddle.push(`\n${cr}`); } } newBio = bio.replace( regex, [prefix, newMiddle.join(""), suffix].join("") ); } else { let newMiddle: string[] = []; for (const cr of formattedCurrentlyReading) { if ( [ bio, `\n\nCurrently Reading\nšŸ“š`, newMiddle.join(""), `\n${cr}`, `\nšŸ“š` ].join("").length <= 256 ) { newMiddle.push(`\n${cr}`); } } newBio = [ bio, `\n\nCurrently Reading\nšŸ“š`, newMiddle.join(""), `\nšŸ“š` ].join(""); } if (newBio.length <= 256 && newBio !== bio) { await agent.upsertProfile((p: any) => { return { ...p, description: newBio }; }); } } } } } }; main();