mirror of
https://github.com/amtoaer/bili-sync.git
synced 2026-09-05 15:39:07 +08:00
feat: 支持从文件中读取配置
This commit is contained in:
@@ -1,12 +1,14 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::Result;
|
use crate::Result;
|
||||||
|
|
||||||
pub struct PageAnalyzer {
|
pub struct PageAnalyzer {
|
||||||
info: serde_json::Value,
|
info: serde_json::Value,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, strum::FromRepr, PartialEq, PartialOrd)]
|
#[derive(Debug, strum::FromRepr, PartialEq, PartialOrd, Serialize, Deserialize)]
|
||||||
pub enum VideoQuality {
|
pub enum VideoQuality {
|
||||||
Quality360p = 16,
|
Quality360p = 16,
|
||||||
Quality480p = 32,
|
Quality480p = 32,
|
||||||
@@ -19,7 +21,7 @@ pub enum VideoQuality {
|
|||||||
QualityDolby = 126,
|
QualityDolby = 126,
|
||||||
Quality8k = 127,
|
Quality8k = 127,
|
||||||
}
|
}
|
||||||
#[derive(Debug, strum::FromRepr, PartialEq, PartialOrd)]
|
#[derive(Debug, strum::FromRepr, PartialEq, PartialOrd, Serialize, Deserialize)]
|
||||||
pub enum AudioQuality {
|
pub enum AudioQuality {
|
||||||
Quality64k = 30216,
|
Quality64k = 30216,
|
||||||
Quality132k = 30232,
|
Quality132k = 30232,
|
||||||
@@ -28,7 +30,7 @@ pub enum AudioQuality {
|
|||||||
Quality192k = 30280,
|
Quality192k = 30280,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, strum::EnumString, strum::Display, PartialEq, PartialOrd)]
|
#[derive(Debug, strum::EnumString, strum::Display, PartialEq, PartialOrd, Serialize, Deserialize)]
|
||||||
pub enum VideoCodecs {
|
pub enum VideoCodecs {
|
||||||
#[strum(serialize = "hev")]
|
#[strum(serialize = "hev")]
|
||||||
HEV,
|
HEV,
|
||||||
@@ -39,6 +41,7 @@ pub enum VideoCodecs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 视频流的筛选偏好
|
// 视频流的筛选偏好
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct FilterOption {
|
pub struct FilterOption {
|
||||||
pub video_max_quality: VideoQuality,
|
pub video_max_quality: VideoQuality,
|
||||||
pub video_min_quality: VideoQuality,
|
pub video_min_quality: VideoQuality,
|
||||||
|
|||||||
@@ -0,0 +1,96 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
use log::warn;
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::bilibili::{Credential, FilterOption};
|
||||||
|
use crate::Result;
|
||||||
|
|
||||||
|
pub static CONFIG: Lazy<Mutex<Config>> = Lazy::new(|| {
|
||||||
|
let config = Config::new();
|
||||||
|
// 保存一次,确保配置文件存在
|
||||||
|
config.save().unwrap();
|
||||||
|
// 检查配置文件内容
|
||||||
|
config.check();
|
||||||
|
Mutex::new(Config::new())
|
||||||
|
});
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct Config {
|
||||||
|
pub credential: Option<Credential>,
|
||||||
|
pub filter_option: FilterOption,
|
||||||
|
pub favorite_list: HashMap<String, String>,
|
||||||
|
pub video_name: String,
|
||||||
|
pub page_name: String,
|
||||||
|
pub interval: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Config {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
credential: Some(Credential::default()),
|
||||||
|
filter_option: FilterOption::default(),
|
||||||
|
favorite_list: HashMap::new(),
|
||||||
|
video_name: "{{bvid}}".to_string(),
|
||||||
|
page_name: "{{bvid}}".to_string(),
|
||||||
|
interval: 1200,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Config {
|
||||||
|
fn new() -> Self {
|
||||||
|
Config::load().unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 简单的预检查
|
||||||
|
pub fn check(&self) {
|
||||||
|
assert!(
|
||||||
|
!self.favorite_list.is_empty(),
|
||||||
|
"No favorite list found, program won't do anything"
|
||||||
|
);
|
||||||
|
for path in self.favorite_list.values() {
|
||||||
|
assert!(Path::new(path).is_absolute(), "Path in favorite list must be absolute");
|
||||||
|
}
|
||||||
|
assert!(!self.video_name.is_empty(), "No video name template found");
|
||||||
|
assert!(!self.page_name.is_empty(), "No page name template found");
|
||||||
|
match self.credential {
|
||||||
|
Some(ref credential) => {
|
||||||
|
assert!(
|
||||||
|
!(credential.sessdata.is_empty()
|
||||||
|
|| credential.bili_jct.is_empty()
|
||||||
|
|| credential.buvid3.is_empty()
|
||||||
|
|| credential.dedeuserid.is_empty()
|
||||||
|
|| credential.ac_time_value.is_empty()),
|
||||||
|
"Credential is incomplete"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
warn!("No credential found, can't access high quality video");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load() -> Result<Self> {
|
||||||
|
let config_path = dirs::config_dir()
|
||||||
|
.ok_or("No config path found")?
|
||||||
|
.join("bili-sync")
|
||||||
|
.join("config.toml");
|
||||||
|
let config_content = std::fs::read_to_string(config_path)?;
|
||||||
|
Ok(toml::from_str(&config_content)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn save(&self) -> Result<()> {
|
||||||
|
let config_path = dirs::config_dir()
|
||||||
|
.ok_or("No config path found")?
|
||||||
|
.join("bili-sync")
|
||||||
|
.join("config.toml");
|
||||||
|
if let Some(parent) = config_path.parent() {
|
||||||
|
std::fs::create_dir_all(parent)?;
|
||||||
|
}
|
||||||
|
std::fs::write(config_path, toml::to_string_pretty(self)?)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
use std::error;
|
use std::error;
|
||||||
|
|
||||||
pub mod bilibili;
|
pub mod bilibili;
|
||||||
|
pub mod config;
|
||||||
pub mod core;
|
pub mod core;
|
||||||
pub mod database;
|
pub mod database;
|
||||||
pub mod downloader;
|
pub mod downloader;
|
||||||
|
|||||||
Reference in New Issue
Block a user