use crate::agent::{AgentBase, YaydlAgent};
use crate::definitions::SiteDefinition;
use crate::VIDEO;
use anyhow::Result;
use regex::Regex;
use std::path::Path;
use url::Url;
struct GenericFileHandler;
impl SiteDefinition for GenericFileHandler {
fn can_handle_url<'a>(
&'a self,
_video: &mut VIDEO,
url: &'a str,
_webdriver_port: u16,
) -> Result<bool> {
Ok(Regex::new(r"\.mp(4|g)$").unwrap().is_match(url))
}
fn is_playlist<'a>(&'a self, _url: &'a str, _webdriver_port: u16) -> Result<bool> {
Ok(false)
}
fn find_video_title<'a>(
&'a self,
_video: &'a mut VIDEO,
url: &'a str,
_webdriver_port: u16,
) -> Result<String> {
let filename = Path::new(url)
.file_stem()
.unwrap()
.to_string_lossy()
.into_owned();
Ok(filename)
}
fn find_video_direct_url<'a>(
&'a self,
_video: &'a mut VIDEO,
url: &'a str,
_webdriver_port: u16,
_onlyaudio: bool,
) -> Result<String> {
Ok(url.to_string())
}
fn does_video_exist<'a>(
&'a self,
_video: &'a mut VIDEO,
url: &'a str,
_webdriver_port: u16,
) -> Result<bool> {
let url_p = Url::parse(url)?;
let agent = YaydlAgent::init(url_p);
match agent.get(url).call() {
Ok(_) => Ok(true),
Err(_) => Ok(false),
}
}
fn display_name<'a>(&'a self) -> String {
"(direct)".to_string()
}
fn find_video_file_extension<'a>(
&'a self,
_video: &'a mut VIDEO,
url: &'a str,
_webdriver_port: u16,
_onlyaudio: bool,
) -> Result<String> {
Ok(String::from(url).split(".").last().unwrap().to_string())
}
fn web_driver_required<'a>(&'a self) -> bool {
false
}
}
inventory::submit! {
&GenericFileHandler as &dyn SiteDefinition
}