provider refactors and fixes

This commit is contained in:
Simon
2026-03-05 13:28:38 +00:00
parent 060d8e7937
commit 8157e223fe
33 changed files with 3051 additions and 1694 deletions

View File

@@ -5,7 +5,7 @@ use crate::status::*;
use crate::util::cache::VideoCache;
use crate::util::discord::send_discord_error_report;
use crate::util::time::parse_time_to_seconds;
use crate::videos::{ServerOptions, VideoItem};
use crate::videos::{ServerOptions, VideoFormat, VideoItem};
use async_trait::async_trait;
use error_chain::error_chain;
use htmlentity::entity::{decode, ICodedDataTrait};
@@ -132,6 +132,40 @@ impl PmvhavenProvider {
}
}
fn is_direct_media_url(url: &str) -> bool {
let lower = url.to_ascii_lowercase();
(lower.starts_with("http://") || lower.starts_with("https://"))
&& (lower.contains("/videos/")
|| lower.contains(".mp4")
|| lower.contains(".m3u8"))
}
fn pick_downloadable_media_url(&self, video: &serde_json::Value) -> Option<String> {
let video_url = video
.get("videoUrl")
.and_then(|v| v.as_str())
.unwrap_or("")
.trim();
if Self::is_direct_media_url(video_url) {
return Some(video_url.replace(' ', "%20"));
}
// Fallback: derive direct media URL from object key.
let key = video
.get("key")
.and_then(|v| v.as_str())
.unwrap_or("")
.trim_matches('/');
if !key.is_empty() {
let rebuilt = format!("https://video.pmvhaven.com/{key}");
if Self::is_direct_media_url(&rebuilt) {
return Some(rebuilt.replace(' ', "%20"));
}
}
None
}
async fn query(
&self,
cache: VideoCache,
@@ -226,7 +260,12 @@ impl PmvhavenProvider {
.unwrap_or(&title)
.to_string();
let video_url = video.get("videoUrl").and_then(|v| v.as_str()).unwrap_or("").to_string();
let video_url = match self.pick_downloadable_media_url(&video) {
Some(url) => url,
None => {
continue;
}
};
let thumb = video.get("thumbnailUrl").and_then(|v| v.as_str()).unwrap_or("").to_string();
let preview = video.get("previewUrl").and_then(|v| v.as_str()).unwrap_or("").to_string();
@@ -248,9 +287,19 @@ impl PmvhavenProvider {
}
}
let format_type = if video_url.to_ascii_lowercase().contains(".m3u8") {
"m3u8".to_string()
} else {
"mp4".to_string()
};
items.push(
VideoItem::new(id, title, video_url.replace(' ', "%20"), "pmvhaven".into(), thumb, duration as u32)
VideoItem::new(id, title, video_url.clone(), "pmvhaven".into(), thumb, duration as u32)
.views(views as u32)
.formats(vec![VideoFormat::new(
video_url,
"1080".to_string(),
format_type,
)])
.preview(preview)
);
}