More refactoring

This commit is contained in:
Daniel M 2022-03-31 17:13:57 +02:00
parent 396744e3a2
commit 88f23ae568
4 changed files with 17 additions and 26 deletions

View File

@ -35,10 +35,7 @@ pub struct DlReporter {
impl DlReporter { impl DlReporter {
pub fn new(id: u32, transmitter: mpsc::UnboundedSender<DlReport>) -> DlReporter { pub fn new(id: u32, transmitter: mpsc::UnboundedSender<DlReport>) -> DlReporter {
DlReporter { DlReporter { id, transmitter }
id: id,
transmitter: transmitter,
}
} }
pub fn send(&self, status: DlStatus) { pub fn send(&self, status: DlStatus) {
@ -98,12 +95,12 @@ fn print_accumulated_report(
execute!( execute!(
stdout(), stdout(),
Print(format!("----------------------------------------")), Print("----------------------------------------".to_string()),
Clear(ClearType::UntilNewLine), Clear(ClearType::UntilNewLine),
Print("\n") Print("\n")
)?; )?;
for (_k, v) in statuses { for v in statuses.values() {
let percent_complete = v.progress as f64 / v.total_size as f64 * 100.0; let percent_complete = v.progress as f64 / v.total_size as f64 * 100.0;
execute!( execute!(

View File

@ -25,7 +25,7 @@ impl RollingAverage {
} }
fn value(&self) -> f64 { fn value(&self) -> f64 {
if self.data.len() == 0 { if self.data.is_empty() {
0.0 0.0
} else { } else {
let mut max = self.data[0]; let mut max = self.data[0];
@ -64,7 +64,7 @@ impl RollingAverage {
/// Get the filename at the end of the given URL. This will decode the URL Encoding. /// Get the filename at the end of the given URL. This will decode the URL Encoding.
pub fn url_to_filename(url: &str) -> String { pub fn url_to_filename(url: &str) -> String {
let url_dec = percent_decode_str(&url) let url_dec = percent_decode_str(url)
.decode_utf8_lossy() .decode_utf8_lossy()
.to_owned() .to_owned()
.to_string(); .to_string();
@ -74,7 +74,7 @@ pub fn url_to_filename(url: &str) -> String {
.to_str() .to_str()
.unwrap(); .unwrap();
// Split at ? and return the first part. If no ? is present, this just returns the full string // Split at ? and return the first part. If no ? is present, this just returns the full string
file_name.split("?").next().unwrap().to_string() file_name.split('?').next().unwrap().to_string()
} }
pub async fn download_feedback( pub async fn download_feedback(
@ -123,7 +123,7 @@ pub async fn download_feedback_chunks(
let mut ofile = opts let mut ofile = opts
.create(true) .create(true)
.write(true) .write(true)
.truncate(!from_to.is_some()) .truncate(from_to.is_none())
.open(into_file) .open(into_file)
.await?; .await?;
@ -196,7 +196,7 @@ pub async fn download_feedback_chunks(
}); });
} }
if buff.len() > 0 { if !buff.is_empty() {
ofile.write_all(&buff).await?; ofile.write_all(&buff).await?;
} }
@ -243,8 +243,8 @@ pub async fn download_feedback_multi(
let t_start = SystemTime::now(); let t_start = SystemTime::now();
for index in 0..conn_count { for index in 0..conn_count {
let url = url.clone().to_owned(); let url = url.to_owned();
let into_file = into_file.clone().to_owned(); let into_file = into_file.to_owned();
let tx = tx.clone(); let tx = tx.clone();
@ -284,7 +284,7 @@ pub async fn download_feedback_multi(
let rep_task = rep.clone(); let rep_task = rep.clone();
let mut t_last = t_start.clone(); let mut t_last = t_start;
let manager_handle = tokio::task::spawn(async move { let manager_handle = tokio::task::spawn(async move {
let rep = rep_task; let rep = rep_task;

View File

@ -115,13 +115,7 @@ async fn download_multiple(args: CLIArgs, raw_urls: Vec<String>) -> Result<()> {
} }
async fn download_job(urls: SyncQueue, reporter: UnboundedSender<DlReport>, cli_args: CLIArgs) { async fn download_job(urls: SyncQueue, reporter: UnboundedSender<DlReport>, cli_args: CLIArgs) {
loop { while let Some(dlreq) = urls.lock().await.pop_front() {
// Get the next url to download or break if there are no more urls
let dlreq = match urls.lock().await.pop_front() {
Some(it) => it,
None => break,
};
let reporter = DlReporter::new(dlreq.id as u32, reporter.clone()); let reporter = DlReporter::new(dlreq.id as u32, reporter.clone());
// Resolve the zippy url to the direct download url if necessary // Resolve the zippy url to the direct download url if necessary

View File

@ -25,12 +25,12 @@ document.getElementById('dlbutton').href = "/d/0Ky7p1C6/" + (186549 % 51245 + 18
pub async fn resolve_link(url: &str) -> Result<String> { pub async fn resolve_link(url: &str) -> Result<String> {
// Regex to check if the provided url is a zippyshare download url // Regex to check if the provided url is a zippyshare download url
let re = Regex::new(r"(https://www\d*\.zippyshare\.com)")?; let re = Regex::new(r"(https://www\d*\.zippyshare\.com)")?;
if !re.is_match(&url) { if !re.is_match(url) {
return Err(Error::new(ErrorKind::Other, "URL is not a zippyshare url").into()); return Err(Error::new(ErrorKind::Other, "URL is not a zippyshare url").into());
} }
// Extract the hostname (with https:// prefix) for later // Extract the hostname (with https:// prefix) for later
let base_host = &re.captures(&url).unwrap()[0]; let base_host = &re.captures(url).unwrap()[0];
// Download the html body for the download page // Download the html body for the download page
let body = reqwest::get(url).await?.text().await?; let body = reqwest::get(url).await?.text().await?;
@ -47,10 +47,10 @@ pub async fn resolve_link(url: &str) -> Result<String> {
let url_start = &cap_link[1]; let url_start = &cap_link[1];
let url_end = &cap_link[5]; let url_end = &cap_link[5];
let n2: i32 = i32::from_str_radix(&cap_link[2], 10)?; let n2: i32 = cap_link[2].parse()?;
let n3: i32 = i32::from_str_radix(&cap_link[3], 10)?; let n3: i32 = cap_link[3].parse()?;
let n4 = n2; let n4 = n2;
let n5: i32 = i32::from_str_radix(&cap_link[4], 10)?; let n5: i32 = cap_link[4].parse()?;
let mixed = n2 % n3 + n4 % n5; let mixed = n2 % n3 + n4 % n5;