Add bluesky support

This commit is contained in:
2024-11-21 21:19:47 +00:00
parent 3d08a04b06
commit 31a9fa64e2
4 changed files with 506 additions and 62 deletions
+70
View File
@@ -0,0 +1,70 @@
use std::{
borrow::Cow,
io::{Cursor, Seek, SeekFrom},
};
use bsky_sdk::{
api::{
app::bsky::{
embed::images::{ImageData, MainData},
feed::post::{RecordData, RecordEmbedRefs},
},
types::string::Datetime,
},
BskyAgent,
};
const BSKY_IDENT_ENV_VAR: &str = "BSKY_IDENTIFIER";
const BSKY_PASSWORD_ENV_VAR: &str = "BSKY_PASSWORD";
fn getenv(name: &str) -> String {
std::env::var(name).expect(&format!("Please specify {} in the env", name))
}
pub(crate) async fn post(
text: &str,
img: Option<Box<Cursor<Vec<u8>>>>,
) -> Result<(), Box<dyn std::error::Error>> {
let agent = BskyAgent::builder().build().await?;
agent
.login(getenv(BSKY_IDENT_ENV_VAR), getenv(BSKY_PASSWORD_ENV_VAR))
.await?;
let embed = if let Some(mut i) = img {
i.as_mut().seek(SeekFrom::Start(0))?;
let out = agent
.api
.com
.atproto
.repo
.upload_blob(i.into_inner())
.await?;
let embed = RecordEmbedRefs::AppBskyEmbedImagesMain(Box::new(
MainData {
images: vec![ImageData {
alt: "".into(),
aspect_ratio: None,
image: out.data.blob,
}
.into()],
}
.into(),
));
Some(bsky_sdk::api::types::Union::Refs(embed))
} else {
None
};
agent
.create_record(RecordData {
created_at: Datetime::now(),
embed,
entities: None,
facets: None,
labels: None,
langs: None,
reply: None,
tags: None,
text: text.into(),
})
.await?;
Ok(())
}
+4 -1
View File
@@ -4,6 +4,7 @@ use std::convert::TryInto;
use std::fs::File;
use std::io::{BufWriter, Cursor, Write};
use tracing::{event, Level};
mod bsky;
mod mastodon;
mod svg;
mod twitter;
@@ -230,7 +231,9 @@ async fn run_bot(dry_run: bool, target: Option<String>) -> StdError<()> {
let img = svg::render_svg(&svg, 1000, false)?;
let mut buf = Cursor::new(Vec::new());
img.write_to(&mut buf, image::ImageFormat::Png)?;
toot(&text, Some(Box::new(buf))).await?;
let the_img = Some(Box::new(buf));
toot(&text, the_img.clone()).await?;
bsky::post(&text, the_img).await?;
} else {
// Render the image smaller for output to terminal
let img = svg::render_svg(&svg, 128, true)?;