From a388e6468da486db323d7efd2dfd5b253520e48c Mon Sep 17 00:00:00 2001 From: Adrian Groh Date: Fri, 8 Mar 2024 11:32:37 +0100 Subject: [PATCH] Formatting --- src/main.rs | 55 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index f1eb6f0..0796ea9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ #[macro_use] extern crate rocket; +use reqwest::header::{HeaderMap, HeaderValue}; use rocket::fs::FileServer; use rocket::response::Redirect; -use reqwest::header::{HeaderMap, HeaderValue}; #[get("/download/")] async fn download(url: &str) -> Redirect { @@ -21,50 +21,59 @@ async fn download(url: &str) -> Redirect { let json = send_get_request(doc_id).await.unwrap(); let data = json["data"].as_object().unwrap(); - if data["file_preview"].is_null(){ + if data["file_preview"].is_null() { println!("no token found"); let json = send_get_request(doc_id).await.unwrap(); let token = get_token().await.unwrap(); let data = json["data"].as_object().unwrap(); let name = data["filename"].as_str().unwrap(); - let ending=name.split('.') - .last() - .unwrap(); - return Redirect::to(format!("https://cdn.studydrive.net/d/prod/documents/{}/original/{}.{}?token={}", doc_id, doc_id,ending, token)); + let ending = name.split('.').last().unwrap(); + return Redirect::to(format!( + "https://cdn.studydrive.net/d/prod/documents/{}/original/{}.{}?token={}", + doc_id, doc_id, ending, token + )); } let name = data["filename"].as_str().unwrap(); - let ending=name.split('.') - .last() - .unwrap(); + let ending = name.split('.').last().unwrap(); let preview = data["file_preview"].as_str().unwrap(); - let token = preview.split("token=") - .last() - .unwrap(); - Redirect::to(format!("https://cdn.studydrive.net/d/prod/documents/{}/original/{}.{}?token={}", doc_id, doc_id, ending, token)) + let token = preview.split("token=").last().unwrap(); + Redirect::to(format!( + "https://cdn.studydrive.net/d/prod/documents/{}/original/{}.{}?token={}", + doc_id, doc_id, ending, token + )) } async fn get_token() -> Result> { - let doc_id= "1617040"; + let doc_id = "1617040"; let json = send_get_request(doc_id).await?; - + let data = json["data"].as_object().unwrap(); let preview = data["file_preview"].as_str().unwrap(); - let token = preview.split("token=") - .last() - .unwrap(); + let token = preview.split("token=").last().unwrap(); Ok(token.to_string()) } -async fn send_get_request(doc_id: &str) -> Result>{ +async fn send_get_request(doc_id: &str) -> Result> { let url = format!("https://www.studydrive.net/document/{}", doc_id); let mut headers = HeaderMap::new(); - headers.insert("X-Requested-With", HeaderValue::from_static("XMLHttpRequest")); + headers.insert( + "X-Requested-With", + HeaderValue::from_static("XMLHttpRequest"), + ); let client = reqwest::Client::new(); - let response = client.get(&url).headers(headers).send().await.expect("Failed to send request"); + let response = client + .get(&url) + .headers(headers) + .send() + .await + .expect("Failed to send request"); // handle the response as json - let json = response.json::().await.expect("Failed to parse json"); - + let json = response + .json::() + .await + .expect("Failed to parse json"); + Ok(json) }