Files
hottub/src/api.rs
2025-06-06 07:48:21 +00:00

290 lines
11 KiB
Rust

use ntex::http::header;
use ntex::web;
use ntex::web::HttpRequest;
use crate::providers::hanime::HanimeProvider;
use crate::providers::perverzija::PerverzijaProvider;
use crate::util::cache::VideoCache;
use crate::{providers::*, status::*, videos::*, DbPool};
pub fn config(cfg: &mut web::ServiceConfig) {
cfg.service(
web::resource("/status")
.route(web::post().to(status))
.route(web::get().to(status)),
)
.service(
web::resource("/videos")
// .route(web::get().to(videos_get))
.route(web::post().to(videos_post)),
);
}
async fn status(req: HttpRequest) -> Result<impl web::Responder, web::Error> {
let host = req
.headers()
.get(header::HOST)
.and_then(|h| h.to_str().ok())
.unwrap_or_default()
.to_string();
let mut status = Status::new();
// You can now use `method`, `host`, and `port` as needed
// status.add_channel(Channel {
// id: "all".to_string(),
// name: "SpaceMoehre's Hottub".to_string(),
// favicon: format!("http://{}/static/favicon.ico", host).to_string(),
// premium: false,
// description: "Work in Progress".to_string(),
// status: "active".to_string(),
// categories: vec![],
// options: vec![
// ChannelOption {
// id: "channels".to_string(),
// title: "Sites".to_string(),
// description: "Websites included in search results.".to_string(),
// systemImage: "network".to_string(),
// colorName: "purple".to_string(),
// options: vec![
// FilterOption {
// id: "perverzija".to_string(),
// title: "Perverzija".to_string(),
// },
// ],
// multiSelect: true,
// },
// ChannelOption {
// id: "sort".to_string(),
// title: "Sort".to_string(),
// description: "Sort the Videos".to_string(), //"Sort the videos by Date or Name.".to_string(),
// systemImage: "list.number".to_string(),
// colorName: "blue".to_string(),
// options: vec![
// FilterOption {
// id: "date".to_string(),
// title: "Date".to_string(),
// },
// FilterOption {
// id: "name".to_string(),
// title: "Name".to_string(),
// },
// ],
// multiSelect: false,
// },
// ChannelOption {
// id: "duration".to_string(),
// title: "Duration".to_string(),
// description: "Filter the videos by duration.".to_string(),
// systemImage: "timer".to_string(),
// colorName: "green".to_string(),
// options: vec![
// FilterOption {
// id: "short".to_string(),
// title: "< 1h".to_string(),
// },
// FilterOption {
// id: "long".to_string(),
// title: "> 1h".to_string(),
// },
// ],
// multiSelect: true,
// },
// ChannelOption {
// id: "featured".to_string(),
// title: "Featured".to_string(),
// description: "Filter Featured Videos.".to_string(),
// systemImage: "star".to_string(),
// colorName: "red".to_string(),
// options: vec![
// FilterOption {
// id: "all".to_string(),
// title: "No".to_string(),
// },
// FilterOption {
// id: "featured".to_string(),
// title: "Yes".to_string(),
// },
// ],
// multiSelect: false,
// },
// ],
// nsfw: true,
// });
status.add_channel(Channel {
id: "perverzija".to_string(),
name: "Perverzija".to_string(),
description: "Free videos from Perverzija".to_string(),
premium: false,
favicon: "https://www.google.com/s2/favicons?sz=64&domain=tube.perverzija.com".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(), //"Sort the videos by Date or Name.".to_string(),
systemImage: "list.number".to_string(),
colorName: "blue".to_string(),
options: vec![
FilterOption {
id: "date".to_string(),
title: "Date".to_string(),
},
FilterOption {
id: "name".to_string(),
title: "Name".to_string(),
},
],
multiSelect: false,
},
ChannelOption {
id: "featured".to_string(),
title: "Featured".to_string(),
description: "Filter Featured Videos.".to_string(),
systemImage: "star".to_string(),
colorName: "red".to_string(),
options: vec![
FilterOption {
id: "all".to_string(),
title: "No".to_string(),
},
FilterOption {
id: "featured".to_string(),
title: "Yes".to_string(),
},
],
multiSelect: false,
},
ChannelOption {
id: "duration".to_string(),
title: "Duration".to_string(),
description: "Filter the videos by duration.".to_string(),
systemImage: "timer".to_string(),
colorName: "green".to_string(),
options: vec![
FilterOption {
id: "short".to_string(),
title: "< 1h".to_string(),
},
FilterOption {
id: "long".to_string(),
title: "> 1h".to_string(),
},
],
multiSelect: true,
},
],
nsfw: true,
});
status.add_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(), //"Sort the videos by Date or Name.".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: "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,
});
status.iconUrl = format!("http://{}/favicon.ico", host).to_string();
Ok(web::HttpResponse::Ok().json(&status))
}
async fn videos_post(
video_request: web::types::Json<VideosRequest>,
cache: web::types::State<VideoCache>,
pool: web::types::State<DbPool>,
) -> Result<impl web::Responder, web::Error> {
let mut videos = Videos {
pageInfo: PageInfo {
hasNextPage: true,
resultsPerPage: 10,
},
items: vec![],
};
let channel: String = video_request
.channel
.as_deref()
.unwrap_or("all")
.to_string();
let sort: String = video_request.sort.as_deref().unwrap_or("date").to_string();
let mut query: Option<String> = video_request.query.clone();
if video_request.query.as_deref() == Some("") {
query = None;
}
let page: u8 = video_request
.page
.as_deref()
.unwrap_or("1")
.to_string()
.parse()
.unwrap();
let perPage: u8 = video_request
.perPage
.as_deref()
.unwrap_or("10")
.to_string()
.parse()
.unwrap();
let featured = video_request.featured.as_deref().unwrap_or("all").to_string();
let provider = get_provider(channel.as_str())
.ok_or_else(|| web::error::ErrorBadRequest("Invalid channel".to_string()))?;
let video_items = provider
.get_videos(cache.get_ref().clone(), pool.get_ref().clone(), channel, sort, query, page.to_string(), perPage.to_string(), featured)
.await;
videos.items = video_items.clone();
Ok(web::HttpResponse::Ok().json(&videos))
}
pub fn get_provider(channel: &str) -> Option<AnyProvider> {
match channel {
"perverzija" => Some(AnyProvider::Perverzija(PerverzijaProvider::new())),
"hanime" => Some(AnyProvider::Hanime(HanimeProvider::new())),
_ => Some(AnyProvider::Perverzija(PerverzijaProvider::new())),
}
}