Reformat code according to new RFC style

This commit is contained in:
2017-06-04 20:01:37 +02:00
parent ffc0cc8a8c
commit 63e4fd3b07
2 changed files with 105 additions and 80 deletions

View File

@@ -26,8 +26,6 @@ use presentation::Presentation;
// TODO: Old "Publish To Go" packages get sweeped from the server and you have to request a new one;
// Implement this
// TODO: sometimes you need to access the video listing at least once using moodle; emulate this
// TODO: Add aliases with semester postfix so other can contribute aliases; move to separate file
// split into files in general?
const MAX_RETRIES: u8 = 10;
@@ -54,26 +52,36 @@ fn main() {
let matches = App::new("TumMediasiteDownloader")
.author("Boris-Chengbiao Zhou <bobo1239@web.de>")
.about("Downloads \'catalogs\' from the TUM's Mediasite lecture archive.")
.arg(Arg::with_name("CATALOG_NAME")
.help("name of the catalog e.g. from the URL:\n\
.arg(
Arg::with_name("CATALOG_NAME")
.help(
"name of the catalog e.g. from the URL:\n\
https://streams.tum.de/Mediasite/Catalog/catalogs/era-2016 -> era-2016\n\
special cases (WS16/17; login included): DS, EIDI, ERA")
.required(true)
.index(1))
.arg(Arg::with_name("OUTPUT_DIRECTORY")
.help("where to output the downloaded files")
.required(true)
.index(2))
.arg(Arg::with_name("username")
.short("u")
.help("username for login; can be omitted if the user from .env should be used",)
.requires("password")
.takes_value(true))
.arg(Arg::with_name("password")
.short("p")
.help("password for login; can be omitted if the user from .env should be used",)
.requires("username")
.takes_value(true))
special cases (WS16/17; login included): DS, EIDI, ERA, ..."
)
.required(true)
.index(1)
)
.arg(
Arg::with_name("OUTPUT_DIRECTORY")
.help("where to output the downloaded files")
.required(true)
.index(2)
)
.arg(
Arg::with_name("username")
.short("u")
.help("username for login; can be omitted if the user from .env should be used")
.requires("password")
.takes_value(true)
)
.arg(
Arg::with_name("password")
.short("p")
.help("password for login; can be omitted if the user from .env should be used")
.requires("username")
.takes_value(true)
)
.get_matches();
let catalog_name = matches.value_of("CATALOG_NAME").unwrap();
@@ -128,12 +136,14 @@ fn get_auth() {
form_data.insert("UserName", &*username);
form_data.insert("Password", &*password);
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 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(),
);
// FIXME: We're somehow only getting "302 Object moved" instead of the actual response
// => We can't determine if the login was successful
// (we still get a MediasiteAuth cookie that is useless)
@@ -156,14 +166,18 @@ fn download_catalog(catalog_name: &str, out_dir: &Path) {
let catalog_id = get_catalog_id(catalog_name);
let json = get_json(&catalog_id);
let presentations = json_to_presentations(&json);
println!("Starting to download {} presentations!",
presentations.len());
println!(
"Starting to download {} presentations!",
presentations.len()
);
for (i, presentation) in presentations.iter().enumerate() {
println!("\nDownloading {}/{}: {}",
i + 1,
presentations.len(),
presentation.name());
println!(
"\nDownloading {}/{}: {}",
i + 1,
presentations.len(),
presentation.name()
);
for _ in 0..MAX_RETRIES {
match presentation.download(out_dir) {
Ok(()) => break,
@@ -189,8 +203,10 @@ fn get_catalog_id(name: &str) -> String {
let prefix = "CatalogId: '";
let idx = body.find(prefix)
.expect("Failed to find CatalogId on the catalog page! Perhaps you got the wrong catalog \
name or an invalid login? Maybe you need to open the page in a browser once...");
.expect(
"Failed to find CatalogId on the catalog page! Perhaps you got the wrong catalog \
name or an invalid login? Maybe you need to open the page in a browser once..."
);
let pre_len = prefix.len();
// Assuming all catalog ids follow this pattern!
let len = "a6fca0c1-0be4-4e66-83b7-bcdc4eb5e95e".len();
@@ -218,12 +234,14 @@ fn get_json(catalog_id: &str) -> String {
data.insert("CurrentFolderId", catalog_id);
data.insert("ItemsPerPage", "500");
let mut res = try_to_get_response(|client| {
client
.post("https://streams.tum.de/Mediasite/Catalog/Data/GetPresentationsForFolder")
.header(construct_cookie())
.json(&data)
});
let mut res = try_to_get_response(
|client| {
client
.post("https://streams.tum.de/Mediasite/Catalog/Data/GetPresentationsForFolder")
.header(construct_cookie())
.json(&data)
}
);
read_response_body(&mut res)
}
@@ -232,14 +250,16 @@ fn construct_cookie() -> Cookie {
}
fn try_to_get_response<F>(f: F) -> Response
where F: Fn(&Client) -> RequestBuilder
where
F: Fn(&Client) -> RequestBuilder,
{
try_to_get_valid_response(f, |_| true)
}
fn try_to_get_valid_response<F1, F2>(f1: F1, f2: F2) -> Response
where F1: Fn(&Client) -> RequestBuilder,
F2: Fn(&Response) -> bool
where
F1: Fn(&Client) -> RequestBuilder,
F2: Fn(&Response) -> bool,
{
for retries in 0..MAX_RETRIES {
if retries > 0 {