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: use clap for proper CLI
|
||||||
// TODO: replace unwraps with descriptive expects
|
|
||||||
|
|
||||||
const MAX_RETRIES: u8 = 10;
|
const MAX_RETRIES: u8 = 10;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref CLIENT: Client = {
|
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
|
// The login site redirects to itself if no redirect parameter is given
|
||||||
client.redirect(RedirectPolicy::none());
|
client.redirect(RedirectPolicy::none());
|
||||||
client
|
client
|
||||||
@@ -55,8 +54,14 @@ struct Presentation {
|
|||||||
impl<'a> From<&'a JsonValue> for Presentation {
|
impl<'a> From<&'a JsonValue> for Presentation {
|
||||||
fn from(json: &'a JsonValue) -> Presentation {
|
fn from(json: &'a JsonValue) -> Presentation {
|
||||||
Presentation {
|
Presentation {
|
||||||
name: json["Name"].clone().take_string().unwrap(),
|
name: json["Name"]
|
||||||
download_url: json["DownloadUrls"][0]["Url"].clone().take_string().unwrap(),
|
.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() {
|
if path.exists() {
|
||||||
assert!(path.is_dir());
|
assert!(path.is_dir());
|
||||||
} else {
|
} else {
|
||||||
::std::fs::create_dir_all(path).unwrap();
|
::std::fs::create_dir_all(path).expect("Failed to create output directory!");
|
||||||
}
|
}
|
||||||
path.to_owned()
|
path.to_owned()
|
||||||
}
|
}
|
||||||
@@ -108,13 +113,15 @@ fn get_auth(username: &str, password: &str) -> String {
|
|||||||
form_data.insert("UserName", username);
|
form_data.insert("UserName", username);
|
||||||
form_data.insert("Password", password);
|
form_data.insert("Password", password);
|
||||||
|
|
||||||
let res = try_to_get_response(|client| {
|
let res = try_to_get_valid_response(|client| {
|
||||||
client.post("https://streams.tum.de/Mediasite/Login")
|
client.post("https://streams.tum.de/Mediasite/Login")
|
||||||
.form(&form_data)
|
.form(&form_data)
|
||||||
});
|
},
|
||||||
|
|res| res.headers().get::<SetCookie>().is_some());
|
||||||
|
|
||||||
let set_cookie: &SetCookie = res.headers().get().unwrap();
|
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");
|
assert_eq!(cookie.name(), "MediasiteAuth");
|
||||||
|
|
||||||
cookie.value().to_string()
|
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 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 res = try_to_get_response(|client| client.get(&url).header(construct_cookie(auth)));
|
||||||
let mut string = String::new();
|
let body = read_response_body(&mut res);
|
||||||
res.read_to_string(&mut string).unwrap();
|
|
||||||
|
|
||||||
let prefix = "CatalogId: '";
|
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();
|
let pre_len = prefix.len();
|
||||||
// Assuming all catalog ids follow this pattern!
|
// Assuming all catalog ids follow this pattern!
|
||||||
let len = "a6fca0c1-0be4-4e66-83b7-bcdc4eb5e95e".len();
|
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) {
|
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 {
|
match *param {
|
||||||
DispositionParam::Filename(ref charset, _, ref vec) => {
|
DispositionParam::Filename(ref charset, _, ref vec) => {
|
||||||
assert_eq!(&Charset::Ext("UTF-8".to_string()), charset);
|
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,
|
_ => continue,
|
||||||
}
|
}
|
||||||
@@ -191,7 +198,7 @@ fn download_presentation(presentation: &Presentation, out_dir: &Path, auth: &str
|
|||||||
// http://stackoverflow.com/questions/3428102/
|
// 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 reader = BufReader::new(response);
|
||||||
let mut buf = [0u8; 8 * 1024];
|
let mut buf = [0u8; 8 * 1024];
|
||||||
@@ -208,7 +215,7 @@ fn download_presentation(presentation: &Presentation, out_dir: &Path, auth: &str
|
|||||||
Err(e) => panic!(e),
|
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;
|
done += len;
|
||||||
|
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
@@ -216,7 +223,7 @@ fn download_presentation(presentation: &Presentation, out_dir: &Path, auth: &str
|
|||||||
print!("{:.*} kB/s\r",
|
print!("{:.*} kB/s\r",
|
||||||
2,
|
2,
|
||||||
done as f64 * 1000.0 / update_duration_millis as f64 / 1024.0);
|
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;
|
last = now;
|
||||||
done = 0;
|
done = 0;
|
||||||
}
|
}
|
||||||
@@ -232,7 +239,7 @@ fn fix_filename(string: &str) -> String {
|
|||||||
|
|
||||||
fn json_to_presentations(json_str: &str) -> Vec<Presentation> {
|
fn json_to_presentations(json_str: &str) -> Vec<Presentation> {
|
||||||
let mut vec = Vec::new();
|
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;
|
let mut count = 0;
|
||||||
for presentation in json["PresentationDetailsList"].members_mut() {
|
for presentation in json["PresentationDetailsList"].members_mut() {
|
||||||
assert_eq!(1, presentation["DownloadUrls"].len());
|
assert_eq!(1, presentation["DownloadUrls"].len());
|
||||||
@@ -256,9 +263,7 @@ fn get_json(catalog_id: &str, auth: &str) -> String {
|
|||||||
.header(construct_cookie(auth))
|
.header(construct_cookie(auth))
|
||||||
.json(&data)
|
.json(&data)
|
||||||
});
|
});
|
||||||
let mut string = String::new();
|
read_response_body(&mut res)
|
||||||
res.read_to_string(&mut string).unwrap();
|
|
||||||
string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn construct_cookie(auth: &str) -> Cookie {
|
fn construct_cookie(auth: &str) -> Cookie {
|
||||||
@@ -268,7 +273,7 @@ fn construct_cookie(auth: &str) -> Cookie {
|
|||||||
fn zip_is_valid(path: &Path) -> bool {
|
fn zip_is_valid(path: &Path) -> bool {
|
||||||
assert!(path.exists());
|
assert!(path.exists());
|
||||||
assert!(path.is_file());
|
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()
|
ZipArchive::new(file).is_ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,5 +300,11 @@ fn try_to_get_valid_response<F1, F2>(f1: F1, f2: F2) -> Response
|
|||||||
}
|
}
|
||||||
retries += 1;
|
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