use crate::agent::{AgentBase, YaydlAgent};
use crate::definitions::SiteDefinition;
use crate::VIDEO;
use anyhow::Result;
use regex::Regex;
use scraper::{Html, Selector};
use url::Url;
const MAX_FILENAME_LENGTH: usize = 142;
fn get_video_info(video: &mut VIDEO, url: &str) -> Result<bool> {
if video.info.is_empty() {
let local_url = url.to_owned();
let url_p = Url::parse(&local_url)?;
let agent = YaydlAgent::init(url_p);
let body = agent
.get(&local_url)
.call()
.expect("Could not go to the url")
.body_mut()
.read_to_string()
.expect("Could not read the site source");
video.info.push_str(&body);
}
Ok(true)
}
struct SpankbangHandler;
impl SiteDefinition for SpankbangHandler {
fn can_handle_url<'a>(
&'a self,
_video: &mut VIDEO,
url: &'a str,
_webdriver_port: u16,
) -> Result<bool> {
Ok(Regex::new(r"spankbang.com/.+").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: &mut VIDEO,
url: &'a str,
_webdriver_port: u16,
) -> Result<String> {
Ok(url_filename(url.to_string()))
}
fn find_video_direct_url<'a>(
&'a self,
video: &'a mut VIDEO,
url: &'a str,
_webdriver_port: u16,
_onlyaudio: bool,
) -> Result<String> {
let _not_used = get_video_info(video, url);
let video_info_html = Html::parse_document(&video.info);
let url_selector = Selector::parse(r#"source[type="video/mp4"]"#).unwrap();
let url_elem = video_info_html.select(&url_selector).next().unwrap();
let url_contents = url_elem.value().attr("src").unwrap();
Ok(url_contents.to_string())
}
fn does_video_exist<'a>(
&'a self,
video: &'a mut VIDEO,
url: &'a str,
_webdriver_port: u16,
) -> Result<bool> {
let _not_used = get_video_info(video, url);
Ok(!video.info.is_empty())
}
fn display_name<'a>(&'a self) -> String {
"Spankbang".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("mp4".to_string())
}
fn web_driver_required<'a>(&'a self) -> bool {
false
}
}
inventory::submit! {
&SpankbangHandler as &dyn SiteDefinition
}
fn url_filename(url: String) -> String {
let path = match Url::parse(&url) {
Ok(urlx) => urlx.path().to_string(),
Err(e) => {
println!("Error: Could not parse '{}'. {}.", url, e);
"url_filename_parse_error".to_string()
}
};
let vec: Vec<&str> = path.as_str().split("/").map(|s| s).collect();
let id_5char = vec[1]; let description = vec[3];
let mut base_filename = format!("{}-{}", description, id_5char);
if description.len() > MAX_FILENAME_LENGTH {
let (shorten, _) = description.split_at(MAX_FILENAME_LENGTH); base_filename = format!("{}...-{}", shorten, id_5char); }
return windows_filename(linux_filename(base_filename));
}
fn linux_filename(in_filename: String) -> String {
let out_filename = format!(
"{}",
in_filename.trim().replace(
&['|', '\'', '\"', ':', '\'', '\\', '/'][..], r#"_"#
)
);
return out_filename;
}
fn windows_filename(in_filename: String) -> String {
let out_filename = format!(
"{}",
in_filename
.trim()
.replace(&['<', '>', ':', '?', '*', '\n', '+'][..], r#"_"#) );
return out_filename;
}