From 2ef99a20c9398078e0ca7c2c1c39279c870e3ecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=80=E1=B4=8D=E1=B4=9B=E1=B4=8F=E1=B4=80=E1=B4=87?= =?UTF-8?q?=CA=80?= Date: Fri, 31 May 2024 12:01:39 -0700 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=20NFO=20=E6=96=87=E4=BB=B6=E4=B8=AD=E7=9A=84=E8=A7=86?= =?UTF-8?q?=E9=A2=91=E6=97=B6=E9=97=B4=EF=BC=8C=E5=8F=AF=E9=80=89=E5=8A=A0?= =?UTF-8?q?=E5=85=A5=E6=94=B6=E8=97=8F=E5=A4=B9=E7=9A=84=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E3=80=81=E8=A7=86=E9=A2=91=E5=8F=91=E5=B8=83=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E9=97=B4=20(#114)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 支持自定义 NFO 文件中的视频时间,可选加入收藏夹的时间、视频发布的时间 * chore: 使用小写 --- src/config.rs | 21 ++++++++++++++------- src/core/command.rs | 6 +++++- src/core/utils.rs | 38 +++++++++++++++++++++++--------------- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/config.rs b/src/config.rs index ca43fe2..575d3d8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -19,7 +19,7 @@ pub static CONFIG: Lazy = Lazy::new(|| { panic!("加载配置文件失败,错误为: {err}"); } warn!("配置文件不存在,使用默认配置..."); - Config::new() + Config::default() }); // 放到外面,确保新的配置项被保存 info!("配置加载完毕,覆盖刷新原有配置"); @@ -44,16 +44,20 @@ pub struct Config { pub page_name: Cow<'static, str>, pub interval: u64, pub upper_path: PathBuf, + #[serde(default)] + pub nfo_time_type: NFOTimeType, } -impl Default for Config { - fn default() -> Self { - Self::new() - } +#[derive(Serialize, Deserialize, Default)] +#[serde(rename_all = "lowercase")] +pub enum NFOTimeType { + #[default] + FavTime, + PubTime, } -impl Config { - fn new() -> Self { +impl Default for Config { + fn default() -> Self { Self { credential: ArcSwapOption::from(Some(Arc::new(Credential::default()))), filter_option: FilterOption::default(), @@ -63,9 +67,12 @@ impl Config { page_name: Cow::Borrowed("{{bvid}}"), interval: 1200, upper_path: CONFIG_DIR.join("upper_face"), + nfo_time_type: NFOTimeType::FavTime, } } +} +impl Config { /// 简单的预检查 pub fn check(&self) { let mut ok = true; diff --git a/src/core/command.rs b/src/core/command.rs index 428e68c..25edfba 100644 --- a/src/core/command.rs +++ b/src/core/command.rs @@ -657,7 +657,11 @@ async fn generate_nfo(serializer: NFOSerializer<'_>, nfo_path: PathBuf) -> Resul if let Some(parent) = nfo_path.parent() { fs::create_dir_all(parent).await?; } - fs::write(nfo_path, serializer.generate_nfo().await?.as_bytes()).await?; + fs::write( + nfo_path, + serializer.generate_nfo(&CONFIG.nfo_time_type).await?.as_bytes(), + ) + .await?; Ok(()) } diff --git a/src/core/utils.rs b/src/core/utils.rs index 03762c5..9ebe5f9 100644 --- a/src/core/utils.rs +++ b/src/core/utils.rs @@ -17,7 +17,7 @@ use serde_json::json; use tokio::io::AsyncWriteExt; use crate::bilibili::{FavoriteListInfo, PageInfo, VideoInfo}; -use crate::config::CONFIG; +use crate::config::{NFOTimeType, CONFIG}; use crate::core::status::Status; pub static TEMPLATE: Lazy = Lazy::new(|| { @@ -274,7 +274,7 @@ pub async fn update_pages_model(pages: Vec, connection: &Data /// serde xml 似乎不太好用,先这么裸着写 /// (真是又臭又长啊 impl<'a> NFOSerializer<'a> { - pub async fn generate_nfo(self) -> Result { + pub async fn generate_nfo(self, nfo_time_type: &NFOTimeType) -> Result { let mut buffer = r#" "# .as_bytes() @@ -283,6 +283,10 @@ impl<'a> NFOSerializer<'a> { let mut writer = Writer::new_with_indent(&mut tokio_buffer, b' ', 4); match self { NFOSerializer(ModelWrapper::Video(v), NFOMode::MOVIE) => { + let nfo_time = match nfo_time_type { + NFOTimeType::FavTime => v.favtime, + NFOTimeType::PubTime => v.pubtime, + }; writer .create_element("movie") .write_inner_content_async::<_, _, Error>(|writer| async move { @@ -316,7 +320,7 @@ impl<'a> NFOSerializer<'a> { .unwrap(); writer .create_element("year") - .write_text_content_async(BytesText::new(&v.favtime.format("%Y").to_string())) + .write_text_content_async(BytesText::new(&nfo_time.format("%Y").to_string())) .await .unwrap(); if let Some(tags) = &v.tags { @@ -337,7 +341,7 @@ impl<'a> NFOSerializer<'a> { .unwrap(); writer .create_element("aired") - .write_text_content_async(BytesText::new(&v.favtime.format("%Y-%m-%d").to_string())) + .write_text_content_async(BytesText::new(&nfo_time.format("%Y-%m-%d").to_string())) .await .unwrap(); Ok(writer) @@ -346,6 +350,10 @@ impl<'a> NFOSerializer<'a> { .unwrap(); } NFOSerializer(ModelWrapper::Video(v), NFOMode::TVSHOW) => { + let nfo_time = match nfo_time_type { + NFOTimeType::FavTime => v.favtime, + NFOTimeType::PubTime => v.pubtime, + }; writer .create_element("tvshow") .write_inner_content_async::<_, _, Error>(|writer| async move { @@ -379,7 +387,7 @@ impl<'a> NFOSerializer<'a> { .unwrap(); writer .create_element("year") - .write_text_content_async(BytesText::new(&v.favtime.format("%Y").to_string())) + .write_text_content_async(BytesText::new(&nfo_time.format("%Y").to_string())) .await .unwrap(); if let Some(tags) = &v.tags { @@ -400,7 +408,7 @@ impl<'a> NFOSerializer<'a> { .unwrap(); writer .create_element("aired") - .write_text_content_async(BytesText::new(&v.favtime.format("%Y-%m-%d").to_string())) + .write_text_content_async(BytesText::new(&nfo_time.format("%Y-%m-%d").to_string())) .await .unwrap(); Ok(writer) @@ -490,8 +498,8 @@ mod tests { chrono::NaiveTime::from_hms_opt(2, 2, 2).unwrap(), ), pubtime: chrono::NaiveDateTime::new( - chrono::NaiveDate::from_ymd_opt(2022, 2, 2).unwrap(), - chrono::NaiveTime::from_hms_opt(2, 2, 2).unwrap(), + chrono::NaiveDate::from_ymd_opt(2033, 3, 3).unwrap(), + chrono::NaiveTime::from_hms_opt(3, 3, 3).unwrap(), ), bvid: "bvid".to_string(), tags: Some(serde_json::json!(["tag1", "tag2"])), @@ -499,7 +507,7 @@ mod tests { }; assert_eq!( NFOSerializer(ModelWrapper::Video(&video), NFOMode::MOVIE) - .generate_nfo() + .generate_nfo(&NFOTimeType::PubTime) .await .unwrap(), r#" @@ -511,16 +519,16 @@ mod tests { 1 upper_name - 2022 + 2033 tag1 tag2 bvid - 2022-02-02 + 2033-03-03 "#, ); assert_eq!( NFOSerializer(ModelWrapper::Video(&video), NFOMode::TVSHOW) - .generate_nfo() + .generate_nfo(&NFOTimeType::FavTime) .await .unwrap(), r#" @@ -541,7 +549,7 @@ mod tests { ); assert_eq!( NFOSerializer(ModelWrapper::Video(&video), NFOMode::UPPER) - .generate_nfo() + .generate_nfo(&NFOTimeType::FavTime) .await .unwrap(), r#" @@ -549,7 +557,7 @@ mod tests { false - 2022-02-02 02:02:02 + 2033-03-03 03:03:03 1 1 "#, @@ -561,7 +569,7 @@ mod tests { }; assert_eq!( NFOSerializer(ModelWrapper::Page(&page), NFOMode::EPOSODE) - .generate_nfo() + .generate_nfo(&NFOTimeType::FavTime) .await .unwrap(), r#"