cargo fmt
This commit is contained in:
parent
5f059a7b8e
commit
3a35b2100c
3 changed files with 58 additions and 37 deletions
|
@ -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 maud::{html, DOCTYPE};
|
||||||
|
use reqwest::Client;
|
||||||
|
use rocket::http::Status;
|
||||||
|
use rocket::response::content::RawHtml;
|
||||||
|
use scraper::{Html, Selector};
|
||||||
|
|
||||||
#[get("/<path..>")]
|
#[get("/<path..>")]
|
||||||
async fn handle_route(path: std::path::PathBuf) -> Result<RawHtml<String>, Status> {
|
async fn handle_route(path: std::path::PathBuf) -> Result<RawHtml<String>, Status> {
|
||||||
|
@ -12,7 +13,7 @@ async fn handle_route(path: std::path::PathBuf) -> Result<RawHtml<String>, Statu
|
||||||
|
|
||||||
let html = match fetch_content(&target).await {
|
let html = match fetch_content(&target).await {
|
||||||
Ok(html) => html,
|
Ok(html) => html,
|
||||||
Err(err) => return Err(err),
|
Err(err) => return Err(err),
|
||||||
};
|
};
|
||||||
|
|
||||||
let modified = create_page(&target, &html).await.unwrap();
|
let modified = create_page(&target, &html).await.unwrap();
|
||||||
|
@ -32,7 +33,7 @@ async fn fetch_content(url: &String) -> Result<String, Status> {
|
||||||
Ok(text) => text,
|
Ok(text) => text,
|
||||||
Err(_) => return Err(Status::InternalServerError),
|
Err(_) => return Err(Status::InternalServerError),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(html)
|
Ok(html)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 illust = json::parse(data_meta.value().attr("content").unwrap()).unwrap();
|
||||||
let image = match illust["illust"].entries().next() {
|
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(),
|
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() {
|
let title_meta = match dom.select(&title_selector).next() {
|
||||||
Some(meta) => meta.attr("content").unwrap(),
|
Some(meta) => meta.attr("content").unwrap(),
|
||||||
None => "unknown title"
|
None => "unknown title",
|
||||||
};
|
};
|
||||||
let desc_selector = match Selector::parse(r#"meta[property="og:description"]"#) {
|
let desc_selector = match Selector::parse(r#"meta[property="og:description"]"#) {
|
||||||
Ok(sel) => sel,
|
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() {
|
let desc_meta = match dom.select(&desc_selector).next() {
|
||||||
Some(meta) => meta.attr("content").unwrap(),
|
Some(meta) => meta.attr("content").unwrap(),
|
||||||
None => "unknown title"
|
None => "unknown title",
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(html! {
|
Ok(html! {
|
||||||
|
@ -101,7 +105,8 @@ async fn create_page(source: &String, html: &String) -> Result<String, Status> {
|
||||||
a href=(source) { "refresh manually" }
|
a href=(source) { "refresh manually" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.into_string())
|
}
|
||||||
|
.into_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[launch]
|
#[launch]
|
||||||
|
|
|
@ -3,9 +3,9 @@ extern crate reqwest;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
use chrono::Local;
|
use chrono::Local;
|
||||||
|
use reqwest::header::{HeaderName, AUTHORIZATION, CONTENT_TYPE, USER_AGENT};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use reqwest::header::{HeaderName, AUTHORIZATION, CONTENT_TYPE, USER_AGENT};
|
|
||||||
|
|
||||||
mod models;
|
mod models;
|
||||||
|
|
||||||
|
@ -41,7 +41,10 @@ impl PixivAppClient {
|
||||||
|
|
||||||
let client_id = "MOBrBDS8blbauoSck0ZfDbtuzpyT";
|
let client_id = "MOBrBDS8blbauoSck0ZfDbtuzpyT";
|
||||||
let client_secret = "lsACyCD94FhDUtGTXi3QzcFE2uU1hqtDaKeqrdwj";
|
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 hash = PixivAppClient::md5(hash_input.as_str());
|
||||||
|
|
||||||
let req = self.http_client
|
let req = self.http_client
|
||||||
|
@ -54,37 +57,50 @@ impl PixivAppClient {
|
||||||
.build()
|
.build()
|
||||||
.expect("failed to build login request");
|
.expect("failed to build login request");
|
||||||
|
|
||||||
let r = match self.http_client
|
let r = match self.http_client.execute(req).await {
|
||||||
.execute(req)
|
|
||||||
.await {
|
|
||||||
Ok(r) => r.text().await.unwrap(),
|
Ok(r) => r.text().await.unwrap(),
|
||||||
Err(_e) => return
|
Err(_e) => return,
|
||||||
};
|
};
|
||||||
let d: Value = serde_json::from_str(&r).unwrap();
|
let d: Value = serde_json::from_str(&r).unwrap();
|
||||||
|
|
||||||
assert!(!d["response"]["access_token"].is_null());
|
assert!(!d["response"]["access_token"].is_null());
|
||||||
assert!(!d["response"]["refresh_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.access_token = Arc::new(Mutex::new(String::from(
|
||||||
self.refresh_token = Arc::new(Mutex::new(String::from(d["response"]["refresh_token"].as_str().unwrap())));
|
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 url = format!("{}/v1/illust/detail", self.host);
|
||||||
let illust_id_str = illust_id.to_string();
|
let illust_id_str = illust_id.to_string();
|
||||||
|
|
||||||
let req = self.http_client
|
let req = self
|
||||||
|
.http_client
|
||||||
.get(url)
|
.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").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(HeaderName::from_lowercase(b"app-version").unwrap(), "7.6.2")
|
||||||
.header(USER_AGENT, "PixivIOSApp/7.6.2 (iOS 12.2; iPhone9,1)")
|
.header(USER_AGENT, "PixivIOSApp/7.6.2 (iOS 12.2; iPhone9,1)")
|
||||||
.query(&[("illust_id", illust_id_str.as_str())])
|
.query(&[("illust_id", illust_id_str.as_str())])
|
||||||
.build()
|
.build()
|
||||||
.expect("failed to build request");
|
.expect("failed to build request");
|
||||||
|
|
||||||
let r = self.http_client
|
let r = self
|
||||||
|
.http_client
|
||||||
.execute(req)
|
.execute(req)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -99,26 +115,24 @@ impl PixivAppClient {
|
||||||
Err(e) => return Err(Box::from(e)),
|
Err(e) => return Err(Box::from(e)),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
match deserialized.illust {
|
match deserialized.illust {
|
||||||
Some(r) => Ok(r),
|
Some(r) => Ok(r),
|
||||||
None => Err(Box::from(deserialized.error.unwrap()))
|
None => Err(Box::from(deserialized.error.unwrap())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod client_tests {
|
mod client_tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::{assert, assert_eq, panic, env};
|
use std::{assert, assert_eq, env, panic};
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn login() {
|
async fn login() {
|
||||||
let token = env::var("PIXIV_REFRESH_TOKEN");
|
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;
|
client.refresh_token().await;
|
||||||
let cloned_access_token = Arc::clone(&client.access_token);
|
let cloned_access_token = Arc::clone(&client.access_token);
|
||||||
match cloned_access_token.lock() {
|
match cloned_access_token.lock() {
|
||||||
|
@ -131,10 +145,12 @@ mod client_tests {
|
||||||
async fn illust_details() {
|
async fn illust_details() {
|
||||||
let illust_id = 122388293;
|
let illust_id = 122388293;
|
||||||
let token = env::var("PIXIV_REFRESH_TOKEN");
|
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;
|
client.refresh_token().await;
|
||||||
let illust = client.illust_details(illust_id).await;
|
let illust = client.illust_details(illust_id).await;
|
||||||
assert!(illust.is_ok(), "Expected illustration data: {:#?}", illust);
|
assert!(illust.is_ok(), "Expected illustration data: {:#?}", illust);
|
||||||
assert_eq!(illust.unwrap().id, illust_id);
|
assert_eq!(illust.unwrap().id, illust_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ pub struct Illustration {
|
||||||
pub tools: Vec<String>,
|
pub tools: Vec<String>,
|
||||||
pub series: Option<String>,
|
pub series: Option<String>,
|
||||||
pub restrict: i32,
|
pub restrict: i32,
|
||||||
pub x_restrict: i32,
|
pub x_restrict: i32,
|
||||||
pub image_urls: IllustrationURLs,
|
pub image_urls: IllustrationURLs,
|
||||||
pub meta_pages: Vec<IllustrationURLsWrapper>,
|
pub meta_pages: Vec<IllustrationURLsWrapper>,
|
||||||
pub total_view: u32,
|
pub total_view: u32,
|
||||||
|
@ -35,7 +35,7 @@ pub struct User {
|
||||||
name: String,
|
name: String,
|
||||||
account: String,
|
account: String,
|
||||||
profile_image_urls: UserProfileImages,
|
profile_image_urls: UserProfileImages,
|
||||||
is_followed: bool
|
is_followed: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, std::fmt::Debug)]
|
#[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 {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
write!(f, "{}: {}", self.user_message, self.reason)
|
write!(f, "{}: {}", self.user_message, self.reason)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue