Add new settings fanbox-page-number

This commit is contained in:
2023-07-01 01:55:25 +00:00
committed by GitHub
parent a7073fb8ec
commit 663435789c
6 changed files with 107 additions and 2 deletions

View File

@@ -534,18 +534,40 @@ pub async fn download_file(
/// * `progress_bars` - Multiple progress bars
/// * `datas` - The artwork's data
/// * `base` - The directory of the target
/// * `total_page` - The total count of pages
pub async fn download_fanbox_image(
dh: DownloaderHelper,
np: u16,
progress_bars: Option<Arc<MultiProgress>>,
datas: Arc<FanboxData>,
base: Arc<PathBuf>,
total_page: u16,
) -> Result<(), PixivDownloaderError> {
let mut ndh = dh.clone();
let helper = get_helper();
let file_name = dh
if helper.fanbox_page_number() {
let len = format!("{}", total_page).len();
let basep = match &datas.id {
PixivID::Artwork(a) => format!("{}", a),
PixivID::FanboxCreator(f) => format!("{}", f),
PixivID::FanboxPost(p) => format!("{}", p.post_id),
};
let mut nps = format!("{}", np + 1);
while nps.len() < len {
nps = String::from("0") + &nps;
}
let ofn = ndh
.get_local_file_path(&*base)
.try_err(gettext("Failed to get file name from url."))?;
let ext = ofn
.extension()
.map_or("jpg", |v| v.to_str().unwrap_or("jpg"));
ndh.set_file_name(&format!("{}_{}.{}", basep, nps, ext));
}
let file_name = ndh
.get_local_file_path(&*base)
.try_err(gettext("Failed to get file name from url."))?;
match dh.download_local(helper.overwrite(), &*base)? {
match ndh.download_local(helper.overwrite(), &*base)? {
DownloaderResult::Ok(d) => {
d.handle_options(&helper, progress_bars);
d.download();
@@ -658,6 +680,7 @@ pub async fn download_fanbox_post(
.file_map()
.ok_or(gettext("Failed to get file map from article."))?;
let mut np = 0;
let total_page = image_map.len() as u16;
let mut datas = data.clone();
#[cfg(feature = "exif")]
datas.exif_data.replace(Box::new(Arc::clone(&article)));
@@ -685,6 +708,7 @@ pub async fn download_fanbox_post(
},
Arc::clone(&datas),
Arc::clone(&base),
total_page,
))
.await;
if !download_multiple_files {
@@ -794,6 +818,7 @@ pub async fn download_fanbox_post(
.images()
.try_err(gettext("Failed to get images from the image post."))?;
let mut np = 0;
let total_page = images.len() as u16;
let mut datas = data.clone();
#[cfg(feature = "exif")]
datas.exif_data.replace(Box::new(Arc::clone(&img)));
@@ -813,6 +838,7 @@ pub async fn download_fanbox_post(
},
Arc::clone(&datas),
Arc::clone(&base),
total_page,
))
.await;
if !download_multiple_files {
@@ -927,6 +953,14 @@ pub async fn download_fanbox_creator_info(
let fdata = Arc::new(fdata);
let download_multiple_files = helper.download_multiple_files();
let mut np = 0u16;
let mut total_page = 0;
match data.download_cover_image_url()? {
Some(_) => {
total_page += 1;
}
None => {}
}
total_page += data.profile_items()?.len() as u16;
{
match data.download_cover_image_url()? {
Some(dh) => {
@@ -941,6 +975,7 @@ pub async fn download_fanbox_creator_info(
},
Arc::clone(&fdata),
Arc::clone(&base),
total_page,
))
.await;
if !download_multiple_files {
@@ -968,6 +1003,7 @@ pub async fn download_fanbox_creator_info(
},
Arc::clone(&fdata),
Arc::clone(&base),
total_page,
))
.await;
if !download_multiple_files {

View File

@@ -147,6 +147,29 @@ impl DownloaderHelperBuilder {
}
}
impl Clone for DownloaderHelper {
fn clone(&self) -> Self {
Self {
url: self.url.clone(),
client: self.client.clone(),
headers: match &self.headers {
Some(headers) => match headers.to_headers() {
Some(headers) => Some(Box::new(headers)),
None => None,
},
None => None,
},
file_name: match &self.file_name {
Some(p) => {
let path = p.as_ref().as_ref();
Some(Box::new(path.to_owned()))
}
None => None,
},
}
}
}
impl From<Url> for DownloaderHelper {
fn from(url: Url) -> Self {
Self {

View File

@@ -108,6 +108,10 @@ impl FanboxArticleImageMap {
None
}
}
pub fn len(&self) -> usize {
self.data.len()
}
}
impl CheckUnknown for FanboxArticleImageMap {

View File

@@ -485,6 +485,24 @@ impl OptHelper {
}
X264Profile::default()
}
/// Use page number for pictures' file name in fanbox.
pub fn fanbox_page_number(&self) -> bool {
match self.opt.get_ref().fanbox_page_number {
Some(r) => {
return r;
}
None => {}
}
if self.settings.get_ref().have_bool("fanbox-page-number") {
return self
.settings
.get_ref()
.get_bool("fanbox-page-number")
.unwrap();
}
false
}
}
impl Default for OptHelper {

View File

@@ -119,6 +119,7 @@ pub struct CommandOpts {
pub x264_crf: Option<f32>,
#[cfg(feature = "ugoira")]
pub ugoira_max_fps: Option<f32>,
pub fanbox_page_number: Option<bool>,
}
impl CommandOpts {
@@ -161,6 +162,7 @@ impl CommandOpts {
x264_crf: None,
#[cfg(feature = "ugoira")]
ugoira_max_fps: None,
fanbox_page_number: None,
}
}
@@ -581,6 +583,14 @@ pub fn parse_cmd() -> Option<CommandOpts> {
HasArg::Maybe,
getopts::Occur::Optional,
);
opts.opt(
"",
"fanbox-page-number",
gettext("Use page number for pictures' file name in fanbox."),
"BOOLEAN",
HasArg::Maybe,
getopts::Occur::Optional,
);
let result = match opts.parse(&argv[1..]) {
Ok(m) => m,
Err(err) => {
@@ -929,6 +939,19 @@ pub fn parse_cmd() -> Option<CommandOpts> {
return None;
}
}
match parse_optional_opt(&result, "fanbox-page-number", true, parse_bool) {
Ok(b) => re.as_mut().unwrap().fanbox_page_number = b,
Err(e) => {
println!(
"{} {}",
gettext("Failed to parse <opt>:")
.replace("<opt>", "fanbox-page-number")
.as_str(),
e
);
return None;
}
}
re
}

View File

@@ -61,6 +61,7 @@ pub fn get_settings_list() -> Vec<SettingDes> {
SettingDes::new("x264-crf", gettext("The Constant Rate Factor when converting ugoira(GIF) to video."), JsonValueType::Number, Some(check_crf)).unwrap(),
#[cfg(feature = "ugoira")]
SettingDes::new("ugoira-max-fps", gettext("The max fps when converting ugoira(GIF) to video."), JsonValueType::Number, Some(check_ugoira_max_fps)).unwrap(),
SettingDes::new("fanbox-page-number", gettext("Use page number for pictures' file name in fanbox."), JsonValueType::Boolean, None).unwrap(),
]
}