fixes and cleanup

This commit is contained in:
Simon
2026-03-05 18:18:48 +00:00
parent 76fd5a4f4f
commit 2627505ade
49 changed files with 3245 additions and 1376 deletions

View File

@@ -45,7 +45,7 @@ impl VideoCache {
cache.remove(key);
}
}
pub fn entries(&self) -> Option<Vec<(String, (SystemTime, Vec<VideoItem>))>> {
if let Ok(cache) = self.cache.lock() {
// Return a cloned vector of the cache entries

View File

@@ -1,10 +1,10 @@
use std::error::Error;
use std::fmt::Write as _;
use std::time::{SystemTime, UNIX_EPOCH};
use crate::util::requester;
use dashmap::DashMap;
use once_cell::sync::Lazy;
use serde_json::json;
use crate::util::requester;
use std::error::Error;
use std::fmt::Write as _;
use std::time::{SystemTime, UNIX_EPOCH};
// Global cache: Map<ErrorSignature, LastSentTimestamp>
static ERROR_CACHE: Lazy<DashMap<String, u64>> = Lazy::new(DashMap::new);
@@ -42,11 +42,11 @@ pub async fn send_discord_error_report(
if let Some(_) = ERROR_CACHE.get(&error_signature) {
// if now - *last_sent < COOLDOWN_SECONDS {
// Error is still in cooldown, skip sending
return;
// Error is still in cooldown, skip sending
return;
// }
}
// Update the cache with the current timestamp
ERROR_CACHE.insert(error_signature, now);
// ---------------------------
@@ -104,4 +104,4 @@ pub async fn send_discord_error_report(
let mut requester = requester::Requester::new();
let _ = requester.post_json(&webhook_url, &payload, vec![]).await;
}
}

View File

@@ -57,10 +57,7 @@ pub struct Flaresolverr {
impl Flaresolverr {
pub fn new(url: String) -> Self {
Self {
url,
proxy: false,
}
Self { url, proxy: false }
}
pub fn set_proxy(&mut self, proxy: bool) {
@@ -71,9 +68,7 @@ impl Flaresolverr {
&self,
request: FlareSolverrRequest,
) -> Result<FlareSolverrResponse, Box<dyn std::error::Error>> {
let client = Client::builder()
.emulation(Emulation::Firefox136)
.build()?;
let client = Client::builder().emulation(Emulation::Firefox136).build()?;
let mut req = client
.post(&self.url)

View File

@@ -1,9 +1,9 @@
pub mod time;
pub mod flaresolverr;
pub mod cache;
pub mod requester;
pub mod discord;
pub mod flaresolverr;
pub mod proxy;
pub mod requester;
pub mod time;
pub fn parse_abbreviated_number(s: &str) -> Option<u32> {
let s = s.trim();
@@ -20,7 +20,10 @@ pub fn parse_abbreviated_number(s: &str) -> Option<u32> {
"" => 1.0,
_ => return None,
};
num_part.parse::<f64>().ok().map(|n| (n * multiplier) as u32)
num_part
.parse::<f64>()
.ok()
.map(|n| (n * multiplier) as u32)
}
pub fn interleave<T: Clone>(lists: &[Vec<T>]) -> Vec<T> {

View File

@@ -1,6 +1,6 @@
use std::fmt;
use std::sync::Arc;
use std::str::FromStr;
use std::sync::Arc;
use serde::Serialize;
use tokio::sync::{OnceCell, RwLock};
@@ -71,11 +71,7 @@ impl fmt::Display for Proxy {
"{}://{}@{}:{}",
self.protocol, username, self.host, self.port
),
(None, Some(_)) => write!(
f,
"{}://{}:{}",
self.protocol, self.host, self.port
),
(None, Some(_)) => write!(f, "{}://{}:{}", self.protocol, self.host, self.port),
(None, None) => write!(f, "{}://{}:{}", self.protocol, self.host, self.port),
}
}
@@ -224,8 +220,7 @@ pub fn init_all_proxies_background(requester: Requester) {
for list in PROXY_LIST {
let proxy_cache = proxy_cache.clone();
let mut requester = requester.clone();
tasks
.spawn(async move { fetch_proxies(&mut requester, list, proxy_cache).await });
tasks.spawn(async move { fetch_proxies(&mut requester, list, proxy_cache).await });
}
while let Some(result) = tasks.join_next().await {
@@ -278,9 +273,7 @@ impl FromStr for Proxy {
.ok_or_else(|| ProxyParseError::new("proxy url is missing host"))?
.to_string();
let port = url
.port()
.unwrap_or(80);
let port = url.port().unwrap_or(80);
Ok(Proxy {
protocol: url.scheme().to_string(),

View File

@@ -1,17 +1,17 @@
use serde::Serialize;
use wreq::multipart::Form;
use std::env;
use wreq::Client;
use wreq::Proxy;
use wreq::Response;
use wreq::Version;
use wreq::header::HeaderValue;
use wreq::multipart::Form;
use wreq::redirect::Policy;
use wreq_util::Emulation;
use crate::util::proxy;
use crate::util::flaresolverr::FlareSolverrRequest;
use crate::util::flaresolverr::Flaresolverr;
use crate::util::proxy;
// A Send + Sync error type for all async paths
type AnyErr = Box<dyn std::error::Error + Send + Sync + 'static>;
@@ -99,7 +99,7 @@ impl Requester {
request.send().await
}
pub async fn post_json<S>(
pub async fn post_json<S>(
&mut self,
url: &str,
data: &S,
@@ -131,7 +131,11 @@ pub async fn post_json<S>(
data: &str,
headers: Vec<(&str, &str)>,
) -> Result<Response, wreq::Error> {
let mut request = self.client.post(url).version(Version::HTTP_11).body(data.to_string());
let mut request = self
.client
.post(url)
.version(Version::HTTP_11)
.body(data.to_string());
// Set custom headers
for (key, value) in headers.iter() {
@@ -154,8 +158,7 @@ pub async fn post_json<S>(
form: Form,
headers: Vec<(String, String)>,
_http_version: Option<Version>,
) -> Result<Response, wreq::Error>
{
) -> Result<Response, wreq::Error> {
let http_version = match _http_version {
Some(v) => v,
None => Version::HTTP_11,
@@ -178,7 +181,11 @@ pub async fn post_json<S>(
request.send().await
}
pub async fn get(&mut self, url: &str, _http_version: Option<Version>) -> Result<String, AnyErr> {
pub async fn get(
&mut self,
url: &str,
_http_version: Option<Version>,
) -> Result<String, AnyErr> {
let http_version = match _http_version {
Some(v) => v,
None => Version::HTTP_11,
@@ -190,7 +197,7 @@ pub async fn post_json<S>(
let proxy = Proxy::all(&proxy_url).unwrap();
request = request.proxy(proxy);
}
}
}
let response = request.send().await?;
if response.status().is_success() || response.status().as_u16() == 404 {
return Ok(response.text().await?);
@@ -208,7 +215,6 @@ pub async fn post_json<S>(
}
}
// If direct request failed, try FlareSolverr. Map its error to a Send+Sync error immediately,
// so no non-Send error value lives across later `.await`s.
let flare_url = match env::var("FLARE_URL") {