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, pool: DbPool,
mut requester: Requester, mut requester: Requester,
) -> Result<VideoItem> { ) -> Result<VideoItem> {
// 1. Database Check // 1. Attempt live fetch
{ let fetched = self.fetch_video_item(&url_str, &mut requester).await;
let mut conn = pool
.get() match fetched {
.map_err(|e| Error::from(format!("Pool error: {}", e)))?; Ok(video_item) => {
if let Ok(Some(entry)) = db::get_video(&mut conn, url_str.clone()) { // Store/override in DB
if let Ok(video_item) = serde_json::from_str::<VideoItem>(entry.as_str()) { if let Ok(mut conn) = pool.get() {
return Ok(video_item); 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 let vid = requester
.get(&url_str, Some(Version::HTTP_2)) .get(url_str, Some(Version::HTTP_2))
.await .await
.unwrap_or_else(|e| { .map_err(|e| {
eprintln!("Error fetching Missav URL {}: {}", url_str, e); eprintln!("Error fetching Missav URL {}: {}", url_str, e);
let _ = send_discord_error_report( let _ = send_discord_error_report(
e.to_string(), e.to_string(),
None, None,
Some(&url_str), Some(url_str),
None, None,
file!(), file!(),
line!(), line!(),
module_path!(), module_path!(),
); );
"".to_string() Error::from(e.to_string())
}); })?;
// Helper closure to extract content between two strings // Helper closure to extract content between two strings
let extract = |html: &str, start_tag: &str, end_tag: &str| -> Option<String> { 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(); 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![]; let mut tags = vec![];
for (label, route_kind) in [ for (label, route_kind) in [
("Actress:", "actress"), ("Actress:", "actress"),
@@ -490,7 +512,7 @@ impl MissavProvider {
} }
} }
// 4. Extract Video URL (The m3u8 logic) // Extract Video URL (m3u8)
let video_url = (|| { let video_url = (|| {
let parts_str = vid.split("m3u8").nth(1)?.split("https").next()?; let parts_str = vid.split("m3u8").nth(1)?.split("https").next()?;
let mut parts: Vec<&str> = parts_str.split('|').collect(); 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()); 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()); 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]) .formats(vec![format])
.tags(tags) .tags(tags)
.preview(format!( .preview(format!(
@@ -518,15 +540,6 @@ impl MissavProvider {
url_str.split('/').last().unwrap_or_default() 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) Ok(video_item)
} }
} }