Add clap v3

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 2 years ago
parent 0ac7df73e8
commit 1407b09cc3
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

558
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -1,7 +1,7 @@
[package] [package]
name = "hydrus-pixiv-tagger" name = "hydrus-utils"
version = "0.3.0" version = "0.3.0"
edition = "2018" edition = "2021"
license = "Apache-2.0" license = "Apache-2.0"
description = "Automatically tag hydrus file by using pixiv and saucenao" description = "Automatically tag hydrus file by using pixiv and saucenao"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
@ -14,12 +14,12 @@ repository = "https://github.com/Trivernis/hydrus-pixiv-tagger"
pixiv-rs = "0.1.0" pixiv-rs = "0.1.0"
hydrus-api = "0.9.3" hydrus-api = "0.9.3"
rustnao = "0.2.1" rustnao = "0.2.1"
structopt = "0.3.26"
tempdir = "0.3.7" tempdir = "0.3.7"
env_logger = "0.9.0" env_logger = "0.9.0"
log = "0.4.16" log = "0.4.17"
thiserror = "1.0.30" thiserror = "1.0.32"
clap = { version = "3.2.17", features = ["derive", "env"] }
[dependencies.tokio] [dependencies.tokio]
version = "1.17.0" version = "1.20.1"
features = ["macros", "rt", "time"] features = ["macros", "rt", "time"]

@ -3,60 +3,67 @@ pub mod search;
use crate::error::Result; use crate::error::Result;
use crate::search::get_urls; use crate::search::get_urls;
use clap::{Parser, Subcommand};
use hydrus_api::wrapper::hydrus_file::HydrusFile; use hydrus_api::wrapper::hydrus_file::HydrusFile;
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};
use pixiv_rs::PixivClient; use pixiv_rs::PixivClient;
use rustnao::{Handler, HandlerBuilder, Sauce}; use rustnao::{Handler, HandlerBuilder, Sauce};
use structopt::clap::AppSettings;
use structopt::StructOpt;
use tempdir::TempDir; use tempdir::TempDir;
use tokio::time::{Duration, Instant}; use tokio::time::{Duration, Instant};
#[derive(StructOpt, Debug)] #[derive(Parser, Debug)]
#[structopt()] #[clap(author, version, about)]
struct Args {
#[clap(subcommand)]
subcommand: Command,
/// The hydrus client api key
#[clap(long, env)]
hydrus_key: String,
/// The url to the hydrus client api
#[clap(long, default_value = "http://127.0.0.1:45869", env)]
hydrus_url: String,
}
#[derive(Subcommand, Clone, Debug)]
enum Command { enum Command {
#[clap(name = "send-url")]
/// Sends urls to hydrus to be imported /// Sends urls to hydrus to be imported
SendUrl(Options), SendUrl(Options),
#[clap(name = "send-tags")]
/// Maps the tags found for the hydrus url to the hydrus file /// Maps the tags found for the hydrus url to the hydrus file
SendTags(Options), SendTags(Options),
} }
#[derive(StructOpt, Debug, Clone)] #[derive(Parser, Debug, Clone)]
#[structopt(settings = &[AppSettings::AllowLeadingHyphen])]
struct Options { struct Options {
/// The saucenao api key /// The saucenao api key
#[structopt(long, env)] #[clap(long, env)]
saucenao_key: String, saucenao_key: String,
/// The hydrus client api key
#[structopt(long, env)]
hydrus_key: String,
/// The url to the hydrus client api
#[structopt(long, default_value = "http://127.0.0.1:45869", env)]
hydrus_url: String,
/// The tag service the tags will be assigned to /// The tag service the tags will be assigned to
#[structopt(long, default_value = "my tags")] #[clap(long, default_value = "my tags")]
tag_service: String, tag_service: String,
/// Tag that is assigned to files that have been processed /// Tag that is assigned to files that have been processed
#[structopt(long)] #[clap(long)]
finish_tag: Option<String>, finish_tag: Option<String>,
/// Tags used to search for files /// Tags used to search for files
#[structopt(short, long)] #[clap(short, long)]
tags: Vec<String>, tags: Vec<String>,
} }
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() { async fn main() {
env_logger::builder().init(); env_logger::builder().init();
let cmd: Command = Command::from_args(); let args: Args = Args::parse();
log::debug!("args: {cmd:?}"); log::debug!("args: {args:?}");
let opt = match &cmd { let opt = match &args.subcommand {
Command::SendUrl(opt) => opt.clone(), Command::SendUrl(opt) => opt.clone(),
Command::SendTags(opt) => opt.clone(), Command::SendTags(opt) => opt.clone(),
}; };
@ -67,7 +74,7 @@ async fn main() {
.db(Handler::PIXIV) .db(Handler::PIXIV)
.build(); .build();
let hydrus = Hydrus::new(Client::new(opt.hydrus_url, opt.hydrus_key)); let hydrus = Hydrus::new(Client::new(&args.hydrus_url, &args.hydrus_key));
let pixiv = PixivClient::new(); let pixiv = PixivClient::new();
let tags = opt.tags.into_iter().map(Tag::from).collect(); let tags = opt.tags.into_iter().map(Tag::from).collect();
@ -81,7 +88,7 @@ async fn main() {
for mut file in files { for mut file in files {
let start = Instant::now(); let start = Instant::now();
match &cmd { match &args.subcommand {
Command::SendUrl(_) => { Command::SendUrl(_) => {
let _ = search_and_send_urls(&hydrus, &handler, &tmpdir, &mut file).await; let _ = search_and_send_urls(&hydrus, &handler, &tmpdir, &mut file).await;
} }

Loading…
Cancel
Save