Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Fix linter warnings and formatting #6

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 34 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,70 +1,79 @@
#[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/<url>")]
async fn download(url: &str) -> Redirect {
if !url.starts_with("https://www.studydrive.net/") {
return Redirect::to("/not_found");
}
let doc_id = url
.split("/")
.split('/')
.last()
.unwrap()
.split("?")
.split('?')
.collect::<Vec<_>>()[0];

println!("doc_id={}", doc_id);

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<String, Box<dyn std::error::Error>> {
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<serde_json::Value, Box<dyn std::error::Error>>{
async fn send_get_request(doc_id: &str) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
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::<serde_json::Value>().await.expect("Failed to parse json");

let json = response
.json::<serde_json::Value>()
.await
.expect("Failed to parse json");

Ok(json)
}

Expand Down
Loading