provider refactors and fixes
This commit is contained in:
@@ -4,9 +4,11 @@ use futures::future::join_all;
|
||||
use serde_json::json;
|
||||
use std::vec;
|
||||
|
||||
use crate::api::ClientVersion;
|
||||
use crate::DbPool;
|
||||
use crate::db;
|
||||
use crate::providers::Provider;
|
||||
use crate::providers::{Provider, report_provider_error, report_provider_error_background};
|
||||
use crate::status::*;
|
||||
use crate::util::cache::VideoCache;
|
||||
use crate::videos::{self, ServerOptions, VideoItem};
|
||||
|
||||
@@ -124,13 +126,83 @@ impl HanimeProvider {
|
||||
}
|
||||
}
|
||||
|
||||
fn build_channel(&self, _clientversion: ClientVersion) -> Channel {
|
||||
Channel {
|
||||
id: "hanime".to_string(),
|
||||
name: "Hanime".to_string(),
|
||||
description: "Free Hentai from Hanime".to_string(),
|
||||
premium: false,
|
||||
favicon: "https://www.google.com/s2/favicons?sz=64&domain=hanime.tv".to_string(),
|
||||
status: "active".to_string(),
|
||||
categories: vec![],
|
||||
options: vec![ChannelOption {
|
||||
id: "sort".to_string(),
|
||||
title: "Sort".to_string(),
|
||||
description: "Sort the Videos".to_string(),
|
||||
systemImage: "list.number".to_string(),
|
||||
colorName: "blue".to_string(),
|
||||
options: vec![
|
||||
FilterOption {
|
||||
id: "created_at_unix.desc".to_string(),
|
||||
title: "Recent Upload".to_string(),
|
||||
},
|
||||
FilterOption {
|
||||
id: "created_at_unix.asc".to_string(),
|
||||
title: "Old Upload".to_string(),
|
||||
},
|
||||
FilterOption {
|
||||
id: "views.desc".to_string(),
|
||||
title: "Most Views".to_string(),
|
||||
},
|
||||
FilterOption {
|
||||
id: "views.asc".to_string(),
|
||||
title: "Least Views".to_string(),
|
||||
},
|
||||
FilterOption {
|
||||
id: "likes.desc".to_string(),
|
||||
title: "Most Likes".to_string(),
|
||||
},
|
||||
FilterOption {
|
||||
id: "likes.asc".to_string(),
|
||||
title: "Least Likes".to_string(),
|
||||
},
|
||||
FilterOption {
|
||||
id: "released_at_unix.desc".to_string(),
|
||||
title: "New".to_string(),
|
||||
},
|
||||
FilterOption {
|
||||
id: "released_at_unix.asc".to_string(),
|
||||
title: "Old".to_string(),
|
||||
},
|
||||
FilterOption {
|
||||
id: "title_sortable.asc".to_string(),
|
||||
title: "A - Z".to_string(),
|
||||
},
|
||||
FilterOption {
|
||||
id: "title_sortable.desc".to_string(),
|
||||
title: "Z - A".to_string(),
|
||||
},
|
||||
],
|
||||
multiSelect: false,
|
||||
}],
|
||||
nsfw: true,
|
||||
cacheDuration: None,
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_video_item(
|
||||
&self,
|
||||
hit: HanimeSearchResult,
|
||||
pool: DbPool,
|
||||
options: ServerOptions,
|
||||
) -> Result<VideoItem> {
|
||||
let mut conn = pool.get().expect("couldn't get db connection from pool");
|
||||
let mut conn = match pool.get() {
|
||||
Ok(conn) => conn,
|
||||
Err(e) => {
|
||||
report_provider_error("hanime", "get_video_item.pool_get", &e.to_string()).await;
|
||||
return Err(Error::from("Failed to get DB connection"));
|
||||
}
|
||||
};
|
||||
let db_result = db::get_video(
|
||||
&mut conn,
|
||||
format!(
|
||||
@@ -169,13 +241,24 @@ impl HanimeProvider {
|
||||
"m3u8".to_string(),
|
||||
)]));
|
||||
} else {
|
||||
let _ = db::delete_video(
|
||||
&mut pool.get().expect("couldn't get db connection from pool"),
|
||||
format!(
|
||||
"https://h.freeanimehentai.net/api/v8/video?id={}&",
|
||||
hit.slug.clone()
|
||||
),
|
||||
);
|
||||
match pool.get() {
|
||||
Ok(mut conn) => {
|
||||
let _ = db::delete_video(
|
||||
&mut conn,
|
||||
format!(
|
||||
"https://h.freeanimehentai.net/api/v8/video?id={}&",
|
||||
hit.slug.clone()
|
||||
),
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
report_provider_error_background(
|
||||
"hanime",
|
||||
"get_video_item.delete_video.pool_get",
|
||||
&e.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None) => (),
|
||||
@@ -189,7 +272,7 @@ impl HanimeProvider {
|
||||
id
|
||||
);
|
||||
|
||||
let mut requester = options.requester.clone().unwrap();
|
||||
let mut requester = crate::providers::requester_or_default(&options, module_path!(), "missing_requester");
|
||||
let payload = json!({
|
||||
"width": 571, "height": 703, "ab": "kh" }
|
||||
);
|
||||
@@ -216,41 +299,71 @@ impl HanimeProvider {
|
||||
],
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
.map_err(|e| {
|
||||
report_provider_error_background(
|
||||
"hanime",
|
||||
"get_video_item.get_raw_with_headers",
|
||||
&e.to_string(),
|
||||
);
|
||||
Error::from(format!("Failed to fetch manifest response: {e}"))
|
||||
})?
|
||||
.text()
|
||||
.await
|
||||
.unwrap();
|
||||
.map_err(|e| {
|
||||
report_provider_error_background(
|
||||
"hanime",
|
||||
"get_video_item.response_text",
|
||||
&e.to_string(),
|
||||
);
|
||||
Error::from(format!("Failed to decode manifest response body: {e}"))
|
||||
})?;
|
||||
if text.contains("Unautho") {
|
||||
println!("Fetched video details for {}: {}", title, text);
|
||||
return Err(Error::from("Unauthorized"));
|
||||
}
|
||||
let urls = text.split("streams").collect::<Vec<&str>>()[1];
|
||||
let urls = text
|
||||
.split("streams")
|
||||
.nth(1)
|
||||
.ok_or_else(|| Error::from("Missing streams section in manifest"))?;
|
||||
let mut url_vec = vec![];
|
||||
|
||||
for el in urls.split("\"url\":\"").collect::<Vec<&str>>() {
|
||||
let url = el.split("\"").collect::<Vec<&str>>()[0];
|
||||
let url = el.split("\"").collect::<Vec<&str>>().get(0).copied().unwrap_or_default();
|
||||
if !url.is_empty() && url.contains("m3u8") {
|
||||
url_vec.push(url.to_string());
|
||||
}
|
||||
}
|
||||
let mut conn = pool.get().expect("couldn't get db connection from pool");
|
||||
let _ = db::insert_video(
|
||||
&mut conn,
|
||||
&format!(
|
||||
"https://h.freeanimehentai.net/api/v8/video?id={}&",
|
||||
hit.slug.clone()
|
||||
),
|
||||
&url_vec[0].clone(),
|
||||
);
|
||||
drop(conn);
|
||||
let first_url = url_vec
|
||||
.first()
|
||||
.cloned()
|
||||
.ok_or_else(|| Error::from("No stream URL found in manifest"))?;
|
||||
match pool.get() {
|
||||
Ok(mut conn) => {
|
||||
let _ = db::insert_video(
|
||||
&mut conn,
|
||||
&format!(
|
||||
"https://h.freeanimehentai.net/api/v8/video?id={}&",
|
||||
hit.slug.clone()
|
||||
),
|
||||
&first_url,
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
report_provider_error_background(
|
||||
"hanime",
|
||||
"get_video_item.insert_video.pool_get",
|
||||
&e.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok(
|
||||
VideoItem::new(id, title, url_vec[0].clone(), channel, thumb, duration)
|
||||
VideoItem::new(id, title, first_url.clone(), channel, thumb, duration)
|
||||
.tags(hit.tags)
|
||||
.uploader(hit.brand)
|
||||
.views(hit.views as u32)
|
||||
.rating((hit.likes as f32 / (hit.likes + hit.dislikes) as f32) * 100 as f32)
|
||||
.formats(vec![videos::VideoFormat::new(
|
||||
url_vec[0].clone(),
|
||||
first_url,
|
||||
"1080".to_string(),
|
||||
"m3u8".to_string(),
|
||||
)]),
|
||||
@@ -268,11 +381,11 @@ impl HanimeProvider {
|
||||
) -> Result<Vec<VideoItem>> {
|
||||
let index = format!("hanime:{}:{}:{}", query, page, sort);
|
||||
let order_by = match sort.contains(".") {
|
||||
true => sort.split(".").collect::<Vec<&str>>()[0].to_string(),
|
||||
true => sort.split(".").collect::<Vec<&str>>().get(0).copied().unwrap_or_default().to_string(),
|
||||
false => "created_at_unix".to_string(),
|
||||
};
|
||||
let ordering = match sort.contains(".") {
|
||||
true => sort.split(".").collect::<Vec<&str>>()[1].to_string(),
|
||||
true => sort.split(".").collect::<Vec<&str>>().get(1).copied().unwrap_or_default().to_string(),
|
||||
false => "desc".to_string(),
|
||||
};
|
||||
let old_items = match cache.get(&index) {
|
||||
@@ -295,11 +408,22 @@ impl HanimeProvider {
|
||||
.order_by(order_by)
|
||||
.ordering(ordering);
|
||||
|
||||
let mut requester = options.requester.clone().unwrap();
|
||||
let response = requester
|
||||
let mut requester = crate::providers::requester_or_default(&options, module_path!(), "missing_requester");
|
||||
let response = match requester
|
||||
.post_json("https://search.htv-services.com/search", &search, vec![])
|
||||
.await
|
||||
.unwrap();
|
||||
{
|
||||
Ok(response) => response,
|
||||
Err(e) => {
|
||||
report_provider_error(
|
||||
"hanime",
|
||||
"get.search_request",
|
||||
&format!("query={query}; page={page}; error={e}"),
|
||||
)
|
||||
.await;
|
||||
return Ok(old_items);
|
||||
}
|
||||
};
|
||||
|
||||
let hits = match response.json::<HanimeSearchResponse>().await {
|
||||
Ok(resp) => resp.hits,
|
||||
@@ -374,4 +498,8 @@ impl Provider for HanimeProvider {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_channel(&self, clientversion: ClientVersion) -> Option<Channel> {
|
||||
Some(self.build_channel(clientversion))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user