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)]
#[command(version, about, long_about = None)]
#[command(version, about = "Social media posting bot.", long_about = None)]
pub struct CliArgs {
/// Redis host
#[arg(short, long)]
@ -19,11 +31,16 @@ pub struct CliArgs {
#[arg(short = 'n', long)]
pub redis_consumer_name: String,
/// 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,
/// Platform
#[command(subcommand)]
pub platform: Command,
}
/// Available Subcommands
#[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::cli::CliArgs;
use crate::cli::{CliArgs, Command};
use clap::Parser;
use infrastructure::RedisService;
use log::{error, info, warn};
@ -65,48 +65,57 @@ async fn main() -> Result<(), anyhow::Error> {
warn!("{}", err);
}
let mut bluesky_client =
BlueSkyClient::new(&args.bluesky_handle, &args.bluesky_password).await?;
match args.platform {
Command::Bluesky(bluesky) => {
let mut bluesky_client =
BlueSkyClient::new(&bluesky.bluesky_handle, &bluesky.bluesky_password).await?;
// Read from stream
while running.load(Ordering::SeqCst) {
match redis_service
.read_stream::<NewsPost>(
&args.redis_stream_name,
&args.redis_consumer_group,
&args.redis_consumer_name,
5000,
)
.await
{
Ok(post) => {
let mut data: bluesky::atproto::ATProtoRepoCreateRecord = post.clone().into();
data.repo = args.bluesky_handle.clone();
// Read from stream
while running.load(Ordering::SeqCst) {
match redis_service
.read_stream::<NewsPost>(
&args.redis_stream_name,
&args.redis_consumer_group,
&args.redis_consumer_name,
5000,
)
.await
{
Ok(post) => {
let mut data: bluesky::atproto::ATProtoRepoCreateRecord =
post.clone().into();
data.repo = bluesky.bluesky_handle.clone();
if let Some(image_link) = post.image.clone() {
let result =
add_image_to_post(&mut bluesky_client, &image_link, &mut data).await;
if let Err(err) = result {
warn!("Failed to upload image: {err}")
}
}
let json = serde_json::to_string(&data);
match json {
Ok(json) => {
if let Err(err) = bluesky_client.post(json).await {
error!("failed to post: {post:?} {err}")
} else {
info!("Published a post! 🦀")
if let Some(image_link) = post.image.clone() {
let result =
add_image_to_post(&mut bluesky_client, &image_link, &mut data)
.await;
if let Err(err) = result {
warn!("Failed to upload image: {err}")
}
}
let json = serde_json::to_string(&data);
match json {
Ok(json) => {
if let Err(err) = bluesky_client.post(json).await {
error!("failed to post: {post:?} {err}")
} else {
info!("Published a post! 🦀")
}
}
Err(err) => {
error!("failed to convert post to json: {post:?} {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-stream-name"
- "posts::g4media"
bot:
bluesky-bot:
container_name: bluesky-bot
image: metonymy/bot
environment:
@ -24,6 +24,7 @@ services:
- "bluesky-bot-nas"
- "--redis-consumer-name"
- "bot-1"
- "bluesky"
- "--bluesky-handle"
- "REPLACE-ME"
- "--bluesky-password"