79 lines
2.3 KiB
Rust
79 lines
2.3 KiB
Rust
use std::{fs, vec};
|
|
use error_chain::error_chain;
|
|
use futures::future::join_all;
|
|
use serde_json::error::Category;
|
|
use wreq::Client;
|
|
use wreq_util::Emulation;
|
|
use crate::api::get_provider;
|
|
use crate::db;
|
|
use crate::providers::{AnyProvider, Provider};
|
|
use crate::util::cache::VideoCache;
|
|
use crate::util::interleave;
|
|
use crate::videos::{self, ServerOptions, VideoItem};
|
|
use crate::DbPool;
|
|
|
|
error_chain! {
|
|
foreign_links {
|
|
Io(std::io::Error);
|
|
HttpRequest(wreq::Error);
|
|
}
|
|
}
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
#[allow(dead_code)]
|
|
pub struct AllProvider {
|
|
}
|
|
|
|
impl AllProvider {
|
|
pub fn new() -> Self {
|
|
AllProvider {}
|
|
}
|
|
}
|
|
|
|
impl Provider for AllProvider {
|
|
async fn get_videos(
|
|
&self,
|
|
cache: VideoCache,
|
|
pool: DbPool,
|
|
sort: String,
|
|
query: Option<String>,
|
|
page: String,
|
|
per_page: String,
|
|
options: ServerOptions,
|
|
) -> Vec<VideoItem> {
|
|
let mut sites_str = options.clone().sites.unwrap();
|
|
if sites_str.is_empty() {
|
|
let files = fs::read_dir("./src/providers").unwrap();
|
|
let providers = files.map(|entry| entry.unwrap().file_name())
|
|
.filter(|name| name.to_str().unwrap().ends_with(".rs"))
|
|
.filter(|name| !name.to_str().unwrap().contains("mod.rs") && !name.to_str().unwrap().contains("all.rs"))
|
|
.map(|name| name.to_str().unwrap().replace(".rs", ""))
|
|
.collect::<Vec<String>>();
|
|
sites_str = providers.join(",");
|
|
}
|
|
let sites = sites_str
|
|
.split(',')
|
|
.map(|s| s.to_string()) // or s.to_owned()
|
|
.collect::<Vec<String>>();
|
|
let providers = sites.iter().map(|el| get_provider(el.as_str()).unwrap()).collect::<Vec<AnyProvider>>();
|
|
|
|
let futures = providers.iter().map(|provider| {
|
|
provider.get_videos(
|
|
cache.clone(),
|
|
pool.clone(),
|
|
sort.clone(),
|
|
query.clone(),
|
|
page.clone(),
|
|
per_page.clone(),
|
|
options.clone()
|
|
)
|
|
}).collect::<Vec<_>>();
|
|
let results:Vec<Vec<VideoItem>> = join_all(futures).await;
|
|
let video_items: Vec<VideoItem> = interleave(&results);
|
|
|
|
|
|
return video_items;
|
|
}
|
|
}
|