fixes and cleanup
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
use std::vec;
|
||||
use async_trait::async_trait;
|
||||
use diesel::r2d2;
|
||||
use error_chain::error_chain;
|
||||
use htmlentity::entity::{decode, ICodedDataTrait};
|
||||
use futures::future::join_all;
|
||||
use wreq::Version;
|
||||
use crate::DbPool;
|
||||
use crate::api::ClientVersion;
|
||||
use crate::db;
|
||||
use crate::providers::Provider;
|
||||
use crate::status::*;
|
||||
use crate::util::cache::VideoCache;
|
||||
use crate::util::discord::{format_error_chain, send_discord_error_report};
|
||||
use crate::videos::ServerOptions;
|
||||
use crate::videos::{VideoItem};
|
||||
use crate::DbPool;
|
||||
use crate::util::requester::Requester;
|
||||
use crate::videos::ServerOptions;
|
||||
use crate::videos::VideoItem;
|
||||
use async_trait::async_trait;
|
||||
use diesel::r2d2;
|
||||
use error_chain::error_chain;
|
||||
use futures::future::join_all;
|
||||
use htmlentity::entity::{ICodedDataTrait, decode};
|
||||
use std::vec;
|
||||
use wreq::Version;
|
||||
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
@@ -39,7 +39,7 @@ pub struct MissavProvider {
|
||||
impl MissavProvider {
|
||||
pub fn new() -> Self {
|
||||
MissavProvider {
|
||||
url: "https://missav.ws".to_string()
|
||||
url: "https://missav.ws".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,7 +177,14 @@ impl MissavProvider {
|
||||
}
|
||||
}
|
||||
|
||||
async fn get(&self, cache: VideoCache, pool: DbPool, page: u8, mut sort: String, options: ServerOptions) -> Result<Vec<VideoItem>> {
|
||||
async fn get(
|
||||
&self,
|
||||
cache: VideoCache,
|
||||
pool: DbPool,
|
||||
page: u8,
|
||||
mut sort: String,
|
||||
options: ServerOptions,
|
||||
) -> Result<Vec<VideoItem>> {
|
||||
// Use ok_or to avoid unwrapping options
|
||||
let language = options.language.as_ref().ok_or("Missing language")?;
|
||||
let filter = options.filter.as_ref().ok_or("Missing filter")?;
|
||||
@@ -187,35 +194,57 @@ impl MissavProvider {
|
||||
sort = format!("&sort={}", sort);
|
||||
}
|
||||
let url_str = format!("{}/{}/{}?page={}{}", self.url, language, filter, page, sort);
|
||||
|
||||
|
||||
if let Some((time, items)) = cache.get(&url_str) {
|
||||
if time.elapsed().unwrap_or_default().as_secs() < 3600 {
|
||||
return Ok(items.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let text = requester.get(&url_str, Some(Version::HTTP_2)).await.unwrap_or_else(|e| {
|
||||
eprintln!("Error fetching Missav URL {}: {}", url_str, e);
|
||||
let _ = send_discord_error_report(e.to_string(), None, Some(&url_str), None, file!(), line!(), module_path!());
|
||||
"".to_string()
|
||||
});
|
||||
let text = requester
|
||||
.get(&url_str, Some(Version::HTTP_2))
|
||||
.await
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("Error fetching Missav URL {}: {}", url_str, e);
|
||||
let _ = send_discord_error_report(
|
||||
e.to_string(),
|
||||
None,
|
||||
Some(&url_str),
|
||||
None,
|
||||
file!(),
|
||||
line!(),
|
||||
module_path!(),
|
||||
);
|
||||
"".to_string()
|
||||
});
|
||||
let video_items = self.get_video_items_from_html(text, pool, requester).await;
|
||||
|
||||
|
||||
if !video_items.is_empty() {
|
||||
cache.insert(url_str, video_items.clone());
|
||||
}
|
||||
Ok(video_items)
|
||||
}
|
||||
|
||||
async fn query(&self, cache: VideoCache, pool: DbPool, page: u8, query: &str, mut sort: String, options: ServerOptions) -> Result<Vec<VideoItem>> {
|
||||
async fn query(
|
||||
&self,
|
||||
cache: VideoCache,
|
||||
pool: DbPool,
|
||||
page: u8,
|
||||
query: &str,
|
||||
mut sort: String,
|
||||
options: ServerOptions,
|
||||
) -> Result<Vec<VideoItem>> {
|
||||
let language = options.language.as_ref().ok_or("Missing language")?;
|
||||
let mut requester = options.requester.clone().ok_or("Missing requester")?;
|
||||
|
||||
|
||||
let search_string = query.replace(" ", "%20");
|
||||
if !sort.is_empty() {
|
||||
sort = format!("&sort={}", sort);
|
||||
}
|
||||
let url_str = format!("{}/{}/search/{}?page={}{}", self.url, language, search_string, page, sort);
|
||||
let url_str = format!(
|
||||
"{}/{}/search/{}?page={}{}",
|
||||
self.url, language, search_string, page, sort
|
||||
);
|
||||
|
||||
if let Some((time, items)) = cache.get(&url_str) {
|
||||
if time.elapsed().unwrap_or_default().as_secs() < 3600 {
|
||||
@@ -223,24 +252,44 @@ impl MissavProvider {
|
||||
}
|
||||
}
|
||||
|
||||
let text = requester.get(&url_str, Some(Version::HTTP_2)).await.unwrap_or_else(|e| {
|
||||
eprintln!("Error fetching Missav URL {}: {}", url_str, e);
|
||||
let _ = send_discord_error_report(e.to_string(), None, Some(&url_str), None, file!(), line!(), module_path!());
|
||||
"".to_string()
|
||||
});
|
||||
let text = requester
|
||||
.get(&url_str, Some(Version::HTTP_2))
|
||||
.await
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("Error fetching Missav URL {}: {}", url_str, e);
|
||||
let _ = send_discord_error_report(
|
||||
e.to_string(),
|
||||
None,
|
||||
Some(&url_str),
|
||||
None,
|
||||
file!(),
|
||||
line!(),
|
||||
module_path!(),
|
||||
);
|
||||
"".to_string()
|
||||
});
|
||||
let video_items = self.get_video_items_from_html(text, pool, requester).await;
|
||||
|
||||
|
||||
if !video_items.is_empty() {
|
||||
cache.insert(url_str, video_items.clone());
|
||||
}
|
||||
Ok(video_items)
|
||||
}
|
||||
|
||||
async fn get_video_items_from_html(&self, html: String, pool: DbPool, requester: Requester) -> Vec<VideoItem> {
|
||||
if html.is_empty() { return vec![]; }
|
||||
async fn get_video_items_from_html(
|
||||
&self,
|
||||
html: String,
|
||||
pool: DbPool,
|
||||
requester: Requester,
|
||||
) -> Vec<VideoItem> {
|
||||
if html.is_empty() {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
let segments: Vec<&str> = html.split("@mouseenter=\"setPreview(\'").collect();
|
||||
if segments.len() < 2 { return vec![]; }
|
||||
if segments.len() < 2 {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
let mut urls = vec![];
|
||||
for video_segment in &segments[1..] {
|
||||
@@ -253,14 +302,27 @@ impl MissavProvider {
|
||||
}
|
||||
}
|
||||
|
||||
let futures = urls.into_iter().map(|url| self.get_video_item(url, pool.clone(), requester.clone()));
|
||||
join_all(futures).await.into_iter().filter_map(Result::ok).collect()
|
||||
let futures = urls
|
||||
.into_iter()
|
||||
.map(|url| self.get_video_item(url, pool.clone(), requester.clone()));
|
||||
join_all(futures)
|
||||
.await
|
||||
.into_iter()
|
||||
.filter_map(Result::ok)
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn get_video_item(&self, url_str: String, pool: DbPool, mut requester: Requester) -> Result<VideoItem> {
|
||||
async fn get_video_item(
|
||||
&self,
|
||||
url_str: String,
|
||||
pool: DbPool,
|
||||
mut requester: Requester,
|
||||
) -> Result<VideoItem> {
|
||||
// 1. Database Check
|
||||
{
|
||||
let mut conn = pool.get().map_err(|e| Error::from(format!("Pool error: {}", e)))?;
|
||||
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);
|
||||
@@ -269,11 +331,22 @@ impl MissavProvider {
|
||||
}
|
||||
|
||||
// 2. Fetch Page
|
||||
let vid = requester.get(&url_str, Some(Version::HTTP_2)).await.unwrap_or_else(|e| {
|
||||
eprintln!("Error fetching Missav URL {}: {}", url_str, e);
|
||||
let _ = send_discord_error_report(e.to_string(), None, Some(&url_str), None, file!(), line!(), module_path!());
|
||||
"".to_string()
|
||||
});
|
||||
let vid = requester
|
||||
.get(&url_str, Some(Version::HTTP_2))
|
||||
.await
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("Error fetching Missav URL {}: {}", url_str, e);
|
||||
let _ = send_discord_error_report(
|
||||
e.to_string(),
|
||||
None,
|
||||
Some(&url_str),
|
||||
None,
|
||||
file!(),
|
||||
line!(),
|
||||
module_path!(),
|
||||
);
|
||||
"".to_string()
|
||||
});
|
||||
|
||||
// Helper closure to extract content between two strings
|
||||
let extract = |html: &str, start_tag: &str, end_tag: &str| -> Option<String> {
|
||||
@@ -285,24 +358,33 @@ impl MissavProvider {
|
||||
|
||||
let mut title = extract(&vid, "<meta property=\"og:title\" content=\"", "\"")
|
||||
.ok_or_else(|| ErrorKind::ParsingError(format!("title\n{:?}", vid)))?;
|
||||
|
||||
|
||||
title = decode(title.as_bytes()).to_string().unwrap_or(title);
|
||||
if url_str.contains("uncensored") {
|
||||
title = format!("[Uncensored] {}", title);
|
||||
}
|
||||
|
||||
let thumb = extract(&vid, "<meta property=\"og:image\" content=\"", "\"")
|
||||
.unwrap_or_default();
|
||||
let thumb =
|
||||
extract(&vid, "<meta property=\"og:image\" content=\"", "\"").unwrap_or_default();
|
||||
|
||||
let duration = extract(&vid, "<meta property=\"og:video:duration\" content=\"", "\"")
|
||||
.and_then(|d| d.parse::<u32>().ok())
|
||||
.unwrap_or(0);
|
||||
let duration = extract(
|
||||
&vid,
|
||||
"<meta property=\"og:video:duration\" content=\"",
|
||||
"\"",
|
||||
)
|
||||
.and_then(|d| d.parse::<u32>().ok())
|
||||
.unwrap_or(0);
|
||||
|
||||
let id = url_str.split('/').last().ok_or("No ID found")?.to_string();
|
||||
|
||||
// 3. Extract Tags (Generic approach to avoid repetitive code)
|
||||
let mut tags = vec![];
|
||||
for (label, prefix) in [("Actress:", "@actress"), ("Actor:", "@actor"), ("Maker:", "@maker"), ("Genre:", "@genre")] {
|
||||
for (label, prefix) in [
|
||||
("Actress:", "@actress"),
|
||||
("Actor:", "@actor"),
|
||||
("Maker:", "@maker"),
|
||||
("Genre:", "@genre"),
|
||||
] {
|
||||
let marker = format!("<span>{}</span>", label);
|
||||
if let Some(section) = extract(&vid, &marker, "</div>") {
|
||||
for part in section.split("class=\"text-nord13 font-medium\">").skip(1) {
|
||||
@@ -331,15 +413,24 @@ impl MissavProvider {
|
||||
parts.get(6)?,
|
||||
parts.get(7)?
|
||||
))
|
||||
})().ok_or_else(|| ErrorKind::ParsingError(format!("video_url\n{:?}", vid).to_string()))?;
|
||||
})()
|
||||
.ok_or_else(|| ErrorKind::ParsingError(format!("video_url\n{:?}", vid).to_string()))?;
|
||||
|
||||
let video_item = VideoItem::new(id, title, video_url, "missav".to_string(), thumb, duration)
|
||||
.tags(tags)
|
||||
.preview(format!("https://fourhoi.com/{}/preview.mp4", url_str.split('/').last().unwrap_or_default()));
|
||||
let video_item =
|
||||
VideoItem::new(id, title, video_url, "missav".to_string(), thumb, duration)
|
||||
.tags(tags)
|
||||
.preview(format!(
|
||||
"https://fourhoi.com/{}/preview.mp4",
|
||||
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());
|
||||
let _ = db::insert_video(
|
||||
&mut conn,
|
||||
&url_str,
|
||||
&serde_json::to_string(&video_item).unwrap_or_default(),
|
||||
);
|
||||
}
|
||||
|
||||
Ok(video_item)
|
||||
@@ -348,7 +439,16 @@ impl MissavProvider {
|
||||
|
||||
#[async_trait]
|
||||
impl Provider for MissavProvider {
|
||||
async fn get_videos(&self, cache: VideoCache, pool: DbPool, sort: String, query: Option<String>, page: String, _per_page: String, options: ServerOptions) -> Vec<VideoItem> {
|
||||
async fn get_videos(
|
||||
&self,
|
||||
cache: VideoCache,
|
||||
pool: DbPool,
|
||||
sort: String,
|
||||
query: Option<String>,
|
||||
page: String,
|
||||
_per_page: String,
|
||||
options: ServerOptions,
|
||||
) -> Vec<VideoItem> {
|
||||
let page_num = page.parse::<u8>().unwrap_or(1);
|
||||
let result = match query {
|
||||
Some(q) => self.query(cache, pool, page_num, &q, sort, options).await,
|
||||
@@ -357,7 +457,15 @@ impl Provider for MissavProvider {
|
||||
|
||||
result.unwrap_or_else(|e| {
|
||||
eprintln!("Error fetching videos: {}", e);
|
||||
let _ = send_discord_error_report(e.to_string(), Some(format_error_chain(&e)), None, None, file!(), line!(), module_path!());
|
||||
let _ = send_discord_error_report(
|
||||
e.to_string(),
|
||||
Some(format_error_chain(&e)),
|
||||
None,
|
||||
None,
|
||||
file!(),
|
||||
line!(),
|
||||
module_path!(),
|
||||
);
|
||||
vec![]
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user