mirror of
https://github.com/amtoaer/bili-sync.git
synced 2026-09-06 07:57:12 +08:00
feat: 迁移所有配置到数据库,并支持运行时重载 (#364)
This commit is contained in:
@@ -2,7 +2,7 @@ use anyhow::{Context, Result, bail};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::bilibili::error::BiliError;
|
||||
use crate::config::CONFIG;
|
||||
use crate::config::VersionedConfig;
|
||||
|
||||
pub struct PageAnalyzer {
|
||||
info: serde_json::Value,
|
||||
@@ -136,7 +136,7 @@ impl Stream {
|
||||
let mut urls = std::iter::once(url.as_str())
|
||||
.chain(backup_url.iter().map(|s| s.as_str()))
|
||||
.collect::<Vec<_>>();
|
||||
if CONFIG.cdn_sorting {
|
||||
if VersionedConfig::get().load().cdn_sorting {
|
||||
urls.sort_by_key(|u| {
|
||||
if u.contains("upos-") {
|
||||
0 // 服务商 cdn
|
||||
@@ -351,7 +351,7 @@ impl PageAnalyzer {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::bilibili::{BiliClient, Video};
|
||||
use crate::config::CONFIG;
|
||||
use crate::config::VersionedConfig;
|
||||
|
||||
#[test]
|
||||
fn test_quality_order() {
|
||||
@@ -433,7 +433,7 @@ mod tests {
|
||||
.get_page_analyzer(&first_page)
|
||||
.await
|
||||
.expect("failed to get page analyzer")
|
||||
.best_stream(&CONFIG.filter_option)
|
||||
.best_stream(&VersionedConfig::get().load().filter_option)
|
||||
.expect("failed to get best stream");
|
||||
dbg!(bvid, &best_stream);
|
||||
match best_stream {
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use anyhow::Result;
|
||||
use leaky_bucket::RateLimiter;
|
||||
use reqwest::{Method, header};
|
||||
use sea_orm::DatabaseConnection;
|
||||
|
||||
use crate::bilibili::Credential;
|
||||
use crate::bilibili::credential::WbiImg;
|
||||
use crate::config::{CONFIG, RateLimit};
|
||||
use crate::config::{RateLimit, VersionedCache, VersionedConfig};
|
||||
|
||||
// 一个对 reqwest::Client 的简单封装,用于 Bilibili 请求
|
||||
#[derive(Clone)]
|
||||
@@ -63,53 +64,54 @@ impl Default for Client {
|
||||
|
||||
pub struct BiliClient {
|
||||
pub client: Client,
|
||||
limiter: Option<RateLimiter>,
|
||||
limiter: VersionedCache<Option<RateLimiter>>,
|
||||
}
|
||||
|
||||
impl BiliClient {
|
||||
pub fn new() -> Self {
|
||||
let client = Client::new();
|
||||
let limiter = CONFIG
|
||||
.concurrent_limit
|
||||
.rate_limit
|
||||
.as_ref()
|
||||
.map(|RateLimit { limit, duration }| {
|
||||
RateLimiter::builder()
|
||||
.initial(*limit)
|
||||
.refill(*limit)
|
||||
.max(*limit)
|
||||
.interval(Duration::from_millis(*duration))
|
||||
.build()
|
||||
});
|
||||
let limiter = VersionedCache::new(|config| {
|
||||
Ok(config
|
||||
.concurrent_limit
|
||||
.rate_limit
|
||||
.as_ref()
|
||||
.map(|RateLimit { limit, duration }| {
|
||||
RateLimiter::builder()
|
||||
.initial(*limit)
|
||||
.refill(*limit)
|
||||
.max(*limit)
|
||||
.interval(Duration::from_millis(*duration))
|
||||
.build()
|
||||
}))
|
||||
})
|
||||
.expect("failed to create rate limiter");
|
||||
Self { client, limiter }
|
||||
}
|
||||
|
||||
/// 获取一个预构建的请求,通过该方法获取请求时会检查并等待速率限制
|
||||
pub async fn request(&self, method: Method, url: &str) -> reqwest::RequestBuilder {
|
||||
if let Some(limiter) = &self.limiter {
|
||||
if let Some(limiter) = self.limiter.load().as_ref() {
|
||||
limiter.acquire_one().await;
|
||||
}
|
||||
let credential = CONFIG.credential.load();
|
||||
self.client.request(method, url, credential.as_deref())
|
||||
let credential = VersionedConfig::get().load().credential.load();
|
||||
self.client.request(method, url, Some(credential.as_ref()))
|
||||
}
|
||||
|
||||
pub async fn check_refresh(&self) -> Result<()> {
|
||||
let credential = CONFIG.credential.load();
|
||||
let Some(credential) = credential.as_deref() else {
|
||||
return Ok(());
|
||||
};
|
||||
pub async fn check_refresh(&self, connection: &DatabaseConnection) -> Result<()> {
|
||||
let credential = VersionedConfig::get().load().credential.load();
|
||||
if !credential.need_refresh(&self.client).await? {
|
||||
return Ok(());
|
||||
}
|
||||
let new_credential = credential.refresh(&self.client).await?;
|
||||
CONFIG.credential.store(Some(Arc::new(new_credential)));
|
||||
CONFIG.save()
|
||||
let config = VersionedConfig::get().load();
|
||||
config.credential.store(Arc::new(new_credential));
|
||||
config.save_to_database(connection).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 获取 wbi img,用于生成请求签名
|
||||
pub async fn wbi_img(&self) -> Result<WbiImg> {
|
||||
let credential = CONFIG.credential.load();
|
||||
let credential = credential.as_deref().context("no credential found")?;
|
||||
let credential = VersionedConfig::get().load().credential.load();
|
||||
credential.wbi_img(&self.client).await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ use serde_json::Value;
|
||||
use crate::bilibili::credential::encoded_query;
|
||||
use crate::bilibili::{BiliClient, MIXIN_KEY, Validate, VideoInfo};
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Debug, Deserialize, Default)]
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Debug, Deserialize, Default, Copy)]
|
||||
pub enum CollectionType {
|
||||
Series,
|
||||
#[default]
|
||||
@@ -55,7 +55,7 @@ pub struct CollectionItem {
|
||||
|
||||
pub struct Collection<'a> {
|
||||
client: &'a BiliClient,
|
||||
collection: &'a CollectionItem,
|
||||
collection: CollectionItem,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
@@ -94,7 +94,7 @@ impl<'de> Deserialize<'de> for CollectionInfo {
|
||||
}
|
||||
|
||||
impl<'a> Collection<'a> {
|
||||
pub fn new(client: &'a BiliClient, collection: &'a CollectionItem) -> Self {
|
||||
pub fn new(client: &'a BiliClient, collection: CollectionItem) -> Self {
|
||||
Self { client, collection }
|
||||
}
|
||||
|
||||
|
||||
@@ -88,14 +88,14 @@ impl fmt::Display for CanvasStyles {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AssWriter<W: AsyncWrite> {
|
||||
pub struct AssWriter<'a, W: AsyncWrite> {
|
||||
f: Pin<Box<BufWriter<W>>>,
|
||||
title: String,
|
||||
canvas_config: CanvasConfig,
|
||||
canvas_config: CanvasConfig<'a>,
|
||||
}
|
||||
|
||||
impl<W: AsyncWrite> AssWriter<W> {
|
||||
pub fn new(f: W, title: String, canvas_config: CanvasConfig) -> Self {
|
||||
impl<'a, W: AsyncWrite> AssWriter<'a, W> {
|
||||
pub fn new(f: W, title: String, canvas_config: CanvasConfig<'a>) -> Self {
|
||||
AssWriter {
|
||||
// 对于 HDD、docker 之类的场景,磁盘 IO 是非常大的瓶颈。使用大缓存
|
||||
f: Box::pin(BufWriter::with_capacity(10 << 20, f)),
|
||||
@@ -104,7 +104,7 @@ impl<W: AsyncWrite> AssWriter<W> {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn construct(f: W, title: String, canvas_config: CanvasConfig) -> Result<Self> {
|
||||
pub async fn construct(f: W, title: String, canvas_config: CanvasConfig<'a>) -> Result<Self> {
|
||||
let mut res = Self::new(f, title, canvas_config);
|
||||
res.init().await?;
|
||||
Ok(res)
|
||||
|
||||
@@ -18,7 +18,7 @@ pub struct Lane {
|
||||
}
|
||||
|
||||
impl Lane {
|
||||
pub fn draw(danmu: &Danmu, config: &CanvasConfig) -> Self {
|
||||
pub fn draw(danmu: &Danmu, config: &CanvasConfig<'_>) -> Self {
|
||||
Lane {
|
||||
last_shoot_time: danmu.timeline_s,
|
||||
last_length: danmu.length(config),
|
||||
@@ -26,7 +26,7 @@ impl Lane {
|
||||
}
|
||||
|
||||
/// 这个槽位是否可以发射另外一条弹幕,返回可能的情形
|
||||
pub fn available_for(&self, other: &Danmu, config: &CanvasConfig) -> Collision {
|
||||
pub fn available_for(&self, other: &Danmu, config: &CanvasConfig<'_>) -> Collision {
|
||||
#[allow(non_snake_case)]
|
||||
let T = config.danmaku_option.duration;
|
||||
#[allow(non_snake_case)]
|
||||
|
||||
@@ -10,7 +10,7 @@ use crate::bilibili::danmaku::canvas::lane::Collision;
|
||||
use crate::bilibili::danmaku::danmu::DanmuType;
|
||||
use crate::bilibili::danmaku::{Danmu, DrawEffect, Drawable};
|
||||
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
|
||||
pub struct DanmakuOption {
|
||||
pub duration: f64,
|
||||
pub font: String,
|
||||
@@ -54,13 +54,13 @@ impl Default for DanmakuOption {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CanvasConfig {
|
||||
pub struct CanvasConfig<'a> {
|
||||
pub width: u64,
|
||||
pub height: u64,
|
||||
pub danmaku_option: &'static DanmakuOption,
|
||||
pub danmaku_option: &'a DanmakuOption,
|
||||
}
|
||||
impl CanvasConfig {
|
||||
pub fn new(danmaku_option: &'static DanmakuOption, page: &PageInfo) -> Self {
|
||||
impl<'a> CanvasConfig<'a> {
|
||||
pub fn new(danmaku_option: &'a DanmakuOption, page: &PageInfo) -> Self {
|
||||
let (width, height) = Self::dimension(page);
|
||||
Self {
|
||||
width,
|
||||
@@ -86,7 +86,7 @@ impl CanvasConfig {
|
||||
((720.0 / height as f64 * width as f64) as u64, 720)
|
||||
}
|
||||
|
||||
pub fn canvas(self) -> Canvas {
|
||||
pub fn canvas(self) -> Canvas<'a> {
|
||||
let float_lanes_cnt =
|
||||
(self.danmaku_option.float_percentage * self.height as f64 / self.danmaku_option.lane_size as f64) as usize;
|
||||
|
||||
@@ -97,12 +97,12 @@ impl CanvasConfig {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Canvas {
|
||||
pub config: CanvasConfig,
|
||||
pub struct Canvas<'a> {
|
||||
pub config: CanvasConfig<'a>,
|
||||
pub float_lanes: Vec<Option<Lane>>,
|
||||
}
|
||||
|
||||
impl Canvas {
|
||||
impl<'a> Canvas<'a> {
|
||||
pub fn draw(&mut self, mut danmu: Danmu) -> Result<Option<Drawable>> {
|
||||
danmu.timeline_s += self.config.danmaku_option.time_offset;
|
||||
if danmu.timeline_s < 0.0 {
|
||||
|
||||
@@ -40,7 +40,7 @@ impl Danmu {
|
||||
/// 计算弹幕的“像素长度”,会乘上一个缩放因子
|
||||
///
|
||||
/// 汉字算一个全宽,英文算2/3宽
|
||||
pub fn length(&self, config: &CanvasConfig) -> f64 {
|
||||
pub fn length(&self, config: &CanvasConfig<'_>) -> f64 {
|
||||
let pts = config.danmaku_option.font_size
|
||||
* self
|
||||
.content
|
||||
|
||||
@@ -6,7 +6,7 @@ use tokio::fs::{self, File};
|
||||
use crate::bilibili::PageInfo;
|
||||
use crate::bilibili::danmaku::canvas::CanvasConfig;
|
||||
use crate::bilibili::danmaku::{AssWriter, Danmu};
|
||||
use crate::config::CONFIG;
|
||||
use crate::config::VersionedConfig;
|
||||
|
||||
pub struct DanmakuWriter<'a> {
|
||||
page: &'a PageInfo,
|
||||
@@ -22,7 +22,8 @@ impl<'a> DanmakuWriter<'a> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).await?;
|
||||
}
|
||||
let canvas_config = CanvasConfig::new(&CONFIG.danmaku_option, self.page);
|
||||
let config = VersionedConfig::get().load_full();
|
||||
let canvas_config = CanvasConfig::new(&config.danmaku_option, self.page);
|
||||
let mut writer =
|
||||
AssWriter::construct(File::create(path).await?, self.page.name.clone(), canvas_config.clone()).await?;
|
||||
let mut canvas = canvas_config.canvas();
|
||||
|
||||
@@ -4,7 +4,7 @@ use anyhow::Result;
|
||||
use reqwest::Method;
|
||||
|
||||
use crate::bilibili::{BiliClient, Validate};
|
||||
use crate::config::CONFIG;
|
||||
use crate::config::VersionedConfig;
|
||||
pub struct Me<'a> {
|
||||
client: &'a BiliClient,
|
||||
mid: String,
|
||||
@@ -73,12 +73,7 @@ impl<'a> Me<'a> {
|
||||
}
|
||||
|
||||
fn my_id() -> String {
|
||||
CONFIG
|
||||
.credential
|
||||
.load()
|
||||
.as_deref()
|
||||
.map(|c| c.dedeuserid.clone())
|
||||
.unwrap()
|
||||
VersionedConfig::get().load().credential.load().dedeuserid.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -154,13 +154,14 @@ mod tests {
|
||||
panic!("获取 mixin key 失败");
|
||||
};
|
||||
set_global_mixin_key(mixin_key);
|
||||
// 测试视频合集
|
||||
let collection_item = CollectionItem {
|
||||
mid: "521722088".to_string(),
|
||||
sid: "4523".to_string(),
|
||||
collection_type: CollectionType::Season,
|
||||
};
|
||||
let collection = Collection::new(&bili_client, &collection_item);
|
||||
let collection = Collection::new(
|
||||
&bili_client,
|
||||
CollectionItem {
|
||||
mid: "521722088".to_string(),
|
||||
sid: "4523".to_string(),
|
||||
collection_type: CollectionType::Season,
|
||||
},
|
||||
);
|
||||
let videos = collection
|
||||
.into_video_stream()
|
||||
.take(20)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use anyhow::{Context, Result, anyhow};
|
||||
use arc_swap::access::Access;
|
||||
use async_stream::try_stream;
|
||||
use futures::Stream;
|
||||
use reqwest::Method;
|
||||
|
||||
@@ -68,7 +68,7 @@ impl<'a> Video<'a> {
|
||||
Ok(serde_json::from_value(res["data"].take())?)
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
#[allow(dead_code)]
|
||||
pub async fn get_pages(&self) -> Result<Vec<PageInfo>> {
|
||||
let mut res = self
|
||||
.client
|
||||
|
||||
Reference in New Issue
Block a user