Reformat; Handle AuthorizationTimeout for p2g presentations

This commit is contained in:
2017-06-17 03:32:46 +02:00
parent 07d77ab0f6
commit 4e71132674
3 changed files with 95 additions and 108 deletions

View File

@@ -144,11 +144,13 @@ fn get_auth() {
let res = try_to_get_valid_response(
|client| {
client
.post("https://streams.tum.de/Mediasite/Login")
.form(&form_data)
client.post("https://streams.tum.de/Mediasite/Login").form(
&form_data,
)
},
|res| res.headers().get::<SetCookie>().is_some(),
).expect(
"Didn't receive a valid response trying to login! Maybe wrong login data?",
);
// FIXME: We're somehow only getting "302 Object moved" instead of the actual response
// => We can't determine if the login was successful
@@ -258,26 +260,35 @@ fn try_to_get_response<F>(f: F) -> Response
where
F: Fn(&Client) -> RequestBuilder,
{
try_to_get_valid_response(f, |_| true)
try_to_get_valid_response(f, |_| true).unwrap()
}
fn try_to_get_valid_response<F1, F2>(f1: F1, f2: F2) -> Response
fn try_to_get_valid_response<F1, F2>(f1: F1, f2: F2) -> Result<Response, reqwest::Result<Response>>
where
F1: Fn(&Client) -> RequestBuilder,
F2: Fn(&Response) -> bool,
{
let mut last_response = None;
for retries in 0..MAX_RETRIES {
if retries > 0 {
println!("Retrying request!");
}
let response = f1(&*CLIENT).send();
if let Ok(response) = response {
let response_res = f1(&*CLIENT).send();
if let Ok(response) = response_res {
if f2(&response) {
return response;
return Ok(response);
} else {
last_response = Some(Ok(response));
}
} else {
last_response = Some(response_res);
}
}
panic!("Reached maximum amount of retries!")
println!(
"Failed to get valid response! (tried {} times)",
MAX_RETRIES
);
Err(last_response.unwrap())
}
fn read_response_body(response: &mut Response) -> String {

View File

@@ -57,11 +57,18 @@ impl Presentation {
match *self {
Presentation::PublishToGo { ref download_url, .. } => {
let mut response = try_to_get_valid_response(
let response_res = try_to_get_valid_response(
|client| client.get(download_url).header(construct_cookie()),
|resp| resp.headers().get::<ContentDisposition>().is_some(),
);
if response_res.is_err() {
// Appears to be the reason...
return Err(DownloadError::AuthorizationTimeout);
}
let mut response = response_res.unwrap();
let filename = {
let content_disposition =
response.headers().get::<ContentDisposition>().unwrap();
@@ -130,7 +137,7 @@ impl Presentation {
let mut response = try_to_get_valid_response(
|client| client.get(download_url).header(construct_cookie()),
|resp| resp.status() == &StatusCode::Ok,
);
).unwrap();
// TODO: Support formats besides mp4
// extract extension from url or somewehre else...
let filename = format!("{} - {}.mp4", fix_filename(self.name()), i + 1);