diff --git a/fxpixiv/src/main.rs b/fxpixiv/src/main.rs index 667e2d6..3250570 100644 --- a/fxpixiv/src/main.rs +++ b/fxpixiv/src/main.rs @@ -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("/")] async fn handle_route(path: std::path::PathBuf) -> Result, Status> { @@ -12,7 +13,7 @@ async fn handle_route(path: std::path::PathBuf) -> Result, Statu let html = match fetch_content(&target).await { Ok(html) => html, - Err(err) => return Err(err), + Err(err) => return Err(err), }; let modified = create_page(&target, &html).await.unwrap(); @@ -32,7 +33,7 @@ async fn fetch_content(url: &String) -> Result { Ok(text) => text, Err(_) => return Err(Status::InternalServerError), }; - + Ok(html) } @@ -50,7 +51,10 @@ async fn create_page(source: &String, html: &String) -> Result { 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 { }; 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 { }; 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 { a href=(source) { "refresh manually" } } } - }.into_string()) + } + .into_string()) } #[launch] diff --git a/libpixiv/src/lib.rs b/libpixiv/src/lib.rs index bd4d530..530548d 100644 --- a/libpixiv/src/lib.rs +++ b/libpixiv/src/lib.rs @@ -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> { + pub async fn illust_details( + &self, + illust_id: u32, + ) -> Result> { 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,10 +145,12 @@ 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); assert_eq!(illust.unwrap().id, illust_id); } -} \ No newline at end of file +} diff --git a/libpixiv/src/models.rs b/libpixiv/src/models.rs index 6745db0..d680679 100644 --- a/libpixiv/src/models.rs +++ b/libpixiv/src/models.rs @@ -15,7 +15,7 @@ pub struct Illustration { pub tools: Vec, pub series: Option, pub restrict: i32, - pub x_restrict: i32, + pub x_restrict: i32, pub image_urls: IllustrationURLs, pub meta_pages: Vec, pub total_view: u32, @@ -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)] @@ -81,4 +81,4 @@ impl std::fmt::Display for PixivError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "{}: {}", self.user_message, self.reason) } -} \ No newline at end of file +}