this repo has no description
at main 2.6 kB view raw
1import { AtpAgent } from "@atproto/api"; 2import * as dotenv from "dotenv"; 3 4dotenv.config(); 5 6const IDENTIFIER = process.env.IDENTIFIER || ""; 7const PASSWORD = process.env.PASSWORD || ""; 8const PDS_URL = process.env.PDS_URL || "https://bsky.social"; 9 10const main = async () => { 11 const agent = new AtpAgent({ service: PDS_URL }); 12 13 await agent.login({ identifier: IDENTIFIER, password: PASSWORD }); 14 15 const handle = agent.session?.handle; 16 17 if (handle) { 18 const profile = await agent.app.bsky.actor.getProfile({ 19 actor: handle 20 }); 21 22 if (profile) { 23 let bio: string = profile.data.description; 24 25 if (bio) { 26 const bookhiveResponse = 27 await agent.com.atproto.repo.listRecords({ 28 repo: handle, 29 collection: "buzz.bookhive.book" 30 }); 31 32 if (bookhiveResponse.success) { 33 const currentlyReading = bookhiveResponse.data.records 34 .filter( 35 (record: { value: { status: string } }) => 36 record.value.status === 37 "buzz.bookhive.defs#reading" 38 ) 39 .sort((a, b) => 40 b.value.startedAt.localeCompare(a.value.startedAt) 41 ); 42 const formattedCurrentlyReading = currentlyReading.map( 43 (cr: { value: { title: any; authors: any } }) => 44 `${cr.value.title} by ${cr.value.authors}` 45 ); 46 47 const regex = /(\nCurrently Reading\n📚)([\s\S]*?)(\n📚\n)/; 48 49 let newBio = ""; 50 51 if (regex.test(bio)) { 52 const [_, prefix, __, suffix] = bio.match(regex)!; 53 54 let newMiddle: string[] = []; 55 56 for (const cr of formattedCurrentlyReading) { 57 if ( 58 bio.replace( 59 regex, 60 [ 61 prefix, 62 newMiddle.join(""), 63 `\n${cr}`, 64 suffix 65 ].join("") 66 ).length <= 256 67 ) { 68 newMiddle.push(`\n${cr}`); 69 } 70 } 71 72 newBio = bio.replace( 73 regex, 74 [prefix, newMiddle.join(""), suffix].join("") 75 ); 76 } else { 77 let newMiddle: string[] = []; 78 79 for (const cr of formattedCurrentlyReading) { 80 if ( 81 [ 82 bio, 83 `\n\nCurrently Reading\n📚`, 84 newMiddle.join(""), 85 `\n${cr}`, 86 `\n📚` 87 ].join("").length <= 256 88 ) { 89 newMiddle.push(`\n${cr}`); 90 } 91 } 92 93 newBio = [ 94 bio, 95 `\n\nCurrently Reading\n📚`, 96 newMiddle.join(""), 97 `\n📚` 98 ].join(""); 99 } 100 101 if (newBio.length <= 256 && newBio !== bio) { 102 await agent.upsertProfile((p: any) => { 103 return { ...p, description: newBio }; 104 }); 105 } 106 } 107 } 108 } 109 } 110}; 111 112main();