This commit is contained in:
Simon
2026-01-03 10:17:39 +00:00
parent 97eeccf2bd
commit 4a7528c516

View File

@@ -7,9 +7,9 @@ use crate::util::time::parse_time_to_seconds;
use crate::videos::{ServerOptions, VideoItem}; use crate::videos::{ServerOptions, VideoItem};
use async_trait::async_trait; use async_trait::async_trait;
use error_chain::error_chain; use error_chain::error_chain;
use htmlentity::entity::{ICodedDataTrait, decode}; use htmlentity::entity::{decode, ICodedDataTrait};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use std::{vec}; use std::vec;
error_chain! { error_chain! {
foreign_links { foreign_links {
@@ -24,19 +24,25 @@ pub struct PmvhavenProvider {
stars: Arc<RwLock<Vec<String>>>, stars: Arc<RwLock<Vec<String>>>,
categories: Arc<RwLock<Vec<String>>>, categories: Arc<RwLock<Vec<String>>>,
} }
impl PmvhavenProvider { impl PmvhavenProvider {
pub fn new() -> Self { pub fn new() -> Self {
let provider = PmvhavenProvider { Self {
url: "https://pmvhaven.com".to_string(), url: "https://pmvhaven.com".to_string(),
stars: Arc::new(RwLock::new(vec![])), stars: Arc::new(RwLock::new(vec![])),
categories: Arc::new(RwLock::new(vec![])), categories: Arc::new(RwLock::new(vec![])),
}; }
provider
} }
fn build_channel(&self, clientversion: ClientVersion) -> Channel { fn build_channel(&self, clientversion: ClientVersion) -> Channel {
// if clientversion >= ClientVersion::new(22, 101, "22e".to_string()) {
let _ = clientversion; let _ = clientversion;
let categories = self
.categories
.read()
.map(|g| g.clone())
.unwrap_or_default();
Channel { Channel {
id: "pmvhaven".to_string(), id: "pmvhaven".to_string(),
name: "PMVHaven".to_string(), name: "PMVHaven".to_string(),
@@ -44,14 +50,14 @@ impl PmvhavenProvider {
premium: false, premium: false,
favicon: "https://www.google.com/s2/favicons?sz=64&domain=pmvhaven.com".to_string(), favicon: "https://www.google.com/s2/favicons?sz=64&domain=pmvhaven.com".to_string(),
status: "active".to_string(), status: "active".to_string(),
categories: self.categories.read().unwrap().iter().map(|c| c.clone()).collect(), categories,
options: vec![ options: vec![
ChannelOption { ChannelOption {
id: "sort".to_string(), id: "sort".into(),
title: "Sort".to_string(), title: "Sort".into(),
description: "Sort the Videos".to_string(), description: "Sort the Videos".into(),
systemImage: "list.number".to_string(), systemImage: "list.number".into(),
colorName: "blue".to_string(), colorName: "blue".into(),
options: vec![ options: vec![
FilterOption { FilterOption {
id: "relevance".into(), id: "relevance".into(),
@@ -81,11 +87,11 @@ impl PmvhavenProvider {
multiSelect: false, multiSelect: false,
}, },
ChannelOption { ChannelOption {
id: "duration".to_string(), id: "duration".into(),
title: "Duration".to_string(), title: "Duration".into(),
description: "Length of the Videos".to_string(), description: "Length of the Videos".into(),
systemImage: "timer".to_string(), systemImage: "timer".into(),
colorName: "green".to_string(), colorName: "green".into(),
options: vec![ options: vec![
FilterOption { FilterOption {
id: "any".into(), id: "any".into(),
@@ -116,7 +122,6 @@ impl PmvhavenProvider {
} }
} }
// Push one item with minimal lock time and dedup by id
fn push_unique(target: &Arc<RwLock<Vec<String>>>, item: String) { fn push_unique(target: &Arc<RwLock<Vec<String>>>, item: String) {
if let Ok(mut vec) = target.write() { if let Ok(mut vec) = target.write() {
if !vec.iter().any(|x| x == &item) { if !vec.iter().any(|x| x == &item) {
@@ -132,119 +137,128 @@ impl PmvhavenProvider {
query: &str, query: &str,
options: ServerOptions, options: ServerOptions,
) -> Result<Vec<VideoItem>> { ) -> Result<Vec<VideoItem>> {
let search_string = query.trim().to_string(); let search = query.trim().to_string();
let sort_string = match options.sort.unwrap_or("".to_string()).as_str() {
"newest" => "sort=-uploadDate", let sort = match options.sort.as_deref() {
"oldest" => "sort=uploadDate", Some("newest") => "&sort=-uploadDate",
"most viewed" => "sort=-views", Some("oldest") => "&sort=uploadDate",
"most liked" => "sort=-likes", Some("most viewed") => "&sort=-views",
"most disliked" => "sort=-dislikes", Some("most liked") => "&sort=-likes",
Some("most disliked") => "&sort=-dislikes",
_ => "", _ => "",
}; };
let duration_string = match options.duration.unwrap_or("".to_string()).as_str(){
"<4 min" => "durationMax=240", let duration = match options.duration.as_deref() {
"4-20 min" => "durationMin=240&durationMax=1200", Some("<4 min") => "&durationMax=240",
"20-60 min" => "durationMin=1200&durationMax=3600", Some("4-20 min") => "&durationMin=240&durationMax=1200",
">1 hour" => "durationMin=3600", Some("20-60 min") => "&durationMin=1200&durationMax=3600",
Some(">1 hour") => "&durationMin=3600",
_ => "", _ => "",
}; };
let endpoint = if search_string.is_empty() {
let endpoint = if search.is_empty() {
"api/videos" "api/videos"
} else { } else {
"api/videos/search" "api/videos/search"
}; };
let mut video_url = format!("{}/{}?limit=100&page={}&{}&{}", self.url, endpoint, page, duration_string, sort_string);
if let Some(star) = self.stars.read().unwrap().iter().find(|s| s.to_ascii_lowercase() == search_string.to_ascii_lowercase()) { let mut url = format!(
video_url = format!("{}&stars={}", video_url, star); "{}/{endpoint}?limit=100&page={page}{duration}{sort}",
} else if let Some(category) = self.categories.read().unwrap().iter().find(|s| s.to_ascii_lowercase() == search_string.to_ascii_lowercase()) { self.url
video_url = format!("{}&tagMode=AND&tags={}", video_url, category); );
} else {
video_url = format!("{}&q={}", video_url, search_string); if let Ok(stars) = self.stars.read() {
if let Some(star) = stars.iter().find(|s| s.eq_ignore_ascii_case(&search)) {
url.push_str(&format!("&stars={star}"));
}
} }
let old_items = match cache.get(&video_url) {
Some((time, items)) => { if let Ok(cats) = self.categories.read() {
if time.elapsed().unwrap_or_default().as_secs() < 60 * 5 { if let Some(cat) = cats.iter().find(|c| c.eq_ignore_ascii_case(&search)) {
return Ok(items.clone()); url.push_str(&format!("&tagMode=OR&tags={cat}&expandTags=false"));
} else {
let _ = cache.check().await;
return Ok(items.clone());
}
} }
None => { }
vec![]
if !search.is_empty() {
url.push_str(&format!("&q={search}"));
}
println!("pmvhaven query url: {}", url);
if let Some((time, items)) = cache.get(&url) {
println!("pmvhaven cache hit for url: {}", url);
println!("cache age: {} secs", time.elapsed().unwrap_or_default().as_secs());
println!("cached items: {}", items.len());
if time.elapsed().unwrap_or_default().as_secs() < 300 {
return Ok(items.clone());
} }
}
let mut requester = match options.requester {
Some(r) => r,
None => return Ok(vec![]),
}; };
let mut requester = options.requester.clone().unwrap(); let text = requester.get(&url, None).await.unwrap_or_default();
let json = serde_json::from_str(&text).unwrap_or(serde_json::Value::Null);
let items = self.get_video_items_from_json(json).await;
let text = requester.get(&video_url, None).await.unwrap(); if !items.is_empty() {
let json = serde_json::from_str::<serde_json::Value>(&text).unwrap_or(serde_json::Value::Null); cache.remove(&url);
let video_items: Vec<VideoItem> = self cache.insert(url, items.clone());
.get_video_items_from_json(json)
.await;
if !video_items.is_empty() {
cache.remove(&video_url);
cache.insert(video_url.clone(), video_items.clone());
} else {
return Ok(old_items);
} }
Ok(video_items) Ok(items)
} }
async fn get_video_items_from_json( async fn get_video_items_from_json(&self, json: serde_json::Value) -> Vec<VideoItem> {
&self,
json: serde_json::Value,
) -> Vec<VideoItem> {
if json.is_null() {
return vec![];
}
let mut items = vec![]; let mut items = vec![];
let success = json["success"].as_bool().unwrap_or(false);
if !success { if !json.get("success").and_then(|v| v.as_bool()).unwrap_or(false) {
return items; return items;
} }
let videos = json["data"].as_array().cloned().unwrap_or_default();
if videos.is_empty() { let videos = json.get("data").and_then(|v| v.as_array()).cloned().unwrap_or_default();
return items;
} for video in videos {
for video in videos.clone() { let title = decode(video.get("title").and_then(|v| v.as_str()).unwrap_or("").as_bytes())
let title = decode(video["title"].as_str().unwrap_or("").as_bytes()).to_string().unwrap_or("".to_string()); .to_string()
let id = video["_id"].as_str().unwrap_or(title.clone().as_str()).to_string(); .unwrap_or_default();
let video_url = video["videoUrl"].as_str().unwrap_or("").to_string();
let views = video["views"].as_u64().unwrap_or(0); let id = video
let thumb = video["thumbnailUrl"].as_str().unwrap_or("").to_string(); .get("_id")
let duration_str = video["duration"].as_str().unwrap_or("0"); .and_then(|v| v.as_str())
let duration = parse_time_to_seconds(duration_str).unwrap_or(0); .unwrap_or(&title)
let preview = video["previewUrl"].as_str().unwrap_or("").to_string(); .to_string();
let tags_array = video["tags"].as_array().cloned().unwrap_or_default();
for tag in tags_array.clone() { let video_url = video.get("videoUrl").and_then(|v| v.as_str()).unwrap_or("").to_string();
let tag_str = decode(tag.as_str().unwrap_or("").as_bytes()).to_string().unwrap_or("".to_string()); let thumb = video.get("thumbnailUrl").and_then(|v| v.as_str()).unwrap_or("").to_string();
Self::push_unique(&self.categories, tag_str.clone()); let preview = video.get("previewUrl").and_then(|v| v.as_str()).unwrap_or("").to_string();
let views = video.get("views").and_then(|v| v.as_u64()).unwrap_or(0);
let duration = parse_time_to_seconds(video.get("duration").and_then(|v| v.as_str()).unwrap_or("0")).unwrap_or(0);
let tags = video.get("tags").and_then(|v| v.as_array()).cloned().unwrap_or_default();
let stars = video.get("starsTags").and_then(|v| v.as_array()).cloned().unwrap_or_default();
for t in tags.iter() {
if let Some(s) = t.as_str() {
let decoded = decode(s.as_bytes()).to_string().unwrap_or_default();
Self::push_unique(&self.categories, decoded.clone());
}
} }
let stars_array = video["starsTags"].as_array().cloned().unwrap_or_default(); for t in stars.iter() {
for tag in stars_array.clone() { if let Some(s) = t.as_str() {
let tag_str = decode(tag.as_str().unwrap_or("").as_bytes()).to_string().unwrap_or("".to_string()); let decoded = decode(s.as_bytes()).to_string().unwrap_or_default();
Self::push_unique(&self.stars, tag_str.clone()); Self::push_unique(&self.stars, decoded.clone());
}
} }
let tags = stars_array.iter().chain(tags_array.iter()).cloned().collect::<Vec<_>>(); items.push(
let video_item = VideoItem::new( VideoItem::new(id, title, video_url.replace(' ', "%20"), "pmvhaven".into(), thumb, duration as u32)
id, .views(views as u32)
title, .preview(preview)
video_url.replace(" ", "%20").to_string(), );
"pmvhaven".to_string(),
thumb,
duration as u32,
)
.views(views as u32)
.preview(preview)
.tags(tags.iter().map(|t| decode(t.as_str().unwrap_or("").as_bytes()).to_string().unwrap_or("".to_string())).collect());
items.push(video_item);
} }
items
return items; }
}
} }
#[async_trait] #[async_trait]
@@ -252,26 +266,26 @@ impl Provider for PmvhavenProvider {
async fn get_videos( async fn get_videos(
&self, &self,
cache: VideoCache, cache: VideoCache,
pool: DbPool, _pool: DbPool,
_sort: String, _sort: String,
query: Option<String>, query: Option<String>,
page: String, page: String,
per_page: String, _per_page: String,
options: ServerOptions, options: ServerOptions,
) -> Vec<VideoItem> { ) -> Vec<VideoItem> {
let _ = per_page; let page = page.parse::<u8>().unwrap_or(1);
let _ = pool; let query = query.unwrap_or_default();
let videos: std::result::Result<Vec<VideoItem>, Error> = self.query(cache, page.parse::<u8>().unwrap_or(1), query.unwrap_or("".to_string()).as_str(), options)
.await; match self.query(cache, page, &query, options).await {
match videos {
Ok(v) => v, Ok(v) => v,
Err(e) => { Err(e) => {
println!("Error fetching videos: {}", e); eprintln!("pmvhaven error: {e}");
vec![] vec![]
} }
} }
} }
fn get_channel(&self, clientversion: ClientVersion) -> crate::status::Channel {
fn get_channel(&self, clientversion: ClientVersion) -> Channel {
self.build_channel(clientversion) self.build_channel(clientversion)
} }
} }