add subcommands for platforms

This commit is contained in:
Denis-Cosmin Nutiu 2025-01-03 23:59:07 +02:00
parent 90a6a03890
commit 05ab839ed6
3 changed files with 72 additions and 45 deletions

View file

@ -1,7 +1,19 @@
use clap::Parser; use clap::{Args, Parser, Subcommand};
/// Bluesky Command Arguments
#[derive(Args, Debug)]
pub struct BlueskyCommand {
/// The Bluesky bot user's handle.
#[arg(short = 'u', long)]
pub bluesky_handle: String,
/// The Bluesky bot user's password.
#[arg(short = 'p', long)]
pub bluesky_password: String,
}
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about = "Social media posting bot.", long_about = None)]
pub struct CliArgs { pub struct CliArgs {
/// Redis host /// Redis host
#[arg(short, long)] #[arg(short, long)]
@ -19,11 +31,16 @@ pub struct CliArgs {
#[arg(short = 'n', long)] #[arg(short = 'n', long)]
pub redis_consumer_name: String, pub redis_consumer_name: String,
/// The bluesky bot user's handle. /// Platform
#[arg(short = 'u', long)] #[command(subcommand)]
pub bluesky_handle: String, pub platform: Command,
}
/// The bluesky bot user's password.
#[arg(short = 'p', long)] /// Available Subcommands
pub bluesky_password: String, #[derive(Subcommand, Debug)]
pub enum Command {
/// Post on bluesky platform.
Bluesky(BlueskyCommand),
/// Post on Mastodon, the FediVerse
Mastodon,
} }

View file

@ -1,5 +1,5 @@
use crate::bluesky::BlueSkyClient; use crate::bluesky::BlueSkyClient;
use crate::cli::CliArgs; use crate::cli::{CliArgs, Command};
use clap::Parser; use clap::Parser;
use infrastructure::RedisService; use infrastructure::RedisService;
use log::{error, info, warn}; use log::{error, info, warn};
@ -65,48 +65,57 @@ async fn main() -> Result<(), anyhow::Error> {
warn!("{}", err); warn!("{}", err);
} }
let mut bluesky_client = match args.platform {
BlueSkyClient::new(&args.bluesky_handle, &args.bluesky_password).await?; Command::Bluesky(bluesky) => {
let mut bluesky_client =
BlueSkyClient::new(&bluesky.bluesky_handle, &bluesky.bluesky_password).await?;
// Read from stream // Read from stream
while running.load(Ordering::SeqCst) { while running.load(Ordering::SeqCst) {
match redis_service match redis_service
.read_stream::<NewsPost>( .read_stream::<NewsPost>(
&args.redis_stream_name, &args.redis_stream_name,
&args.redis_consumer_group, &args.redis_consumer_group,
&args.redis_consumer_name, &args.redis_consumer_name,
5000, 5000,
) )
.await .await
{ {
Ok(post) => { Ok(post) => {
let mut data: bluesky::atproto::ATProtoRepoCreateRecord = post.clone().into(); let mut data: bluesky::atproto::ATProtoRepoCreateRecord =
data.repo = args.bluesky_handle.clone(); post.clone().into();
data.repo = bluesky.bluesky_handle.clone();
if let Some(image_link) = post.image.clone() { if let Some(image_link) = post.image.clone() {
let result = let result =
add_image_to_post(&mut bluesky_client, &image_link, &mut data).await; add_image_to_post(&mut bluesky_client, &image_link, &mut data)
if let Err(err) = result { .await;
warn!("Failed to upload image: {err}") if let Err(err) = result {
} warn!("Failed to upload image: {err}")
} }
let json = serde_json::to_string(&data); }
match json { let json = serde_json::to_string(&data);
Ok(json) => { match json {
if let Err(err) = bluesky_client.post(json).await { Ok(json) => {
error!("failed to post: {post:?} {err}") if let Err(err) = bluesky_client.post(json).await {
} else { error!("failed to post: {post:?} {err}")
info!("Published a post! 🦀") } else {
info!("Published a post! 🦀")
}
}
Err(err) => {
error!("failed to convert post to json: {post:?} {err}")
}
} }
} }
Err(err) => { Err(err) => {
error!("failed to convert post to json: {post:?} {err}") error!("error reading stream: {err}")
} }
} }
} }
Err(err) => { }
error!("error reading stream: {err}") Command::Mastodon => {
} unimplemented!("This command is not currently implemented.")
} }
} }

View file

@ -10,7 +10,7 @@ services:
- "redis://192.168.0.221:6379" - "redis://192.168.0.221:6379"
- "--redis-stream-name" - "--redis-stream-name"
- "posts::g4media" - "posts::g4media"
bot: bluesky-bot:
container_name: bluesky-bot container_name: bluesky-bot
image: metonymy/bot image: metonymy/bot
environment: environment:
@ -24,6 +24,7 @@ services:
- "bluesky-bot-nas" - "bluesky-bot-nas"
- "--redis-consumer-name" - "--redis-consumer-name"
- "bot-1" - "bot-1"
- "bluesky"
- "--bluesky-handle" - "--bluesky-handle"
- "REPLACE-ME" - "REPLACE-ME"
- "--bluesky-password" - "--bluesky-password"