Add tags command to add tags to files and update hydrus api

main
trivernis 5 months ago
parent dbc5275f5d
commit 6c73686387
Signed by: Trivernis
GPG Key ID: 7E6D18B61C8D2F4B

4
Cargo.lock generated

@ -988,9 +988,9 @@ checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421"
[[package]] [[package]]
name = "hydrus-api" name = "hydrus-api"
version = "0.9.3" version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3ee4a01380e02f9bf73d14e87021c756bc06d9838662a6bc27c786bba3b26c0" checksum = "4d751951817b71e4d86ffd345fbfa0113fe8786c7696d49b8820d1af37cc9634"
dependencies = [ dependencies = [
"bytes 1.4.0", "bytes 1.4.0",
"chrono", "chrono",

@ -12,7 +12,7 @@ repository = "https://github.com/Trivernis/hydrus-pixiv-tagger"
[dependencies] [dependencies]
pixiv-rs = "0.1.0" 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" rustnao = "0.2.1"
tempdir = "0.3.7" tempdir = "0.3.7"
thiserror = "1.0.38" thiserror = "1.0.38"

@ -30,6 +30,10 @@ pub enum Command {
/// Looks up a list of urls and imports media found for them /// Looks up a list of urls and imports media found for them
#[clap(name = "import-urls")] #[clap(name = "import-urls")]
ImportUrls(ImportUrlsOptions), ImportUrls(ImportUrlsOptions),
/// Tag a file with a given identifier. The identifier is sent via stdin
#[clap(name = "tag")]
Tag(TagOptions),
} }
#[derive(Parser, Debug, Clone)] #[derive(Parser, Debug, Clone)]
@ -58,3 +62,18 @@ pub struct ImportUrlsOptions {
#[clap(short, long)] #[clap(short, long)]
pub urls: Option<Vec<String>>, pub urls: Option<Vec<String>>,
} }
#[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<String>,
/// The tags to assign
#[clap(short, long)]
pub tags: Vec<String>,
}

@ -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 crate::operations::find_and_send_urls::find_and_send_urls;
use args::*; use args::*;
use clap::Parser; 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::service::ServiceName;
use hydrus_api::wrapper::tag::Tag; use hydrus_api::wrapper::tag::Tag;
use hydrus_api::{Client, Hydrus}; use hydrus_api::{Client, Hydrus};
@ -47,6 +49,7 @@ async fn main() {
Command::ImportRedditPosts(opt) => import_reddit_posts(opt, hydrus).await, Command::ImportRedditPosts(opt) => import_reddit_posts(opt, hydrus).await,
Command::ImportFediPosts(opt) => import_fedi_posts(opt, hydrus).await, Command::ImportFediPosts(opt) => import_fedi_posts(opt, hydrus).await,
Command::ImportUrls(opt) => import_urls(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"); .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 sleep_duration = Duration::from_secs(6);
let total_files = files.len(); let total_files = files.len();
let service_key = hydrus.get_service_key(service.into()).await?;
for (i, mut file) in files.into_iter().enumerate() { for (i, mut file) in files.into_iter().enumerate() {
let start = Instant::now(); let start = Instant::now();
@ -101,7 +105,7 @@ async fn send_tags_or_urls(
opt.finish_tag.as_ref(), opt.finish_tag.as_ref(),
&handler, &handler,
&pixiv, &pixiv,
&service, &service_key,
&tmpdir, &tmpdir,
&mut file, &mut file,
) )
@ -179,3 +183,19 @@ async fn get_urls_from_args(opt: ImportUrlsOptions) -> Result<Vec<String>> {
} }
Ok(urls) Ok(urls)
} }
async fn tag_files(opt: TagOptions, hydrus: Hydrus) -> Result<()> {
let tags = opt.tags.into_iter().map(Tag::from).collect::<Vec<_>>();
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(())
}

@ -2,7 +2,7 @@ use crate::{
error::Result, error::Result,
utils::pixiv::{get_pixiv_url, get_sauces_for_file, get_tags_for_sauce}, 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 pixiv_rs::PixivClient;
use rustnao::{Handler, Sauce}; use rustnao::{Handler, Sauce};
use tempdir::TempDir; use tempdir::TempDir;
@ -12,15 +12,16 @@ pub async fn find_and_send_tags(
finish_tag: Option<&String>, finish_tag: Option<&String>,
handler: &Handler, handler: &Handler,
pixiv: &PixivClient, pixiv: &PixivClient,
service: &ServiceName, service_key: &str,
tmpdir: &TempDir, tmpdir: &TempDir,
mut file: &mut HydrusFile, mut file: &mut HydrusFile,
) -> Result<()> { ) -> 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(); let hash = file.hash().await.unwrap();
tracing::error!("Failed to search tags to file {}: {:?}", hash, e); tracing::error!("Failed to search tags to file {}: {:?}", hash, e);
} else if let Some(finish_tag) = finish_tag { } 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 .await
.unwrap(); .unwrap();
} }
@ -32,20 +33,20 @@ pub async fn find_and_send_tags(
async fn search_and_assign_tags( async fn search_and_assign_tags(
handler: &Handler, handler: &Handler,
pixiv: &PixivClient, pixiv: &PixivClient,
service: &ServiceName, service_key: &str,
tmpdir: &TempDir, tmpdir: &TempDir,
mut file: &mut HydrusFile, mut file: &mut HydrusFile,
) -> Result<()> { ) -> Result<()> {
tracing::debug!("Getting tags for hydrus file {:?}", file.id); tracing::debug!("Getting tags for hydrus file {:?}", file.id);
let sauces = get_sauces_for_file(&handler, tmpdir, file).await?; 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)] #[tracing::instrument(level = "debug", skip_all)]
async fn assign_pixiv_tags_and_url( async fn assign_pixiv_tags_and_url(
pixiv: &&PixivClient, pixiv: &&PixivClient,
service: &ServiceName, service_key: &str,
file: &mut &mut HydrusFile, file: &mut &mut HydrusFile,
sauce: &Vec<Sauce>, sauce: &Vec<Sauce>,
) -> Result<()> { ) -> Result<()> {
@ -55,7 +56,7 @@ async fn assign_pixiv_tags_and_url(
if tags.len() > 0 { if tags.len() > 0 {
tracing::info!("Found {} tags for file {:?}", tags.len(), hash); 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 { } else {
tracing::info!("No tags for file {:?} found", hash); tracing::info!("No tags for file {:?} found", hash);
} }

Loading…
Cancel
Save