From 2a983590850fdd00acc26ab1577a0c91db50b596 Mon Sep 17 00:00:00 2001 From: amtoaer Date: Sat, 25 Jan 2025 00:11:22 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E9=9A=90=E8=97=8F=20target=20?= =?UTF-8?q?=E5=B9=B6=E8=B0=83=E6=95=B4=E8=A1=A8=E8=BF=B0=EF=BC=8C=E7=BC=A9?= =?UTF-8?q?=E5=87=8F=E6=97=A5=E5=BF=97=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/bili_sync/src/adapter/collection.rs | 56 ++++++-------------- crates/bili_sync/src/adapter/favorite.rs | 39 +++++++------- crates/bili_sync/src/adapter/mod.rs | 17 +++--- crates/bili_sync/src/adapter/submission.rs | 51 +++++++----------- crates/bili_sync/src/adapter/watch_later.rs | 36 ++++++------- crates/bili_sync/src/bilibili/collection.rs | 4 +- crates/bili_sync/src/config/global.rs | 5 +- crates/bili_sync/src/main.rs | 58 ++++++++++----------- crates/bili_sync/src/utils/mod.rs | 4 +- crates/bili_sync/src/workflow.rs | 35 +++++-------- 10 files changed, 128 insertions(+), 177 deletions(-) diff --git a/crates/bili_sync/src/adapter/collection.rs b/crates/bili_sync/src/adapter/collection.rs index 88d514c..d74b704 100644 --- a/crates/bili_sync/src/adapter/collection.rs +++ b/crates/bili_sync/src/adapter/collection.rs @@ -37,59 +37,37 @@ impl VideoListModel for collection::Model { }) } + fn log_refresh_video_start(&self) { + info!("开始扫描{}「{}」..", CollectionType::from(self.r#type), self.name); + } + + fn log_refresh_video_end(&self, count: usize) { + info!( + "扫描{}「{}」完成,获取到 {} 条新视频", + CollectionType::from(self.r#type), + self.name, + count, + ); + } + fn log_fetch_video_start(&self) { info!( - "开始获取{} {} - {} 的视频与分页信息...", + "开始填充{}「{}」视频详情..", CollectionType::from(self.r#type), - self.s_id, self.name ); } fn log_fetch_video_end(&self) { - info!( - "获取{} {} - {} 的视频与分页信息完成", - CollectionType::from(self.r#type), - self.s_id, - self.name - ); + info!("填充{}「{}」视频详情完成", CollectionType::from(self.r#type), self.name); } fn log_download_video_start(&self) { - info!( - "开始下载{}: {} - {} 中所有未处理过的视频...", - CollectionType::from(self.r#type), - self.s_id, - self.name - ); + info!("开始下载{}「{}」视频..", CollectionType::from(self.r#type), self.name); } fn log_download_video_end(&self) { - info!( - "下载{}: {} - {} 中未处理过的视频完成", - CollectionType::from(self.r#type), - self.s_id, - self.name - ); - } - - fn log_refresh_video_start(&self) { - info!( - "开始扫描{}: {} - {} 的新视频...", - CollectionType::from(self.r#type), - self.s_id, - self.name - ); - } - - fn log_refresh_video_end(&self, count: usize) { - info!( - "扫描{}: {} - {} 的新视频完成,获取了 {} 条新视频", - CollectionType::from(self.r#type), - self.s_id, - self.name, - count, - ); + info!("下载{}「{}」视频完成", CollectionType::from(self.r#type), self.name); } } diff --git a/crates/bili_sync/src/adapter/favorite.rs b/crates/bili_sync/src/adapter/favorite.rs index 008a989..4d067fd 100644 --- a/crates/bili_sync/src/adapter/favorite.rs +++ b/crates/bili_sync/src/adapter/favorite.rs @@ -37,31 +37,28 @@ impl VideoListModel for favorite::Model { }) } - fn log_fetch_video_start(&self) { - info!("开始获取收藏夹 {} - {} 的视频与分页信息...", self.f_id, self.name); - } - - fn log_fetch_video_end(&self) { - info!("获取收藏夹 {} - {} 的视频与分页信息完成", self.f_id, self.name); - } - - fn log_download_video_start(&self) { - info!("开始下载收藏夹: {} - {} 中所有未处理过的视频...", self.f_id, self.name); - } - - fn log_download_video_end(&self) { - info!("下载收藏夹: {} - {} 中未处理过的视频完成", self.f_id, self.name); - } - fn log_refresh_video_start(&self) { - info!("开始扫描收藏夹: {} - {} 的新视频...", self.f_id, self.name); + info!("开始扫描收藏夹「{}」..", self.name); } fn log_refresh_video_end(&self, count: usize) { - info!( - "扫描收藏夹: {} - {} 的新视频完成,获取了 {} 条新视频", - self.f_id, self.name, count - ); + info!("扫描收藏夹「{}」完成,获取到 {} 条新视频", self.name, count); + } + + fn log_fetch_video_start(&self) { + info!("开始填充收藏夹「{}」视频详情..", self.name); + } + + fn log_fetch_video_end(&self) { + info!("填充收藏夹「{}」视频详情完成", self.name); + } + + fn log_download_video_start(&self) { + info!("开始下载收藏夹「{}」视频..", self.name); + } + + fn log_download_video_end(&self) { + info!("下载收藏夹「{}」视频完成", self.name); } } diff --git a/crates/bili_sync/src/adapter/mod.rs b/crates/bili_sync/src/adapter/mod.rs index aac112c..0605ced 100644 --- a/crates/bili_sync/src/adapter/mod.rs +++ b/crates/bili_sync/src/adapter/mod.rs @@ -52,10 +52,16 @@ pub trait VideoListModel { /// Box 又提示 ActiveModelTrait 没有 object safety,因此手写一个 Enum 静态分发 fn update_latest_row_at(&self, datetime: DateTime) -> _ActiveModel; - /// 开始获取视频 + /// 开始刷新视频 + fn log_refresh_video_start(&self); + + /// 结束刷新视频 + fn log_refresh_video_end(&self, count: usize); + + /// 开始填充视频 fn log_fetch_video_start(&self); - /// 结束获取视频 + /// 结束填充视频 fn log_fetch_video_end(&self); /// 开始下载视频 @@ -63,14 +69,9 @@ pub trait VideoListModel { /// 结束下载视频 fn log_download_video_end(&self); - - /// 开始刷新视频 - fn log_refresh_video_start(&self); - - /// 结束刷新视频 - fn log_refresh_video_end(&self, count: usize); } +#[derive(Clone, Copy)] pub enum Args<'a> { Favorite { fid: &'a str }, Collection { collection_item: &'a CollectionItem }, diff --git a/crates/bili_sync/src/adapter/submission.rs b/crates/bili_sync/src/adapter/submission.rs index aeb58fb..e0a8575 100644 --- a/crates/bili_sync/src/adapter/submission.rs +++ b/crates/bili_sync/src/adapter/submission.rs @@ -37,43 +37,28 @@ impl VideoListModel for submission::Model { }) } - fn log_fetch_video_start(&self) { - info!( - "开始获取 UP 主 {} - {} 投稿的视频与分页信息...", - self.upper_id, self.upper_name - ); - } - - fn log_fetch_video_end(&self) { - info!( - "获取 UP 主 {} - {} 投稿的视频与分页信息完成", - self.upper_id, self.upper_name - ); - } - - fn log_download_video_start(&self) { - info!( - "开始下载 UP 主 {} - {} 投稿的所有未处理过的视频...", - self.upper_id, self.upper_name - ); - } - - fn log_download_video_end(&self) { - info!( - "下载 UP 主 {} - {} 投稿的所有未处理过的视频完成", - self.upper_id, self.upper_name - ); - } - fn log_refresh_video_start(&self) { - info!("开始扫描 UP 主 {} - {} 投稿的新视频...", self.upper_id, self.upper_name); + info!("开始扫描「{}」投稿..", self.upper_name); } fn log_refresh_video_end(&self, count: usize) { - info!( - "扫描 UP 主 {} - {} 投稿的新视频完成,获取了 {} 条新视频", - self.upper_id, self.upper_name, count, - ); + info!("扫描「{}」投稿完成,获取到 {} 条新视频", self.upper_name, count,); + } + + fn log_fetch_video_start(&self) { + info!("开始填充「{}」投稿视频详情..", self.upper_name); + } + + fn log_fetch_video_end(&self) { + info!("填充「{}」投稿视频详情完成", self.upper_name); + } + + fn log_download_video_start(&self) { + info!("开始下载「{}」投稿视频..", self.upper_name); + } + + fn log_download_video_end(&self) { + info!("下载「{}」投稿视频完成", self.upper_name); } } diff --git a/crates/bili_sync/src/adapter/watch_later.rs b/crates/bili_sync/src/adapter/watch_later.rs index de0ec38..979ced8 100644 --- a/crates/bili_sync/src/adapter/watch_later.rs +++ b/crates/bili_sync/src/adapter/watch_later.rs @@ -37,28 +37,28 @@ impl VideoListModel for watch_later::Model { }) } - fn log_fetch_video_start(&self) { - info!("开始获取稍后再看的视频与分页信息..."); - } - - fn log_fetch_video_end(&self) { - info!("获取稍后再看的视频与分页信息完成"); - } - - fn log_download_video_start(&self) { - info!("开始下载稍后再看中所有未处理过的视频..."); - } - - fn log_download_video_end(&self) { - info!("下载稍后再看中未处理过的视频完成"); - } - fn log_refresh_video_start(&self) { - info!("开始扫描稍后再看的新视频..."); + info!("开始扫描稍后再看.."); } fn log_refresh_video_end(&self, count: usize) { - info!("扫描稍后再看的新视频完成,获取了 {} 条新视频", count); + info!("扫描稍后再看完成,获取到 {} 条新视频", count); + } + + fn log_fetch_video_start(&self) { + info!("开始填充稍后再看视频详情.."); + } + + fn log_fetch_video_end(&self) { + info!("填充稍后再看视频详情完成"); + } + + fn log_download_video_start(&self) { + info!("开始下载稍后再看视频.."); + } + + fn log_download_video_end(&self) { + info!("下载稍后再看视频完成"); } } diff --git a/crates/bili_sync/src/bilibili/collection.rs b/crates/bili_sync/src/bilibili/collection.rs index 5c0f67d..7c98700 100644 --- a/crates/bili_sync/src/bilibili/collection.rs +++ b/crates/bili_sync/src/bilibili/collection.rs @@ -38,8 +38,8 @@ impl From for CollectionType { impl Display for CollectionType { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { let s = match self { - CollectionType::Series => "视频列表", - CollectionType::Season => "视频合集", + CollectionType::Series => "列表", + CollectionType::Season => "合集", }; write!(f, "{}", s) } diff --git a/crates/bili_sync/src/config/global.rs b/crates/bili_sync/src/config/global.rs index 0ced6d2..57d5cb7 100644 --- a/crates/bili_sync/src/config/global.rs +++ b/crates/bili_sync/src/config/global.rs @@ -53,10 +53,9 @@ fn load_config() -> Config { info!("已将默认配置写入 {}", CONFIG_DIR.join("config.toml").display()); default_config }); - // 检查配置文件内容 - info!("校验配置文件内容..."); + info!("检查配置文件.."); config.check(); - info!("配置文件内容校验通过"); + info!("配置文件检查通过"); config } diff --git a/crates/bili_sync/src/main.rs b/crates/bili_sync/src/main.rs index 4c45602..ffb7dc9 100644 --- a/crates/bili_sync/src/main.rs +++ b/crates/bili_sync/src/main.rs @@ -10,6 +10,8 @@ mod error; mod utils; mod workflow; +use std::path::PathBuf; + use once_cell::sync::Lazy; use tokio::time; @@ -28,17 +30,17 @@ async fn main() { let connection = database_connection().await.expect("获取数据库连接失败"); let mut anchor = chrono::Local::now().date_naive(); let bili_client = BiliClient::new(); - let watch_later_config = &CONFIG.watch_later; + let params = build_params(); loop { 'inner: { match bili_client.wbi_img().await.map(|wbi_img| wbi_img.into()) { Ok(Some(mixin_key)) => bilibili::set_global_mixin_key(mixin_key), Ok(_) => { - error!("获取 mixin key 失败,无法进行 wbi 签名,等待下一轮执行"); + error!("解析 mixin key 失败,等待下一轮执行"); break 'inner; } Err(e) => { - error!("获取 mixin key 时遇到错误:{e},等待下一轮执行"); + error!("获取 mixin key 遇到错误:{e},等待下一轮执行"); break 'inner; } }; @@ -49,37 +51,33 @@ async fn main() { } anchor = chrono::Local::now().date_naive(); } - for (fid, path) in &CONFIG.favorite_list { - if let Err(e) = process_video_list(Args::Favorite { fid }, &bili_client, path, &connection).await { - error!("处理收藏夹 {fid} 时遇到非预期的错误:{e}"); + for (args, path) in ¶ms { + if let Err(e) = process_video_list(*args, &bili_client, path, &connection).await { + error!("处理过程遇到错误:{e}"); } } - info!("所有收藏夹处理完毕"); - for (collection_item, path) in &CONFIG.collection_list { - if let Err(e) = - process_video_list(Args::Collection { collection_item }, &bili_client, path, &connection).await - { - error!("处理合集 {collection_item:?} 时遇到非预期的错误:{e}"); - } - } - info!("所有合集处理完毕"); - if watch_later_config.enabled { - if let Err(e) = - process_video_list(Args::WatchLater, &bili_client, &watch_later_config.path, &connection).await - { - error!("处理稍后再看时遇到非预期的错误:{e}"); - } - } - info!("稍后再看处理完毕"); - for (upper_id, path) in &CONFIG.submission_list { - if let Err(e) = process_video_list(Args::Submission { upper_id }, &bili_client, path, &connection).await - { - error!("处理 UP 主 {upper_id} 投稿时遇到非预期的错误:{e}"); - } - } - info!("所有 UP 主投稿处理完毕"); info!("本轮任务执行完毕,等待下一轮执行"); } time::sleep(time::Duration::from_secs(CONFIG.interval)).await; } } + +fn build_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 +} diff --git a/crates/bili_sync/src/utils/mod.rs b/crates/bili_sync/src/utils/mod.rs index d92f0c3..f465419 100644 --- a/crates/bili_sync/src/utils/mod.rs +++ b/crates/bili_sync/src/utils/mod.rs @@ -9,9 +9,11 @@ use tracing_subscriber::util::SubscriberInitExt; pub fn init_logger(log_level: &str) { tracing_subscriber::fmt::Subscriber::builder() + .compact() .with_env_filter(tracing_subscriber::EnvFilter::builder().parse_lossy(log_level)) + .with_target(false) .with_timer(tracing_subscriber::fmt::time::ChronoLocal::new( - "%Y-%m-%d %H:%M:%S%.3f".to_owned(), + "%b %d %H:%M:%S".to_owned(), )) .finish() .try_init() diff --git a/crates/bili_sync/src/workflow.rs b/crates/bili_sync/src/workflow.rs index f83eeb4..7022f3f 100644 --- a/crates/bili_sync/src/workflow.rs +++ b/crates/bili_sync/src/workflow.rs @@ -39,7 +39,7 @@ pub async fn process_video_list( // 单独请求视频详情接口,获取视频的详情信息与所有的分页,写入数据库 fetch_video_details(bili_client, &video_list_model, connection).await?; if ARGS.scan_only { - warn!("已开启仅扫描模式,跳过视频下载..."); + warn!("已开启仅扫描模式,跳过视频下载.."); } else { // 从数据库中查找所有未下载的视频与分页,下载并处理 download_unprocessed_videos(bili_client, &video_list_model, connection).await?; @@ -192,7 +192,7 @@ pub async fn download_unprocessed_videos( update_videos_model(models, connection).await?; } if download_aborted { - error!("下载视频时触发风控,终止收藏夹下所有下载任务,等待下一轮执行"); + error!("下载触发风控,已终止所有任务,等待下一轮执行"); } video_list_model.log_download_video_end(); Ok(()) @@ -270,16 +270,10 @@ pub async fn download_video_pages( results .iter() .take(4) - .zip(["封面", "视频 nfo", "up 主头像", "up 主 nfo"]) + .zip(["封面", "详情", "作者头像", "作者详情"]) .for_each(|(res, task_name)| match res { - Ok(_) => info!( - "处理视频 {} - {} 的 {} 成功", - &video_model.bvid, &video_model.name, task_name - ), - Err(e) => error!( - "处理视频 {} - {} 的 {} 失败: {}", - &video_model.bvid, &video_model.name, task_name, e - ), + Ok(_) => info!("处理视频「{}」{}成功", &video_model.name, task_name), + Err(e) => error!("处理视频「{}」{}失败: {}", &video_model.name, task_name, e), }); if let Err(e) = results.into_iter().nth(4).context("page download result not found")? { if e.downcast_ref::().is_some() { @@ -348,16 +342,13 @@ pub async fn dispatch_download_page( update_pages_model(models, connection).await?; } if download_aborted { - error!( - "下载视频 {} - {} 的分页时触发风控,将异常向上传递...", - &video_model.bvid, &video_model.name - ); + error!("下载视频「{}」的分页时触发风控,将异常向上传递..", &video_model.name); bail!(DownloadAbortError()); } if error_occurred { error!( - "下载视频 {} - {} 的分页时出现了错误,将在下一轮尝试重新处理", - &video_model.bvid, &video_model.name + "下载视频「{}」的分页时出现错误,将在下一轮尝试重新处理", + &video_model.name ); bail!(ProcessPageError()); } @@ -449,15 +440,15 @@ pub async fn download_page( status.update_status(&results); results .iter() - .zip(["封面", "视频", "视频 nfo", "弹幕"]) + .zip(["封面", "视频", "详情", "弹幕"]) .for_each(|(res, task_name)| match res { Ok(_) => info!( - "处理视频 {} - {} 第 {} 页的 {} 成功", - &video_model.bvid, &video_model.name, page_model.pid, task_name + "处理视频「{}」第 {} 页{}成功", + &video_model.name, page_model.pid, task_name ), Err(e) => error!( - "处理视频 {} - {} 第 {} 页的 {} 失败: {}", - &video_model.bvid, &video_model.name, page_model.pid, task_name, e + "处理视频「{}」第 {} 页{}失败: {}", + &video_model.name, page_model.pid, task_name, e ), }); // 如果下载视频时触发风控,直接返回 DownloadAbortError