From 6c736863873154d40aa860cd16335505bd3fa405 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 21 Dec 2023 11:43:52 +0100 Subject: [PATCH] Add tags command to add tags to files and update hydrus api --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- src/args.rs | 19 +++++++++++++++++++ src/main.rs | 22 +++++++++++++++++++++- src/operations/find_and_send_tags.rs | 17 +++++++++-------- 5 files changed, 52 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d29e44b..ab83632 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -988,9 +988,9 @@ checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" [[package]] name = "hydrus-api" -version = "0.9.3" +version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3ee4a01380e02f9bf73d14e87021c756bc06d9838662a6bc27c786bba3b26c0" +checksum = "4d751951817b71e4d86ffd345fbfa0113fe8786c7696d49b8820d1af37cc9634" dependencies = [ "bytes 1.4.0", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 1029238..0ca1275 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ repository = "https://github.com/Trivernis/hydrus-pixiv-tagger" [dependencies] pixiv-rs = "0.1.0" -hydrus-api = { version = "0.9.3", default-features = false, features = ["json"] } +hydrus-api = { version = "0.10.2", default-features = false, features = ["json"] } rustnao = "0.2.1" tempdir = "0.3.7" thiserror = "1.0.38" diff --git a/src/args.rs b/src/args.rs index 5c37f2e..66f009c 100644 --- a/src/args.rs +++ b/src/args.rs @@ -30,6 +30,10 @@ pub enum Command { /// Looks up a list of urls and imports media found for them #[clap(name = "import-urls")] ImportUrls(ImportUrlsOptions), + + /// Tag a file with a given identifier. The identifier is sent via stdin + #[clap(name = "tag")] + Tag(TagOptions), } #[derive(Parser, Debug, Clone)] @@ -58,3 +62,18 @@ pub struct ImportUrlsOptions { #[clap(short, long)] pub urls: Option>, } + +#[derive(Parser, Debug, Clone)] +pub struct TagOptions { + /// The tag service the tags will be assigned to + #[clap(long, default_value = "my tags")] + pub tag_service: String, + + /// A list of file hashes + #[clap(long)] + pub files: Vec, + + /// The tags to assign + #[clap(short, long)] + pub tags: Vec, +} diff --git a/src/main.rs b/src/main.rs index 9d16925..c55116a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,8 @@ use crate::operations::find_and_send_tags::find_and_send_tags; use crate::operations::find_and_send_urls::find_and_send_urls; use args::*; use clap::Parser; +use hydrus_api::api_core::common::FileIdentifier; +use hydrus_api::api_core::endpoints::adding_tags::TagAction; use hydrus_api::wrapper::service::ServiceName; use hydrus_api::wrapper::tag::Tag; use hydrus_api::{Client, Hydrus}; @@ -47,6 +49,7 @@ async fn main() { Command::ImportRedditPosts(opt) => import_reddit_posts(opt, hydrus).await, Command::ImportFediPosts(opt) => import_fedi_posts(opt, hydrus).await, Command::ImportUrls(opt) => import_urls(opt, hydrus).await, + Command::Tag(opt) => tag_files(opt, hydrus).await, } .expect("Failed to send tags or urls"); } @@ -89,6 +92,7 @@ async fn send_tags_or_urls( let sleep_duration = Duration::from_secs(6); let total_files = files.len(); + let service_key = hydrus.get_service_key(service.into()).await?; for (i, mut file) in files.into_iter().enumerate() { let start = Instant::now(); @@ -101,7 +105,7 @@ async fn send_tags_or_urls( opt.finish_tag.as_ref(), &handler, &pixiv, - &service, + &service_key, &tmpdir, &mut file, ) @@ -179,3 +183,19 @@ async fn get_urls_from_args(opt: ImportUrlsOptions) -> Result> { } Ok(urls) } + +async fn tag_files(opt: TagOptions, hydrus: Hydrus) -> Result<()> { + let tags = opt.tags.into_iter().map(Tag::from).collect::>(); + let service_key = hydrus + .get_service_key(ServiceName(opt.tag_service).into()) + .await?; + + for file in opt.files { + hydrus + .file(FileIdentifier::hash(file)) + .await? + .add_tags(service_key.to_owned(), tags.clone()) + .await?; + } + Ok(()) +} diff --git a/src/operations/find_and_send_tags.rs b/src/operations/find_and_send_tags.rs index e0e11f0..8b47b58 100644 --- a/src/operations/find_and_send_tags.rs +++ b/src/operations/find_and_send_tags.rs @@ -2,7 +2,7 @@ use crate::{ error::Result, utils::pixiv::{get_pixiv_url, get_sauces_for_file, get_tags_for_sauce}, }; -use hydrus_api::wrapper::{hydrus_file::HydrusFile, service::ServiceName}; +use hydrus_api::wrapper::hydrus_file::HydrusFile; use pixiv_rs::PixivClient; use rustnao::{Handler, Sauce}; use tempdir::TempDir; @@ -12,15 +12,16 @@ pub async fn find_and_send_tags( finish_tag: Option<&String>, handler: &Handler, pixiv: &PixivClient, - service: &ServiceName, + service_key: &str, tmpdir: &TempDir, mut file: &mut HydrusFile, ) -> Result<()> { - if let Err(e) = search_and_assign_tags(&handler, &pixiv, &service, &tmpdir, &mut file).await { + if let Err(e) = search_and_assign_tags(&handler, &pixiv, service_key, &tmpdir, &mut file).await + { let hash = file.hash().await.unwrap(); tracing::error!("Failed to search tags to file {}: {:?}", hash, e); } else if let Some(finish_tag) = finish_tag { - file.add_tags(service.clone().into(), vec![finish_tag.into()]) + file.add_tags(service_key.to_owned(), vec![finish_tag.into()]) .await .unwrap(); } @@ -32,20 +33,20 @@ pub async fn find_and_send_tags( async fn search_and_assign_tags( handler: &Handler, pixiv: &PixivClient, - service: &ServiceName, + service_key: &str, tmpdir: &TempDir, mut file: &mut HydrusFile, ) -> Result<()> { tracing::debug!("Getting tags for hydrus file {:?}", file.id); let sauces = get_sauces_for_file(&handler, tmpdir, file).await?; - assign_pixiv_tags_and_url(&pixiv, service, &mut file, &sauces).await + assign_pixiv_tags_and_url(&pixiv, service_key, &mut file, &sauces).await } #[tracing::instrument(level = "debug", skip_all)] async fn assign_pixiv_tags_and_url( pixiv: &&PixivClient, - service: &ServiceName, + service_key: &str, file: &mut &mut HydrusFile, sauce: &Vec, ) -> Result<()> { @@ -55,7 +56,7 @@ async fn assign_pixiv_tags_and_url( if tags.len() > 0 { tracing::info!("Found {} tags for file {:?}", tags.len(), hash); - file.add_tags(service.clone().into(), tags).await?; + file.add_tags(service_key.to_owned(), tags).await?; } else { tracing::info!("No tags for file {:?} found", hash); }