Skip to content

Commit

Permalink
feat: 支持自定义 NFO 文件中的视频时间,可选加入收藏夹的时间、视频发布的时间 (#114)
Browse files Browse the repository at this point in the history
* feat: 支持自定义 NFO 文件中的视频时间,可选加入收藏夹的时间、视频发布的时间

* chore: 使用小写
  • Loading branch information
amtoaer committed May 31, 2024
1 parent 67de151 commit 2ef99a2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
21 changes: 14 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub static CONFIG: Lazy<Config> = Lazy::new(|| {
panic!("加载配置文件失败,错误为: {err}");
}
warn!("配置文件不存在,使用默认配置...");
Config::new()
Config::default()
});
// 放到外面,确保新的配置项被保存
info!("配置加载完毕,覆盖刷新原有配置");
Expand All @@ -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(),
Expand All @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/core/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
38 changes: 23 additions & 15 deletions src/core/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<handlebars::Handlebars> = Lazy::new(|| {
Expand Down Expand Up @@ -274,7 +274,7 @@ pub async fn update_pages_model(pages: Vec<page::ActiveModel>, connection: &Data
/// serde xml 似乎不太好用,先这么裸着写
/// (真是又臭又长啊
impl<'a> NFOSerializer<'a> {
pub async fn generate_nfo(self) -> Result<String> {
pub async fn generate_nfo(self, nfo_time_type: &NFOTimeType) -> Result<String> {
let mut buffer = r#"<?xml version="1.0" encoding="utf-8" standalone="yes"?>
"#
.as_bytes()
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -490,16 +498,16 @@ 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"])),
..Default::default()
};
assert_eq!(
NFOSerializer(ModelWrapper::Video(&video), NFOMode::MOVIE)
.generate_nfo()
.generate_nfo(&NFOTimeType::PubTime)
.await
.unwrap(),
r#"<?xml version="1.0" encoding="utf-8" standalone="yes"?>
Expand All @@ -511,16 +519,16 @@ mod tests {
<name>1</name>
<role>upper_name</role>
</actor>
<year>2022</year>
<year>2033</year>
<genre>tag1</genre>
<genre>tag2</genre>
<uniqueid type="bilibili">bvid</uniqueid>
<aired>2022-02-02</aired>
<aired>2033-03-03</aired>
</movie>"#,
);
assert_eq!(
NFOSerializer(ModelWrapper::Video(&video), NFOMode::TVSHOW)
.generate_nfo()
.generate_nfo(&NFOTimeType::FavTime)
.await
.unwrap(),
r#"<?xml version="1.0" encoding="utf-8" standalone="yes"?>
Expand All @@ -541,15 +549,15 @@ mod tests {
);
assert_eq!(
NFOSerializer(ModelWrapper::Video(&video), NFOMode::UPPER)
.generate_nfo()
.generate_nfo(&NFOTimeType::FavTime)
.await
.unwrap(),
r#"<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<person>
<plot/>
<outline/>
<lockdata>false</lockdata>
<dateadded>2022-02-02 02:02:02</dateadded>
<dateadded>2033-03-03 03:03:03</dateadded>
<title>1</title>
<sorttitle>1</sorttitle>
</person>"#,
Expand All @@ -561,7 +569,7 @@ mod tests {
};
assert_eq!(
NFOSerializer(ModelWrapper::Page(&page), NFOMode::EPOSODE)
.generate_nfo()
.generate_nfo(&NFOTimeType::FavTime)
.await
.unwrap(),
r#"<?xml version="1.0" encoding="utf-8" standalone="yes"?>
Expand Down

0 comments on commit 2ef99a2

Please sign in to comment.