missav fetch first db fallback

This commit is contained in:
Simon
2026-05-18 20:39:45 +00:00
committed by ForgeCode
parent d15b49b1cf
commit b077b24d63

View File

@@ -375,35 +375,57 @@ impl MissavProvider {
pool: DbPool,
mut requester: Requester,
) -> Result<VideoItem> {
// 1. Database Check
{
let mut conn = pool
.get()
.map_err(|e| Error::from(format!("Pool error: {}", e)))?;
if let Ok(Some(entry)) = db::get_video(&mut conn, url_str.clone()) {
if let Ok(video_item) = serde_json::from_str::<VideoItem>(entry.as_str()) {
return Ok(video_item);
// 1. Attempt live fetch
let fetched = self.fetch_video_item(&url_str, &mut requester).await;
match fetched {
Ok(video_item) => {
// Store/override in DB
if let Ok(mut conn) = pool.get() {
let _ = db::insert_video(
&mut conn,
&url_str,
&serde_json::to_string(&video_item).unwrap_or_default(),
);
}
Ok(video_item)
}
Err(e) => {
// 2. Fall back to DB
if let Ok(mut conn) = pool.get() {
if let Ok(Some(entry)) = db::get_video(&mut conn, url_str.clone()) {
if let Ok(mut video_item) = serde_json::from_str::<VideoItem>(entry.as_str()) {
video_item.url = url_str.clone();
return Ok(video_item);
}
}
}
Err(e)
}
}
}
// 2. Fetch Page
async fn fetch_video_item(
&self,
url_str: &str,
requester: &mut Requester,
) -> Result<VideoItem> {
let vid = requester
.get(&url_str, Some(Version::HTTP_2))
.get(url_str, Some(Version::HTTP_2))
.await
.unwrap_or_else(|e| {
.map_err(|e| {
eprintln!("Error fetching Missav URL {}: {}", url_str, e);
let _ = send_discord_error_report(
e.to_string(),
None,
Some(&url_str),
Some(url_str),
None,
file!(),
line!(),
module_path!(),
);
"".to_string()
});
Error::from(e.to_string())
})?;
// Helper closure to extract content between two strings
let extract = |html: &str, start_tag: &str, end_tag: &str| -> Option<String> {
@@ -434,7 +456,7 @@ impl MissavProvider {
let id = url_str.split('/').last().ok_or("No ID found")?.to_string();
// 3. Extract Tags (Generic approach to avoid repetitive code)
// Extract Tags
let mut tags = vec![];
for (label, route_kind) in [
("Actress:", "actress"),
@@ -490,7 +512,7 @@ impl MissavProvider {
}
}
// 4. Extract Video URL (The m3u8 logic)
// Extract Video URL (m3u8)
let video_url = (|| {
let parts_str = vid.split("m3u8").nth(1)?.split("https").next()?;
let mut parts: Vec<&str> = parts_str.split('|').collect();
@@ -510,7 +532,7 @@ impl MissavProvider {
let mut format = VideoFormat::new(video_url.clone(), "auto".to_string(), "m3u8".to_string());
format.add_http_header("Referer".to_string(), "https://missav.ws/".to_string());
let video_item = VideoItem::new(id, title, url_str.clone(), "missav".to_string(), thumb, duration)
let video_item = VideoItem::new(id, title, url_str.to_string(), "missav".to_string(), thumb, duration)
.formats(vec![format])
.tags(tags)
.preview(format!(
@@ -518,15 +540,6 @@ impl MissavProvider {
url_str.split('/').last().unwrap_or_default()
));
// 5. Cache to DB
if let Ok(mut conn) = pool.get() {
let _ = db::insert_video(
&mut conn,
&url_str,
&serde_json::to_string(&video_item).unwrap_or_default(),
);
}
Ok(video_item)
}
}