From 7d211650e8abb38884b69faab6bb8764dff7cb81 Mon Sep 17 00:00:00 2001 From: soultaco83 <76927195+soultaco83@users.noreply.github.com> Date: Sat, 23 Nov 2024 04:10:00 -0500 Subject: [PATCH] added FILENAME_TITLE_TYPE --- readme.md | 10 +++++++++- src/config.rs | 31 ++++++++++++++++++++++++++++++- src/hentai.rs | 32 +++++++++++++++++++++++++++++--- src/main_inner.rs | 4 ++-- 4 files changed, 70 insertions(+), 7 deletions(-) diff --git a/readme.md b/readme.md index d4d23b5..4dc6bc5 100644 --- a/readme.md +++ b/readme.md @@ -57,6 +57,13 @@ I'm happy about anyone who finds my software useful and feedback is also always This is the path to the file containing the nHentai ID you want to download, separated by line breaks. If this file exists, it has priority over tag search and console input. +- `FILENAME_TITLE_TYPE`, optional, defaults to `english` + + Sets which title to use when naming downloaded files. Available options: + - `english`: Use the English title (default) + - `japanese`: Use the Japanese title if available, falls back to default if not present + - `pretty`: Use the Pretty title if available, falls back to default if not present + - `LIBRARY_PATH` This is the directory temporary images and finished CBZ files are download to. By default, it will download to `./hentai/`. @@ -145,6 +152,7 @@ CSRFTOKEN = "your token here" DATABASE_URL = "./db/db.sqlite" DONTDOWNLOADME_FILEPATH = "./config/dontdownloadme.txt" DOWNLOADME_FILEPATH = "./config/downloadme.txt" +FILENAME_TITLE_TYPE = "english" LIBRARY_PATH = "./hentai/" LIBRARY_SPLIT = 10000 NHENTAI_TAGS = ['language:"english"'] @@ -225,4 +233,4 @@ Seems to have been fixed upstream 2024-10-30. (177013, 17249), (177013, 12227), (177013, 33173); - ``` \ No newline at end of file + ``` diff --git a/src/config.rs b/src/config.rs index 8147a17..55e7034 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,32 @@ // Copyright (c) 2024 구FS, all rights reserved. Subject to the MIT licence in `licence.md`. +/// # Summary +/// Available title types for filenames +#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)] +#[serde(rename_all = "snake_case")] +pub enum Title { + /// Use English title + English, + /// Use Japanese title + Japanese, + /// Use Pretty title + Pretty, +} + +impl std::str::FromStr for Title { + type Err = String; + fn from_str(s: &str) -> Result { + match s.to_lowercase().trim() { + "english" => Ok(Self::English), + "japanese" => Ok(Self::Japanese), + "pretty" => Ok(Self::Pretty), + _ => { + log::warn!("Invalid title type \"{s}\", defaulting to English"); + Ok(Self::English) + } + } + } +} /// # Summary /// Collection of settings making up the configuration of the application. @@ -14,6 +41,7 @@ pub struct Config pub DEBUG: Option, // debug mode? pub DONTDOWNLOADME_FILEPATH: Option, // path to file containing hentai ID to not download, blacklist pub DOWNLOADME_FILEPATH: Option, // path to file containing hentai ID to download + pub FILENAME_TITLE_TYPE: Option, // which title to use for filenames: English,Japanese,Pretty pub LIBRARY_PATH: String, // path to download hentai to pub LIBRARY_SPLIT: Option<u32>, // split library into subdirectories of maximum this many hentai, None or 0 to disable pub NHENTAI_TAGS: Option<Vec<String>>, // keep creating downloadme.txt from these tags and keep downloading (server mode), normal tags are in format "tag:{tag}" for example "tag:ffm-threesome"; if None: don't generate downloadme.txt, download hentai once (client mode) @@ -34,6 +62,7 @@ impl Default for Config DEBUG: None, // no entry in default config, defaults to false DONTDOWNLOADME_FILEPATH: Some("./config/dontdownloadme.txt".to_owned()), DOWNLOADME_FILEPATH: Some("./config/downloadme.txt".to_owned()), + FILENAME_TITLE_TYPE: Some(Title::English), // Default to English title LIBRARY_PATH: "./hentai/".to_owned(), LIBRARY_SPLIT: None, NHENTAI_TAGS: None, @@ -41,4 +70,4 @@ impl Default for Config USER_AGENT: Some("".to_owned()), } } -} \ No newline at end of file +} diff --git a/src/hentai.rs b/src/hentai.rs index 05ebec8..e2c23bb 100644 --- a/src/hentai.rs +++ b/src/hentai.rs @@ -49,7 +49,7 @@ impl Hentai /// /// # Returns /// - created hentai or error - pub async fn new(id: u32, db: &sqlx::sqlite::SqlitePool, http_client: &reqwest::Client, nhentai_hentai_search_url: &str, library_path: &str, library_split: u32) -> Result<Self, HentaiNewError> + pub async fn new(id: u32, db: &sqlx::sqlite::SqlitePool, http_client: &reqwest::Client, nhentai_hentai_search_url: &str, library_path: &str, library_split: u32, filename_title_type: &Option<Title>) -> Result<Self, HentaiNewError> { const FILENAME_SIZE_MAX: u16 = 255; // maximum filename size [B] const TITLE_CHARACTERS_FORBIDDEN: &str = "\\/:*?\"<>|\t\n"; // forbidden characters in Windows file names @@ -90,7 +90,32 @@ impl Hentai return Err(HentaiNewError::HentaiLengthInconsistency {page_types: hentai_table_row.page_types.len() as u16, num_pages: hentai_table_row.num_pages}); } - cbz_filename = hentai_table_row.title_english.clone().unwrap_or_default(); + cbz_filename = match filename_title_type { + Some(Title::English) => hentai_table_row.title_english.clone().unwrap_or_default(), //not adding logging as english is default + Some(Title::Japanese) => { + match hentai_table_row.title_japanese.clone() { + Some(title) if !title.is_empty() => title, + _ => { + log::warn!("Japanese title not available for hentai {}, falling back to English title", id); + hentai_table_row.title_japanese.clone().unwrap_or_default() + } + } + }, + Some(Title::Pretty) => { + match hentai_table_row.title_pretty.clone() { + Some(title) if !title.is_empty() => title, + _ => { + log::warn!("Pretty title not available for hentai {}, falling back to English title", id); + hentai_table_row.title_pretty.clone().unwrap_or_default() + } + } + }, + None => { + log::debug!("No title type specified for hentai {}, using English title", id); + hentai_table_row.title_english.clone().unwrap_or_default() + } + }; + cbz_filename.retain(|c| !TITLE_CHARACTERS_FORBIDDEN.contains(c)); // remove forbidden characters if FILENAME_SIZE_MAX - 12 < cbz_filename.len() as u16 // if title size problematic { @@ -394,6 +419,7 @@ pub struct HentaiTableRow pub page_types: String, pub scanlator: Option<String>, pub title_english: Option<String>, + pub title_japanese: Option<String>, pub title_pretty: Option<String>, pub upload_date: chrono::DateTime<chrono::Utc>, -} \ No newline at end of file +} diff --git a/src/main_inner.rs b/src/main_inner.rs index 3c4ded5..2be2421 100644 --- a/src/main_inner.rs +++ b/src/main_inner.rs @@ -104,7 +104,7 @@ pub async fn main_inner(config: Config) -> Result<(), Error> let hentai: Hentai; // hentai to download - match Hentai::new(*hentai_id, &db, &http_client, NHENTAI_HENTAI_SEARCH_URL, &config.LIBRARY_PATH, config.LIBRARY_SPLIT.unwrap_or_default()).await // create hentai, use u32 with 0 to disable library split and not Option<u32> with None, because that would make Some(0) an invalid state + match Hentai::new(*hentai_id, &db, &http_client, NHENTAI_HENTAI_SEARCH_URL, &config.LIBRARY_PATH, config.LIBRARY_SPLIT.unwrap_or_default(), &config.FILENAME_TITLE_TYPE ).await // create hentai, use u32 with 0 to disable library split and not Option<u32> with None, because that would make Some(0) an invalid state { Ok(o) => hentai = o, // hentai created successfully Err(e) => // hentai creation failed @@ -142,4 +142,4 @@ pub async fn main_inner(config: Config) -> Result<(), Error> } return Ok(()); -} \ No newline at end of file +}