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;
fn get_video_info(video: &mut VIDEO, url: &str) -> Result<Html> {
if video.info.is_empty() {
let id_regex = Regex::new(r"(?:uploads/)([0-9]+)$").unwrap();
let id = id_regex.captures(url).unwrap().get(1).unwrap().as_str();
let static_url = format!("https://pr0gramm.com/static/{}", id).to_owned();
let url_p = Url::parse(&static_url)?;
let agent = YaydlAgent::init(url_p);
let body = agent
.get(&static_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);
}
let d = Html::parse_document(&video.info);
Ok(d)
}
struct Pr0grammHandler;
impl SiteDefinition for Pr0grammHandler {
fn can_handle_url<'a>(
&'a self,
_video: &mut VIDEO,
url: &'a str,
_webdriver_port: u16,
) -> Result<bool> {
Ok(Regex::new(r"pr0gramm.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 video_info = get_video_info(video, url)?;
let title_selector = Selector::parse("title").unwrap();
let text = video_info.select(&title_selector).next();
let result = match text {
Some(value) => value.text().collect::<String>(),
None => "pr0gramm".to_string(),
};
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 video_info = get_video_info(video, url)?;
let url_selector = Selector::parse("video").unwrap();
let url_elem = video_info.select(&url_selector).next().unwrap();
let url_contents = url_elem.value().attr("src").unwrap();
let url_final = format!("https:{}", url_contents);
Ok(url_final.to_string())
}
fn does_video_exist<'a>(
&'a self,
video: &'a mut VIDEO,
url: &'a str,
_webdriver_port: u16,
) -> Result<bool> {
let _video_info = get_video_info(video, url);
Ok(!video.info.is_empty())
}
fn display_name<'a>(&'a self) -> String {
"pr0gramm".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! {
&Pr0grammHandler as &dyn SiteDefinition
}