use indicatif::{ProgressBar, ProgressStyle};
use regex::Regex;
use reqwest::{blocking::Client, header, Url};
use std::{
fs,
io::{self, copy, Read},
path::Path,
process,
};
struct DownloadProgress<R> {
inner: R,
progress_bar: ProgressBar,
}
impl<R: Read> Read for DownloadProgress<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf).map(|n| {
self.progress_bar.inc(n as u64);
n
})
}
}
pub fn run(url: String, download: bool, show_quality: bool, quality: String) {
if !url.contains("playlist") {
find_video(url, download, &show_quality, &quality);
} else {
let content = get_html(&url, false);
if content.contains("Error Page Not Found") {
eprintln!("Playlist is private, cannot access it. Try making it public");
process::exit(1);
}
find_playlist_videos(download, content);
}
}
fn find_video(url: String, download: bool, show_quality: &bool, quality: &str) {
let content = get_html(&url, true);
let title = {
let re = Regex::new("<title>(.*) - Pornhub.com</title>").unwrap();
let i = match re.captures(&content) {
Some(i) => i.get(1).unwrap().as_str().to_string(),
None => "video".to_string(),
};
i.replace("&", "&")
.replace("'", "'")
.replace("/", "╱") };
let url = get_direct_url(&content, &show_quality, quality);
if !show_quality {
if download {
download_video(&url, &title);
println!("Saved");
}
else {
println!("Direct URL: {}", &url);
}
}
}
fn find_playlist_videos(download: bool, content: String) {
let re_urls = Regex::new(
r#"<a href="/view_video\.php\?viewkey=(ph[0-9a-ö]+)&pkey=[0-9]+" title=".*".*class="fade"#,
)
.unwrap();
let show_quality = false;
let quality = "none".to_string();
for url in re_urls.captures_iter(&content) {
let url = format!(
"https://www.pornhub.com/view_video.php?viewkey={}",
url.get(1).unwrap().as_str().to_string()
);
find_video(url, download, &show_quality, &quality);
}
}
fn download_video(url: &str, name: &str) {
let total_size: u64 = {
let resp = ureq::head(url).call();
resp.header("content-length").unwrap().parse().unwrap()
};
let name = format!("{}.mp4", name);
let url = Url::parse(url).unwrap();
let client = Client::new();
let mut request = client.get(url.as_str());
let pb = ProgressBar::new(total_size);
pb.set_style(ProgressStyle::default_bar()
.template("{spinner:.yellow} [{elapsed_precise}] [{bar:40.yellow/blue}] {bytes}/{total_bytes} ({eta})")
.progress_chars("#>-"));
let mut file = Path::new(&name);
if file.exists() {
let size = file.metadata().unwrap().len();
request = request.header(header::RANGE, format!("bytes={}-", size));
pb.inc(size);
}
let mut source = DownloadProgress {
progress_bar: pb,
inner: request.send().unwrap(),
};
let dest_bool = match fs::OpenOptions::new().create(true).append(true).open(&file) {
Err(_e) => "err",
Ok(_o) => "ok",
};
let mut dest: std::fs::File;
if dest_bool == "ok" {
println!("Saving as: {}", name);
dest = fs::OpenOptions::new()
.create(true)
.append(true)
.open(&file)
.unwrap();
} else {
let filename: String;
if Path::new("video.mp4").exists() {
let mut count = 0;
loop {
if !Path::new(format!("video_{:02}.mp4", count).as_str()).exists() {
break;
}
count += 1;
}
filename = format!("video_{:02}.mp4", count);
} else {
filename = "video.mp4".to_string();
}
file = Path::new(filename.as_str());
dest = fs::OpenOptions::new()
.create(true)
.append(true)
.open(&file)
.unwrap();
println!(
"Error occurred, when using video's title. Saving as {}",
filename
);
}
let _ = copy(&mut source, &mut dest).unwrap();
}
fn get_html(url: &str, nokia: bool) -> String {
if nokia {
let user_agent = "Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 4.0; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 635) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko) Mobile Safari/537";
let resp = ureq::request("GET", &url)
.set("User-Agent", user_agent)
.call();
resp.into_string().unwrap()
} else {
let resp = ureq::request("GET", &url).call();
resp.into_string().unwrap()
}
}
fn get_direct_url(html: &str, show_quality: &bool, quality: &str) -> String {
let re_1080p = Regex::new("\"quality_1080p\":\"(.*)\",\"quality_720p\"").unwrap();
let re_720p = Regex::new("\"quality_720p\":\"(.*)\",\"quality_240p\"").unwrap();
let re_480p = Regex::new("\"quality_480p\":\"(.*)\",\"mediaPriority").unwrap();
let re_240p = Regex::new("\"quality_240p\":\"(.*)\",\"quality_480p\"").unwrap();
if *show_quality {
let mut available_qualities: Vec<String> = Vec::new();
if re_1080p.is_match(&html) {
available_qualities.push("1 - 1080p".to_string());
}
if re_720p.is_match(&html) {
available_qualities.push("2 - 720p".to_string());
}
if re_480p.is_match(&html) {
available_qualities.push("3 - 480p".to_string());
}
if re_240p.is_match(&html) {
available_qualities.push("4 - 240p".to_string());
}
for quality in available_qualities {
println!("{}", quality);
}
}
if quality != "none" {
if quality == "1" {
let result = match re_1080p.captures(&html) {
Some(i) => i.get(1).unwrap().as_str().to_string().replace("\\/", "/"),
None => "Error".to_string(),
};
if result == "Error" {
eprintln!("Couldn't find selected quality");
}
return result;
}
if quality == "2" {
let result = match re_720p.captures(&html) {
Some(i) => i.get(1).unwrap().as_str().to_string().replace("\\/", "/"),
None => "Error".to_string(),
};
if result == "Error" {
eprintln!("Couldn't find selected quality");
}
return result;
}
if quality == "3" {
let result = match re_480p.captures(&html) {
Some(i) => i.get(1).unwrap().as_str().to_string().replace("\\/", "/"),
None => "Error".to_string(),
};
if result == "Error" {
eprintln!("Couldn't find selected quality");
}
return result;
}
if quality == "4" {
let result = match re_240p.captures(&html) {
Some(i) => i.get(1).unwrap().as_str().to_string().replace("\\/", "/"),
None => "Error".to_string(),
};
if result == "Error" {
eprintln!("Couldn't find selected quality");
}
return result;
}
}
if html.contains("Error Page Not Found") {
eprintln!("Video is probably deleted from the ph");
process::exit(1);
}
let mut result = match re_1080p.captures(&html) {
Some(i) => i.get(1).unwrap().as_str().to_string().replace("\\/", "/"),
None => "Error".to_string(),
};
if result == "Error" {
result = match re_720p.captures(&html) {
Some(i) => i.get(1).unwrap().as_str().to_string().replace("\\/", "/"),
None => "Error".to_string(),
};
}
if result == "Error" {
result = match re_480p.captures(&html) {
Some(i) => i.get(1).unwrap().as_str().to_string().replace("\\/", "/"),
None => "Error".to_string(),
};
}
if result == "Error" {
result = match re_240p.captures(&html) {
Some(i) => i.get(1).unwrap().as_str().to_string().replace("\\/", "/"),
None => "Error".to_string(),
};
}
if result == "Error" {
eprintln!("Couldn't find direct download link");
process::exit(1);
}
result
}