Replace unwraps with more descriptive expects
This commit is contained in:
59
src/main.rs
59
src/main.rs
@@ -33,13 +33,12 @@ mod catalogs {
|
||||
}
|
||||
|
||||
// TODO: use clap for proper CLI
|
||||
// TODO: replace unwraps with descriptive expects
|
||||
|
||||
const MAX_RETRIES: u8 = 10;
|
||||
|
||||
lazy_static! {
|
||||
static ref CLIENT: Client = {
|
||||
let mut client = Client::new().unwrap();
|
||||
let mut client = Client::new().expect("Failed to create Client!");
|
||||
// The login site redirects to itself if no redirect parameter is given
|
||||
client.redirect(RedirectPolicy::none());
|
||||
client
|
||||
@@ -55,8 +54,14 @@ struct Presentation {
|
||||
impl<'a> From<&'a JsonValue> for Presentation {
|
||||
fn from(json: &'a JsonValue) -> Presentation {
|
||||
Presentation {
|
||||
name: json["Name"].clone().take_string().unwrap(),
|
||||
download_url: json["DownloadUrls"][0]["Url"].clone().take_string().unwrap(),
|
||||
name: json["Name"]
|
||||
.clone()
|
||||
.take_string()
|
||||
.expect("json: Presentation didn't contain a \"Name\"!"),
|
||||
download_url: json["DownloadUrls"][0]["Url"]
|
||||
.clone()
|
||||
.take_string()
|
||||
.expect("json: Presentation didn't have a download url!"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,7 +93,7 @@ fn get_output_directory() -> PathBuf {
|
||||
if path.exists() {
|
||||
assert!(path.is_dir());
|
||||
} else {
|
||||
::std::fs::create_dir_all(path).unwrap();
|
||||
::std::fs::create_dir_all(path).expect("Failed to create output directory!");
|
||||
}
|
||||
path.to_owned()
|
||||
}
|
||||
@@ -108,13 +113,15 @@ fn get_auth(username: &str, password: &str) -> String {
|
||||
form_data.insert("UserName", username);
|
||||
form_data.insert("Password", password);
|
||||
|
||||
let res = try_to_get_response(|client| {
|
||||
client.post("https://streams.tum.de/Mediasite/Login")
|
||||
.form(&form_data)
|
||||
});
|
||||
let res = try_to_get_valid_response(|client| {
|
||||
client.post("https://streams.tum.de/Mediasite/Login")
|
||||
.form(&form_data)
|
||||
},
|
||||
|res| res.headers().get::<SetCookie>().is_some());
|
||||
|
||||
let set_cookie: &SetCookie = res.headers().get().unwrap();
|
||||
let cookie = cookie::Cookie::parse(set_cookie.0[0].to_string()).unwrap();
|
||||
let cookie = cookie::Cookie::parse(set_cookie.0[0].to_string())
|
||||
.expect("Failed to parse SetCookie");
|
||||
assert_eq!(cookie.name(), "MediasiteAuth");
|
||||
|
||||
cookie.value().to_string()
|
||||
@@ -141,16 +148,15 @@ fn get_catalog_id(name: &str, auth: &str) -> String {
|
||||
|
||||
let url = format!("https://streams.tum.de/Mediasite/Catalog/catalogs/{}", name);
|
||||
let mut res = try_to_get_response(|client| client.get(&url).header(construct_cookie(auth)));
|
||||
let mut string = String::new();
|
||||
res.read_to_string(&mut string).unwrap();
|
||||
let body = read_response_body(&mut res);
|
||||
|
||||
let prefix = "CatalogId: '";
|
||||
let idx = string.find(prefix).unwrap();
|
||||
let idx = body.find(prefix).expect("Failed to find CatalogId on the catalog page!");
|
||||
let pre_len = prefix.len();
|
||||
// Assuming all catalog ids follow this pattern!
|
||||
let len = "a6fca0c1-0be4-4e66-83b7-bcdc4eb5e95e".len();
|
||||
|
||||
string[(idx + pre_len)..(idx + pre_len + len)].to_string()
|
||||
body[(idx + pre_len)..(idx + pre_len + len)].to_string()
|
||||
}
|
||||
|
||||
fn download_presentation(presentation: &Presentation, out_dir: &Path, auth: &str) {
|
||||
@@ -169,7 +175,8 @@ fn download_presentation(presentation: &Presentation, out_dir: &Path, auth: &str
|
||||
match *param {
|
||||
DispositionParam::Filename(ref charset, _, ref vec) => {
|
||||
assert_eq!(&Charset::Ext("UTF-8".to_string()), charset);
|
||||
name = Some(String::from_utf8(vec.to_vec()).unwrap());
|
||||
name = Some(String::from_utf8(vec.to_vec())
|
||||
.expect("Suggested filename isn't valid UTF-8!"));
|
||||
}
|
||||
_ => continue,
|
||||
}
|
||||
@@ -191,7 +198,7 @@ fn download_presentation(presentation: &Presentation, out_dir: &Path, auth: &str
|
||||
// http://stackoverflow.com/questions/3428102/
|
||||
}
|
||||
}
|
||||
let mut file = File::create(&path).unwrap();
|
||||
let mut file = File::create(&path).expect("Failed to create file!");
|
||||
|
||||
let mut reader = BufReader::new(response);
|
||||
let mut buf = [0u8; 8 * 1024];
|
||||
@@ -208,7 +215,7 @@ fn download_presentation(presentation: &Presentation, out_dir: &Path, auth: &str
|
||||
Err(e) => panic!(e),
|
||||
};
|
||||
|
||||
file.write_all(&buf[0..len]).unwrap();
|
||||
file.write_all(&buf[0..len]).expect("Failed writing into the file!");
|
||||
done += len;
|
||||
|
||||
let now = Instant::now();
|
||||
@@ -216,7 +223,7 @@ fn download_presentation(presentation: &Presentation, out_dir: &Path, auth: &str
|
||||
print!("{:.*} kB/s\r",
|
||||
2,
|
||||
done as f64 * 1000.0 / update_duration_millis as f64 / 1024.0);
|
||||
std::io::stdout().flush().unwrap();
|
||||
std::io::stdout().flush().expect("Failed flushing the terminal!");
|
||||
last = now;
|
||||
done = 0;
|
||||
}
|
||||
@@ -232,7 +239,7 @@ fn fix_filename(string: &str) -> String {
|
||||
|
||||
fn json_to_presentations(json_str: &str) -> Vec<Presentation> {
|
||||
let mut vec = Vec::new();
|
||||
let mut json = json::parse(json_str).unwrap();
|
||||
let mut json = json::parse(json_str).expect("Failed parsing the json!");
|
||||
let mut count = 0;
|
||||
for presentation in json["PresentationDetailsList"].members_mut() {
|
||||
assert_eq!(1, presentation["DownloadUrls"].len());
|
||||
@@ -256,9 +263,7 @@ fn get_json(catalog_id: &str, auth: &str) -> String {
|
||||
.header(construct_cookie(auth))
|
||||
.json(&data)
|
||||
});
|
||||
let mut string = String::new();
|
||||
res.read_to_string(&mut string).unwrap();
|
||||
string
|
||||
read_response_body(&mut res)
|
||||
}
|
||||
|
||||
fn construct_cookie(auth: &str) -> Cookie {
|
||||
@@ -268,7 +273,7 @@ fn construct_cookie(auth: &str) -> Cookie {
|
||||
fn zip_is_valid(path: &Path) -> bool {
|
||||
assert!(path.exists());
|
||||
assert!(path.is_file());
|
||||
let file = File::open(path).unwrap();
|
||||
let file = File::open(path).expect("Failed opening the zip file!");
|
||||
ZipArchive::new(file).is_ok()
|
||||
}
|
||||
|
||||
@@ -295,5 +300,11 @@ fn try_to_get_valid_response<F1, F2>(f1: F1, f2: F2) -> Response
|
||||
}
|
||||
retries += 1;
|
||||
}
|
||||
panic!("Reached maximum amout of retries!")
|
||||
panic!("Reached maximum amount of retries!")
|
||||
}
|
||||
|
||||
fn read_response_body(response: &mut Response) -> String {
|
||||
let mut string = String::new();
|
||||
response.read_to_string(&mut string).expect("Failed to read body");
|
||||
string
|
||||
}
|
||||
|
Reference in New Issue
Block a user