use crate::definitions::SiteDefinition;
use crate::VIDEO;
use anyhow::{anyhow, Result};
use fantoccini::ClientBuilder;
use regex::Regex;
use scraper::{Html, Selector};
use tokio::runtime;
fn get_video_info(video: &mut VIDEO, url: &str, webdriver_port: u16) -> Result<bool> {
if video.info.is_empty() {
let local_url = url.to_owned();
let rt = runtime::Builder::new_current_thread()
.enable_time()
.enable_io()
.build()
.unwrap();
rt.block_on(async move {
let webdriver_url = format!("http://localhost:{}", webdriver_port);
let c = ClientBuilder::native()
.connect(&webdriver_url)
.await
.expect("failed to connect to web driver");
c.goto(&local_url)
.await
.expect("could not go to the site URL");
c.execute(
"document.getElementsByClassName('age-btn')[0].click();",
vec![],
)
.await
.expect("could not dismiss the age gate");
let body = c.source().await.expect("could not read the site source");
video.info.push_str(body.as_str());
c.close_window().await.expect("could not close the window");
});
}
Ok(true)
}
struct PornDoeHandler;
impl SiteDefinition for PornDoeHandler {
fn can_handle_url<'a>(
&'a self,
_video: &mut VIDEO,
url: &'a str,
_webdriver_port: u16,
) -> Result<bool> {
Ok(Regex::new(r"porndoe.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: &'a mut VIDEO,
url: &'a str,
webdriver_port: u16,
) -> Result<String> {
let _not_used = get_video_info(video, url, webdriver_port)?;
let video_info_html = Html::parse_document(video.info.as_str());
let h1_selector = Selector::parse("h1.-heading").unwrap();
let text = video_info_html.select(&h1_selector).next();
let result = match text {
Some(txt) => txt.text().collect(),
None => return Err(anyhow!("Could not extract the video title.")),
};
Ok(result)
}
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, webdriver_port)?;
let video_info_html = Html::parse_document(video.info.as_str());
let url_selector = Selector::parse(r#"meta[itemprop="contentUrl"]"#).unwrap();
let url_elem = video_info_html.select(&url_selector).next().unwrap();
let url_contents = url_elem.value().attr("content").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, webdriver_port);
Ok(!video.info.is_empty())
}
fn display_name<'a>(&'a self) -> String {
"PornDoe".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 {
true
}
}
inventory::submit! {
&PornDoeHandler as &dyn SiteDefinition
}