fix: 修复配置初始化的检测 (#284)

This commit is contained in:
ᴀᴍᴛᴏᴀᴇʀ
2025-02-21 19:32:46 +08:00
committed by GitHub
parent 94462ca706
commit f8b93d2c76
3 changed files with 25 additions and 35 deletions

View File

@@ -71,7 +71,7 @@ pub trait VideoSource {
fn log_download_video_end(&self);
}
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
pub enum Args<'a> {
Favorite { fid: &'a str },
Collection { collection_item: &'a CollectionItem },

View File

@@ -12,6 +12,7 @@ mod clap;
mod global;
mod item;
use crate::adapter::Args;
use crate::bilibili::{CollectionItem, Credential, DanmakuOption, FilterOption};
pub use crate::config::global::{ARGS, CONFIG, CONFIG_DIR, TEMPLATE};
use crate::config::item::{ConcurrentLimit, deserialize_collection_list, serialize_collection_list};
@@ -107,23 +108,35 @@ impl Config {
Ok(toml::from_str(&config_content)?)
}
pub fn as_video_sources(&self) -> Vec<(Args<'_>, &PathBuf)> {
let mut params = Vec::new();
self.favorite_list
.iter()
.for_each(|(fid, path)| params.push((Args::Favorite { fid }, path)));
self.collection_list
.iter()
.for_each(|(collection_item, path)| params.push((Args::Collection { collection_item }, path)));
if self.watch_later.enabled {
params.push((Args::WatchLater, &self.watch_later.path));
}
self.submission_list
.iter()
.for_each(|(upper_id, path)| params.push((Args::Submission { upper_id }, path)));
params
}
#[cfg(not(test))]
pub fn check(&self) {
let mut ok = true;
if self.favorite_list.is_empty() && self.collection_list.is_empty() && !self.watch_later.enabled {
let video_sources = self.as_video_sources();
if video_sources.is_empty() {
ok = false;
error!("没有配置任何需要扫描的内容,程序空转没有意义");
}
if self.watch_later.enabled && !self.watch_later.path.is_absolute() {
error!(
"稍后再看保存的路径应为绝对路径,检测到:{}",
self.watch_later.path.display()
);
}
for path in self.favorite_list.values() {
for (args, path) in video_sources {
if !path.is_absolute() {
ok = false;
error!("收藏夹保存的路径应为绝对路径,检测到: {}", path.display());
error!("{:?} 保存的路径应为绝对路径,检测到: {}", args, path.display());
}
}
if !self.upper_path.is_absolute() {

View File

@@ -1,10 +1,8 @@
use std::path::PathBuf;
use std::sync::Arc;
use sea_orm::DatabaseConnection;
use tokio::time;
use crate::adapter::Args;
use crate::bilibili::{self, BiliClient};
use crate::config::CONFIG;
use crate::workflow::process_video_source;
@@ -13,7 +11,7 @@ use crate::workflow::process_video_source;
pub async fn video_downloader(connection: Arc<DatabaseConnection>) {
let mut anchor = chrono::Local::now().date_naive();
let bili_client = BiliClient::new();
let params = collect_task_params();
let video_sources = CONFIG.as_video_sources();
loop {
'inner: {
match bili_client.wbi_img().await.map(|wbi_img| wbi_img.into()) {
@@ -34,7 +32,7 @@ pub async fn video_downloader(connection: Arc<DatabaseConnection>) {
}
anchor = chrono::Local::now().date_naive();
}
for (args, path) in &params {
for (args, path) in &video_sources {
if let Err(e) = process_video_source(*args, &bili_client, path, &connection).await {
error!("处理过程遇到错误:{:#}", e);
}
@@ -44,24 +42,3 @@ pub async fn video_downloader(connection: Arc<DatabaseConnection>) {
time::sleep(time::Duration::from_secs(CONFIG.interval)).await;
}
}
/// 构造下载视频任务执行所需的参数(下载类型和保存路径)
fn collect_task_params() -> Vec<(Args<'static>, &'static PathBuf)> {
let mut params = Vec::new();
CONFIG
.favorite_list
.iter()
.for_each(|(fid, path)| params.push((Args::Favorite { fid }, path)));
CONFIG
.collection_list
.iter()
.for_each(|(collection_item, path)| params.push((Args::Collection { collection_item }, path)));
if CONFIG.watch_later.enabled {
params.push((Args::WatchLater, &CONFIG.watch_later.path));
}
CONFIG
.submission_list
.iter()
.for_each(|(upper_id, path)| params.push((Args::Submission { upper_id }, path)));
params
}