Skip to content

Commit

Permalink
added FILENAME_TITLE_TYPE
Browse files Browse the repository at this point in the history
  • Loading branch information
soultaco83 authored and 9-FS committed Nov 23, 2024
1 parent 942fbee commit 7d21165
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
10 changes: 9 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/`.
Expand Down Expand Up @@ -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"']
Expand Down Expand Up @@ -225,4 +233,4 @@ Seems to have been fixed upstream 2024-10-30.
(177013, 17249),
(177013, 12227),
(177013, 33173);
```
```
31 changes: 30 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -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<Self, Self::Err> {
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.
Expand All @@ -14,6 +41,7 @@ pub struct Config
pub DEBUG: Option<bool>, // debug mode?
pub DONTDOWNLOADME_FILEPATH: Option<String>, // path to file containing hentai ID to not download, blacklist
pub DOWNLOADME_FILEPATH: Option<String>, // path to file containing hentai ID to download
pub FILENAME_TITLE_TYPE: Option<Title>, // 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)
Expand All @@ -34,11 +62,12 @@ 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,
SLEEP_INTERVAL: Some(50000),
USER_AGENT: Some("".to_owned()),
}
}
}
}
32 changes: 29 additions & 3 deletions src/hentai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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>,
}
}
4 changes: 2 additions & 2 deletions src/main_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -142,4 +142,4 @@ pub async fn main_inner(config: Config) -> Result<(), Error>
}

return Ok(());
}
}

0 comments on commit 7d21165

Please sign in to comment.