cargo fmt

This commit is contained in:
theBreadCompany 2024-09-23 13:48:23 +02:00
parent 5f059a7b8e
commit 3a35b2100c
3 changed files with 58 additions and 37 deletions

View file

@ -1,10 +1,11 @@
#[macro_use] extern crate rocket;
#[macro_use]
extern crate rocket;
use rocket::response::content::RawHtml;
use rocket::http::Status;
use reqwest::Client;
use scraper::{Html, Selector};
use maud::{html, DOCTYPE};
use reqwest::Client;
use rocket::http::Status;
use rocket::response::content::RawHtml;
use scraper::{Html, Selector};
#[get("/<path..>")]
async fn handle_route(path: std::path::PathBuf) -> Result<RawHtml<String>, Status> {
@ -50,7 +51,10 @@ async fn create_page(source: &String, html: &String) -> Result<String, Status> {
let illust = json::parse(data_meta.value().attr("content").unwrap()).unwrap();
let image = match illust["illust"].entries().next() {
Some (j) => j.1["urls"]["regular"].as_str().unwrap().replace("pximg.net", "fixiv.net"),
Some(j) => j.1["urls"]["regular"]
.as_str()
.unwrap()
.replace("pximg.net", "fixiv.net"),
None => "https://http.cat/images/501.jpg".to_string(),
};
@ -60,7 +64,7 @@ async fn create_page(source: &String, html: &String) -> Result<String, Status> {
};
let title_meta = match dom.select(&title_selector).next() {
Some(meta) => meta.attr("content").unwrap(),
None => "unknown title"
None => "unknown title",
};
let desc_selector = match Selector::parse(r#"meta[property="og:description"]"#) {
Ok(sel) => sel,
@ -68,7 +72,7 @@ async fn create_page(source: &String, html: &String) -> Result<String, Status> {
};
let desc_meta = match dom.select(&desc_selector).next() {
Some(meta) => meta.attr("content").unwrap(),
None => "unknown title"
None => "unknown title",
};
Ok(html! {
@ -101,7 +105,8 @@ async fn create_page(source: &String, html: &String) -> Result<String, Status> {
a href=(source) { "refresh manually" }
}
}
}.into_string())
}
.into_string())
}
#[launch]

View file

@ -3,9 +3,9 @@ extern crate reqwest;
extern crate serde_json;
use chrono::Local;
use reqwest::header::{HeaderName, AUTHORIZATION, CONTENT_TYPE, USER_AGENT};
use serde_json::Value;
use std::sync::{Arc, Mutex};
use reqwest::header::{HeaderName, AUTHORIZATION, CONTENT_TYPE, USER_AGENT};
mod models;
@ -41,7 +41,10 @@ impl PixivAppClient {
let client_id = "MOBrBDS8blbauoSck0ZfDbtuzpyT";
let client_secret = "lsACyCD94FhDUtGTXi3QzcFE2uU1hqtDaKeqrdwj";
let hash_input = format!("{}{}\n", &time_str, "28c1fdd170a5204386cb1313c7077b34f83e4aaf4aa829ce78c231e05b0bae2c");
let hash_input = format!(
"{}{}\n",
&time_str, "28c1fdd170a5204386cb1313c7077b34f83e4aaf4aa829ce78c231e05b0bae2c"
);
let hash = PixivAppClient::md5(hash_input.as_str());
let req = self.http_client
@ -54,37 +57,50 @@ impl PixivAppClient {
.build()
.expect("failed to build login request");
let r = match self.http_client
.execute(req)
.await {
let r = match self.http_client.execute(req).await {
Ok(r) => r.text().await.unwrap(),
Err(_e) => return
Err(_e) => return,
};
let d: Value = serde_json::from_str(&r).unwrap();
assert!(!d["response"]["access_token"].is_null());
assert!(!d["response"]["refresh_token"].is_null());
self.access_token = Arc::new(Mutex::new(String::from(d["response"]["access_token"].as_str().unwrap())));
self.refresh_token = Arc::new(Mutex::new(String::from(d["response"]["refresh_token"].as_str().unwrap())));
self.access_token = Arc::new(Mutex::new(String::from(
d["response"]["access_token"].as_str().unwrap(),
)));
self.refresh_token = Arc::new(Mutex::new(String::from(
d["response"]["refresh_token"].as_str().unwrap(),
)));
}
pub async fn illust_details(&self, illust_id: u32) -> Result<models::Illustration, Box<dyn std::error::Error>> {
pub async fn illust_details(
&self,
illust_id: u32,
) -> Result<models::Illustration, Box<dyn std::error::Error>> {
let url = format!("{}/v1/illust/detail", self.host);
let illust_id_str = illust_id.to_string();
let req = self.http_client
let req = self
.http_client
.get(url)
.header(AUTHORIZATION, format!("Bearer {}", self.access_token.lock().unwrap()))
.header(
AUTHORIZATION,
format!("Bearer {}", self.access_token.lock().unwrap()),
)
.header(HeaderName::from_lowercase(b"app-os").unwrap(), "ios")
.header(HeaderName::from_lowercase(b"app-os-version").unwrap(), "12.2")
.header(
HeaderName::from_lowercase(b"app-os-version").unwrap(),
"12.2",
)
.header(HeaderName::from_lowercase(b"app-version").unwrap(), "7.6.2")
.header(USER_AGENT, "PixivIOSApp/7.6.2 (iOS 12.2; iPhone9,1)")
.query(&[("illust_id", illust_id_str.as_str())])
.build()
.expect("failed to build request");
let r = self.http_client
let r = self
.http_client
.execute(req)
.await
.unwrap()
@ -99,26 +115,24 @@ impl PixivAppClient {
Err(e) => return Err(Box::from(e)),
};
match deserialized.illust {
Some(r) => Ok(r),
None => Err(Box::from(deserialized.error.unwrap()))
None => Err(Box::from(deserialized.error.unwrap())),
}
}
}
#[cfg(test)]
mod client_tests {
use super::*;
use std::{assert, assert_eq, panic, env};
use std::{assert, assert_eq, env, panic};
#[tokio::test]
async fn login() {
let token = env::var("PIXIV_REFRESH_TOKEN");
let mut client = PixivAppClient::new(&token.expect("expecting PIXIV_REFRESH_TOKEN variable for testing!"));
let mut client = PixivAppClient::new(
&token.expect("expecting PIXIV_REFRESH_TOKEN variable for testing!"),
);
client.refresh_token().await;
let cloned_access_token = Arc::clone(&client.access_token);
match cloned_access_token.lock() {
@ -131,7 +145,9 @@ mod client_tests {
async fn illust_details() {
let illust_id = 122388293;
let token = env::var("PIXIV_REFRESH_TOKEN");
let mut client = PixivAppClient::new(&token.expect("expecting PIXIV_REFRESH_TOKEN variable for testing!"));
let mut client = PixivAppClient::new(
&token.expect("expecting PIXIV_REFRESH_TOKEN variable for testing!"),
);
client.refresh_token().await;
let illust = client.illust_details(illust_id).await;
assert!(illust.is_ok(), "Expected illustration data: {:#?}", illust);

View file

@ -35,7 +35,7 @@ pub struct User {
name: String,
account: String,
profile_image_urls: UserProfileImages,
is_followed: bool
is_followed: bool,
}
#[derive(Serialize, Deserialize, std::fmt::Debug)]