added title parameter
This commit is contained in:
parent
271e9e4a78
commit
20725047b9
1 changed files with 17 additions and 7 deletions
24
src/main.rs
24
src/main.rs
|
@ -1,5 +1,6 @@
|
|||
use std::fs;
|
||||
use serde::Deserialize;
|
||||
use std::path::Path;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use reqwest::blocking::Client;
|
||||
use std::error::Error;
|
||||
|
||||
|
@ -32,11 +33,13 @@ fn get_jpeg_files(directory: &str) -> Vec<String> {
|
|||
images
|
||||
}
|
||||
|
||||
fn format_post_text(template: &str, batch_num: usize, total_batches: usize) -> String {
|
||||
template.replace("@batch@", &format!("Batch {} out of {}", batch_num, total_batches))
|
||||
fn format_post_text(template: &str, batch_num: usize, total_batches: usize, title: &str) -> String {
|
||||
template
|
||||
.replace("@batch@", &format!("Batch {} out of {}", batch_num, total_batches))
|
||||
.replace("@title@", title)
|
||||
}
|
||||
|
||||
fn upload_images_batch(client: &Client, config: &Config, images: &[String], batch_num: usize, total_batches: usize) -> Result<(), Box<dyn Error>> {
|
||||
fn upload_images_batch(client: &Client, config: &Config, images: &[String], batch_num: usize, total_batches: usize, title: &str) -> Result<(), Box<dyn Error>> {
|
||||
let url = format!("{}/api/v1/media", config.pixelfed_url);
|
||||
let mut media_ids = Vec::new();
|
||||
|
||||
|
@ -57,7 +60,7 @@ fn upload_images_batch(client: &Client, config: &Config, images: &[String], batc
|
|||
|
||||
if !media_ids.is_empty() {
|
||||
let post_url = format!("{}/api/v1/statuses", config.pixelfed_url);
|
||||
let post_text = format_post_text(&config.default_text, batch_num, total_batches);
|
||||
let post_text = format_post_text(&config.default_text, batch_num, total_batches, title);
|
||||
let body = serde_json::json!({
|
||||
"status": post_text,
|
||||
"media_ids": media_ids,
|
||||
|
@ -76,17 +79,24 @@ fn upload_images_batch(client: &Client, config: &Config, images: &[String], batc
|
|||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() < 2 {
|
||||
eprintln!("Usage: {} <directory>", args[0]);
|
||||
eprintln!("Usage: {} <directory> [--title <title>]", args[0]);
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
let mut title = "".to_string();
|
||||
if let Some(index) = args.iter().position(|x| x == "--title") {
|
||||
if index + 1 < args.len() {
|
||||
title = args[index + 1].clone();
|
||||
}
|
||||
}
|
||||
|
||||
let config = load_config()?;
|
||||
let images = get_jpeg_files(&args[1]);
|
||||
let client = Client::new();
|
||||
let total_batches = (images.len() + 19) / 20;
|
||||
|
||||
for (i, chunk) in images.chunks(20).enumerate() {
|
||||
upload_images_batch(&client, &config, chunk, i + 1, total_batches)?;
|
||||
upload_images_batch(&client, &config, chunk, i + 1, total_batches, &title)?;
|
||||
}
|
||||
|
||||
println!("All images uploaded successfully.");
|
||||
|
|
Loading…
Reference in a new issue