Snapshow of match variant.

This commit is contained in:
Jacob Hoffman-Andrews
2020-11-26 10:13:45 -08:00
committed by Martin Algesten
parent 33066fb074
commit d267dffde1
2 changed files with 33 additions and 9 deletions

View File

@@ -1,11 +1,11 @@
use std::io::{self, BufRead, BufReader, Read}; use std::io::{self, BufRead, BufReader, Read, Write};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle}; use std::thread::{self, JoinHandle};
use std::time::Duration; use std::time::Duration;
use std::{env, error, fmt, result}; use std::{env, error, fmt, result, result::Result};
use log::{error, info}; use log::{error, info};
use ureq; use ureq::{self, Response};
#[derive(Debug)] #[derive(Debug)]
struct Oops(String); struct Oops(String);
@@ -40,12 +40,36 @@ fn get(agent: &ureq::Agent, url: &str) -> Result<Vec<u8>> {
Ok(bytes) Ok(bytes)
} }
fn get_and_write(agent: &ureq::Agent, url: &str) { fn get_and_write(agent: &ureq::Agent, url: &str) -> std::result::Result<(), Oops> {
info!("🕷️ {}", url); let r = get_response(agent, url)?;
match get(agent, url) { let mut reader = r.into_reader();
Ok(_) => info!("✔️ {}", url), let mut bytes = vec![];
Err(e) => error!("⚠️ {} {}", url, e), reader.read_to_end(&mut bytes)?;
std::io::stdout().write_all(&bytes)?;
Ok(())
} }
fn get_response(agent: &ureq::Agent, url: &str) -> result::Result<Response, ureq::Error> {
let fetch = || agent.get(url).call();
let mut result = fetch();
for _ in 1..4 {
match result {
Err(ureq::Error {
response: Some(r), ..
}) if r.status() == 429 => {
let retry: u64 = r
.header("retry-after")
.and_then(|h| h.parse().ok())
.unwrap_or(5);
eprintln!("403: {}", r.into_string()?);
thread::sleep(Duration::from_secs(retry));
}
r => return r,
}
result = fetch();
}
println!("Failed after 5 tries: {:?}", &result);
result
} }
fn get_many(urls: Vec<String>, simultaneous_fetches: usize) -> Result<()> { fn get_many(urls: Vec<String>, simultaneous_fetches: usize) -> Result<()> {

View File

@@ -44,7 +44,7 @@ pub struct Error {
message: Option<String>, message: Option<String>,
url: Option<Url>, url: Option<Url>,
source: Option<Box<dyn error::Error + Send + Sync + 'static>>, source: Option<Box<dyn error::Error + Send + Sync + 'static>>,
response: Option<Response>, pub response: Option<Response>,
} }
impl Display for Error { impl Display for Error {