refactor: 用更 idiomatic 的方式改写一些代码 (#54)
* refactor: Config 采用 arc_swap 而非锁 * refactor: 改进 config 的检查,及其他一些细微优化 * refactor: 不再拆分 lib.rs 和 main.rs
This commit is contained in:
34
src/main.rs
34
src/main.rs
@@ -1,34 +1,44 @@
|
||||
use bili_sync::bilibili::BiliClient;
|
||||
use bili_sync::core::command::process_favorite_list;
|
||||
use bili_sync::database::{database_connection, migrate_database};
|
||||
use log::error;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
mod bilibili;
|
||||
mod config;
|
||||
mod core;
|
||||
mod database;
|
||||
mod downloader;
|
||||
mod error;
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use self::bilibili::BiliClient;
|
||||
use self::config::CONFIG;
|
||||
use self::core::command::process_favorite_list;
|
||||
use self::database::{database_connection, migrate_database};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> ! {
|
||||
env_logger::init();
|
||||
Lazy::force(&CONFIG);
|
||||
let mut anchor = chrono::Local::now().date_naive();
|
||||
let (credential, interval, favorites) = {
|
||||
let config = bili_sync::config::CONFIG.lock().unwrap();
|
||||
(config.credential.clone(), config.interval, config.favorite_list.clone())
|
||||
};
|
||||
let mut bili_client = BiliClient::new(credential);
|
||||
let bili_client = BiliClient::new();
|
||||
let connection = database_connection().await.unwrap();
|
||||
migrate_database(&connection).await.unwrap();
|
||||
loop {
|
||||
if anchor != chrono::Local::now().date_naive() {
|
||||
if let Err(e) = bili_client.check_refresh().await {
|
||||
error!("Error: {e}");
|
||||
tokio::time::sleep(std::time::Duration::from_secs(interval)).await;
|
||||
tokio::time::sleep(std::time::Duration::from_secs(CONFIG.interval)).await;
|
||||
continue;
|
||||
}
|
||||
anchor = chrono::Local::now().date_naive();
|
||||
}
|
||||
for (fid, path) in &favorites {
|
||||
for (fid, path) in &CONFIG.favorite_list {
|
||||
let res = process_favorite_list(&bili_client, fid, path, &connection).await;
|
||||
if let Err(e) = res {
|
||||
error!("Error: {e}");
|
||||
}
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_secs(interval)).await;
|
||||
|
||||
tokio::time::sleep(std::time::Duration::from_secs(CONFIG.interval)).await;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user