mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-10-08 01:42:05 +00:00
refactor(x-media): drop the per-site fns the Site defaults already cover
twitter, bsky, misskey and bilibili each carried enabled() -> true, media_headers(url) -> None and is_retryable(err) -> the trait's own default, with no caller outside their tests (the adapters never override those methods, so the default was already the production policy). The tests that only restated the default are gone; the two that pin site-specific classes (bsky's MediaPrep, bilibili's risk-control codes) now ask the Site impl, and site/mod.rs keeps one assertion of the shared retry policy. The live verification notes (no Referer needed for hdslb/twimg) survive as comments.
This commit is contained in:
@@ -111,10 +111,6 @@ pub static PATTERN: LazyLock<Regex> = LazyLock::new(|| {
|
||||
.unwrap()
|
||||
});
|
||||
|
||||
pub fn enabled() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
pub async fn fetch_from_url(url: &str) -> Result<Fetched, FetchError> {
|
||||
let dynamic_id = PATTERN
|
||||
.captures(url)
|
||||
@@ -133,18 +129,9 @@ pub fn cache_key(url: &str) -> Option<String> {
|
||||
.map(|caps| format!("bilibili:{}", &caps[1]))
|
||||
}
|
||||
|
||||
/// Bilibili's fetch-retry policy: transient classes only. Not-found, blocked
|
||||
/// and parse failures are permanent.
|
||||
pub fn is_retryable(err: &FetchError) -> bool {
|
||||
matches!(err, FetchError::Http(_) | FetchError::Transient(_))
|
||||
}
|
||||
|
||||
/// hdslb media serves without a `Referer` (verified live 2026-09-17 on
|
||||
/// `i0.hdslb.com` image URLs, requested both with and without one), so no
|
||||
/// extra headers.
|
||||
pub fn media_headers(_url: &str) -> Option<Vec<(&'static str, String)>> {
|
||||
None
|
||||
}
|
||||
// hdslb media serves without a `Referer` (verified live 2026-09-17 on
|
||||
// `i0.hdslb.com` image URLs, requested both with and without one), so this
|
||||
// adapter does not override `Site::media_headers`.
|
||||
|
||||
/// `Cookie` header for bilibili requests: the operator's `BILIBILI_COOKIE`
|
||||
/// when set, otherwise the anonymous device cookies.
|
||||
@@ -908,7 +895,7 @@ mod tests {
|
||||
// dropping the post.
|
||||
for code in [-352, -412] {
|
||||
let err = code_error(code, "-352").unwrap();
|
||||
assert!(is_retryable(&err), "{err}");
|
||||
assert!(BilibiliSite.is_retryable(&err), "{err}");
|
||||
}
|
||||
// A removed dynamic is permanent.
|
||||
assert!(matches!(code_error(500, ""), Some(FetchError::NotFound)));
|
||||
@@ -917,7 +904,7 @@ mod tests {
|
||||
Some(FetchError::NotFound)
|
||||
));
|
||||
let err = code_error(-400, "param parsing failed").unwrap();
|
||||
assert!(!is_retryable(&err), "{err}");
|
||||
assert!(!BilibiliSite.is_retryable(&err), "{err}");
|
||||
assert!(err.to_string().contains("-400"), "{err}");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user