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

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Gobidev committed Mar 8, 2024
1 parent a7c765c commit a388e64
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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/<url>")]
async fn download(url: &str) -> Redirect {
Expand All @@ -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<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

0 comments on commit a388e64

Please sign in to comment.