Refactor code for conciseness and clarity

Simplified conditional logic using Rust's let-else syntax, removed unused imports, and improved code readability in extractor.rs, lib.rs, uploader/line.rs, and stream-gears/server.rs. These changes reduce code complexity and improve maintainability.
This commit is contained in:
ForgQi committed 2025-12-15 00:43:24 +08:00
1 parent b57dc29e96
commit 218b8bd127
4 files changed
+12 -17

No files matched your search

+1 -1
View File
@@ -58,7 +58,7 @@ impl Site {
&mut self,
fmt_file_name: &str,
segment: Segmentable,
hook: Option<CallbackFn<'_>>,
_hook: Option<CallbackFn<'_>>,
) -> downloader::error::Result<()> {
let fmt_file_name = fmt_file_name.replace("{title}", &self.title);
self.client
+3 -4
View File
@@ -12,7 +12,7 @@ pub mod uploader;
pub use uploader::bilibili;
pub use uploader::credential;
pub async fn retry<F, Fut, O, E: std::fmt::Display>(mut f: F) -> Result<O, E>
pub async fn retry<F, Fut, O, E: std::fmt::Display>(f: F) -> Result<O, E>
where
F: FnMut() -> Fut,
Fut: Future<Output = Result<O, E>>,
@@ -38,11 +38,10 @@ where
match f().await {
Err(e) if retries > 0 => {
// 如果提供了 should_retry 条件,检查是否应该重试
if let Some(ref predicate) = should_retry {
if !predicate(&e) {
if let Some(ref predicate) = should_retry
&& !predicate(&e) {
break Err(e);
}
}
retries -= 1;
let jitter_factor =
+7 -10
View File
@@ -12,7 +12,7 @@ use crate::error::Kind::{Custom, RateLimit};
use crate::uploader::bilibili::{BiliBili, Video};
use crate::uploader::line::upos::Upos;
use std::time::Instant;
use tracing::{info, warn};
use tracing::info;
pub mod upos;
@@ -56,16 +56,15 @@ impl Parcel {
}
};
if video.title.is_none() {
if let Some(filename) = self.video_file.filepath.file_stem().and_then(OsStr::to_str) {
if video.title.is_none()
&& let Some(filename) = self.video_file.filepath.file_stem().and_then(OsStr::to_str) {
// B站限制分P视频标题不能超过80字符,需要截断
video.title = Some(if filename.chars().count() >= 80 {
Video::truncate_title(filename, 80)
} else {
filename.to_string()
});
}
};
};
Ok(video)
}
}
@@ -163,9 +162,9 @@ impl Line {
let response_text = response.text().await?;
// 尝试解析JSON错误响应,检测限流错误(code: 601)
if let Ok(error_json) = serde_json::from_str::<serde_json::Value>(&response_text) {
if let Some(code) = error_json.get("code").and_then(|c| c.as_i64()) {
if code == 601 {
if let Ok(error_json) = serde_json::from_str::<serde_json::Value>(&response_text)
&& let Some(code) = error_json.get("code").and_then(|c| c.as_i64())
&& code == 601 {
let message = error_json
.get("message")
.and_then(|m| m.as_str())
@@ -174,8 +173,6 @@ impl Line {
// 直接返回限流错误,让调用方决定如何处理
return Err(RateLimit { code, message });
}
}
}
return Err(Custom(format!(
"Failed to pre_upload from {}",
+1 -2
View File
@@ -11,7 +11,7 @@ use biliup_cli::server::core::downloader::DanmakuClient;
use biliup_cli::server::core::plugin::{DownloadBase, DownloadPlugin, StreamInfoExt, StreamStatus};
use biliup_cli::server::errors::{AppError, AppResult};
use biliup_cli::server::infrastructure::connection_pool::ConnectionManager;
use biliup_cli::server::infrastructure::context::{Context, PluginContext, Worker};
use biliup_cli::server::infrastructure::context::{PluginContext, Worker};
use biliup_cli::server::infrastructure::models::StreamerInfo;
use biliup_cli::server::infrastructure::repositories;
use biliup_cli::server::infrastructure::repositories::get_upload_config;
@@ -31,7 +31,6 @@ use pythonize::pythonize;
use std::collections::HashMap;
use std::net::ToSocketAddrs;
use std::ops::Deref;
use std::path::PathBuf;
use std::sync::{Arc, LazyLock, RwLock};
use time::macros::format_description;
use tracing::{debug, info, warn};