Skip to content

Commit

Permalink
feat: 支持各种任务结束之后的 delay 配置 (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoaer authored Jul 23, 2024
1 parent d46881a commit 3a8f33d
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 14 deletions.
6 changes: 3 additions & 3 deletions crates/bili_sync/src/adapter/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use sea_orm::{DatabaseConnection, QuerySelect, TransactionTrait};

use crate::adapter::VideoListModel;
use crate::bilibili::{BiliClient, BiliError, Collection, CollectionItem, CollectionType, Video, VideoInfo};
use crate::config::TEMPLATE;
use crate::utils::id_time_key;
use crate::config::{CONFIG, TEMPLATE};
use crate::utils::model::create_video_pages;
use crate::utils::status::Status;
use crate::utils::{delay, id_time_key};

pub async fn collection_from<'a>(
collection_item: &'a CollectionItem,
Expand Down Expand Up @@ -176,9 +176,9 @@ impl VideoListModel for collection::Model {
video_active_model.valid = Set(false);
video_active_model.save(connection).await?;
}
continue;
}
};
delay(CONFIG.delay.fetch_video_detail.as_ref()).await;
}
Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bili_sync/src/adapter/favorite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use sea_orm::{DatabaseConnection, QuerySelect, TransactionTrait};

use crate::adapter::VideoListModel;
use crate::bilibili::{BiliClient, BiliError, FavoriteList, Video, VideoInfo};
use crate::config::TEMPLATE;
use crate::utils::id_time_key;
use crate::config::{CONFIG, TEMPLATE};
use crate::utils::model::create_video_pages;
use crate::utils::status::Status;
use crate::utils::{delay, id_time_key};

pub async fn favorite_from<'a>(
fid: &str,
Expand Down Expand Up @@ -163,9 +163,9 @@ impl VideoListModel for favorite::Model {
video_active_model.valid = Set(false);
video_active_model.save(connection).await?;
}
continue;
}
};
delay(CONFIG.delay.fetch_video_detail.as_ref()).await;
}
Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bili_sync/src/adapter/watch_later.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use sea_orm::{DatabaseConnection, QuerySelect, TransactionTrait};

use crate::adapter::VideoListModel;
use crate::bilibili::{BiliClient, BiliError, Video, VideoInfo, WatchLater};
use crate::config::TEMPLATE;
use crate::utils::id_time_key;
use crate::config::{CONFIG, TEMPLATE};
use crate::utils::model::create_video_pages;
use crate::utils::status::Status;
use crate::utils::{delay, id_time_key};

pub async fn watch_later_from<'a>(
path: &Path,
Expand Down Expand Up @@ -159,9 +159,9 @@ impl VideoListModel for watch_later::Model {
video_active_model.valid = Set(false);
video_active_model.save(connection).await?;
}
continue;
}
};
delay(CONFIG.delay.fetch_video_detail.as_ref()).await;
}
Ok(())
}
Expand Down
2 changes: 0 additions & 2 deletions crates/bili_sync/src/bilibili/collection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(dead_code)]

use std::fmt::{Display, Formatter};

use anyhow::Result;
Expand Down
35 changes: 34 additions & 1 deletion crates/bili_sync/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ pub struct Config {
pub upper_path: PathBuf,
#[serde(default)]
pub nfo_time_type: NFOTimeType,
#[serde(default)]
pub delay: DelayConfig,
}

#[derive(Serialize, Deserialize, Default)]
Expand All @@ -84,6 +86,21 @@ pub struct WatchLaterConfig {
pub path: PathBuf,
}

#[derive(Serialize, Deserialize, Default)]
pub struct DelayConfig {
pub refresh_video_list: Option<Delay>,
pub fetch_video_detail: Option<Delay>,
pub download_video: Option<Delay>,
pub download_page: Option<Delay>,
}

#[derive(Serialize, Deserialize)]
#[serde(untagged, rename_all = "lowercase")]
pub enum Delay {
Random { min: u64, max: u64 },
Fixed(u64),
}

#[derive(Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum NFOTimeType {
Expand All @@ -106,6 +123,7 @@ impl Default for Config {
interval: 1200,
upper_path: CONFIG_DIR.join("upper_face"),
nfo_time_type: NFOTimeType::FavTime,
delay: Default::default(),
}
}
}
Expand Down Expand Up @@ -160,7 +178,22 @@ impl Config {
error!("未设置 Credential 信息");
}
}

for delay_config in [
&self.delay.refresh_video_list,
&self.delay.fetch_video_detail,
&self.delay.download_video,
&self.delay.download_page,
]
.iter()
.filter_map(|x| x.as_ref())
{
if let Delay::Random { min, max } = delay_config {
if min >= max {
ok = false;
error!("随机延迟的最小值应小于最大值");
}
}
}
if !ok {
panic!(
"位于 {} 的配置文件不合法,请参考提示信息修复后继续运行",
Expand Down
3 changes: 1 addition & 2 deletions crates/bili_sync/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mod downloader;
mod error;
mod utils;
mod workflow;
use std::time::Duration;

use once_cell::sync::Lazy;
use tokio::time;
Expand Down Expand Up @@ -74,6 +73,6 @@ async fn main() {
info!("稍后再看处理完毕");
info!("本轮任务执行完毕,等待下一轮执行");
}
time::sleep(Duration::from_secs(CONFIG.interval)).await;
time::sleep(time::Duration::from_secs(CONFIG.interval)).await;
}
}
20 changes: 20 additions & 0 deletions crates/bili_sync/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ pub mod nfo;
pub mod status;

use chrono::{DateTime, Utc};
use rand::Rng;
use tokio::time;
use tracing_subscriber::util::SubscriberInitExt;

use crate::config::Delay;

pub fn init_logger(log_level: &str) {
tracing_subscriber::fmt::Subscriber::builder()
.with_env_filter(tracing_subscriber::EnvFilter::builder().parse_lossy(log_level))
Expand All @@ -21,3 +25,19 @@ pub fn init_logger(log_level: &str) {
pub fn id_time_key(bvid: &String, time: &DateTime<Utc>) -> String {
format!("{}-{}", bvid, time.timestamp())
}

pub(crate) async fn delay(delay: Option<&Delay>) {
match delay {
None => {}
Some(Delay::Random { min, max }) => {
let delay = {
let mut rng = rand::thread_rng();
rng.gen_range(*min..=*max)
};
time::sleep(time::Duration::from_millis(delay)).await;
}
Some(Delay::Fixed(delay)) => {
time::sleep(time::Duration::from_millis(*delay)).await;
}
}
}
4 changes: 4 additions & 0 deletions crates/bili_sync/src/workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::bilibili::{BestStream, BiliClient, BiliError, Dimension, PageInfo, Vi
use crate::config::{ARGS, CONFIG, TEMPLATE};
use crate::downloader::Downloader;
use crate::error::{DownloadAbortError, ProcessPageError};
use crate::utils::delay;
use crate::utils::model::{create_videos, update_pages_model, update_videos_model};
use crate::utils::nfo::{ModelWrapper, NFOMode, NFOSerializer};
use crate::utils::status::{PageStatus, VideoStatus};
Expand Down Expand Up @@ -59,6 +60,7 @@ pub async fn refresh_video_list<'a>(
info!("到达上一次处理的位置,提前中止");
break;
}
delay(CONFIG.delay.refresh_video_list.as_ref()).await;
}
new_count = video_list_model.video_count(connection).await? - new_count;
video_list_model.log_refresh_video_end(got_count, new_count);
Expand Down Expand Up @@ -226,6 +228,7 @@ pub async fn download_video_pages(
}
let mut video_active_model: video::ActiveModel = video_model.into();
video_active_model.download_status = Set(status.into());
delay(CONFIG.delay.download_video.as_ref()).await;
Ok(video_active_model)
}

Expand Down Expand Up @@ -411,6 +414,7 @@ pub async fn download_page(
let mut page_active_model: page::ActiveModel = page_model.into();
page_active_model.download_status = Set(status.into());
page_active_model.path = Set(Some(video_path.to_str().unwrap().to_string()));
delay(CONFIG.delay.download_page.as_ref()).await;
Ok(page_active_model)
}

Expand Down

0 comments on commit 3a8f33d

Please sign in to comment.