Remove utils::fix_gallery

This commit is contained in:
blank X 2020-12-02 23:00:36 +07:00
parent 829e8ad36c
commit cea920a080
4 changed files with 106 additions and 23 deletions

2
Cargo.lock generated
View File

@ -421,7 +421,7 @@ dependencies = [
[[package]]
name = "nhentairs"
version = "0.2.1"
version = "0.2.2"
dependencies = [
"reqwest",
"serde",

View File

@ -1,6 +1,6 @@
[package]
name = "nhentairs"
version = "0.2.1"
version = "0.2.2"
authors = ["blank X <theblankx@protonmail.com>"]
edition = "2018"

View File

@ -1,4 +1,7 @@
use serde::Deserialize;
use std::fmt;
use std::marker::PhantomData;
use serde::de::{self, Visitor};
use serde::{Deserialize, Deserializer};
#[derive(Deserialize, Debug)]
pub struct GalleryTitleInfo {
@ -32,6 +35,7 @@ pub struct GalleryTagInfo {
#[derive(Deserialize, Debug)]
pub struct GalleryInfoSuccess {
#[serde(deserialize_with = "convert_to_i32")]
pub id: i32,
pub media_id: String,
pub title: GalleryTitleInfo,
@ -61,3 +65,100 @@ pub struct SearchInfo {
pub num_pages: i32,
pub per_page: i32
}
fn convert_to_i32<'de, D>(deserializer: D) -> Result<i32, D::Error>
where
D: Deserializer<'de>
{
struct ConvertToI32<T>(PhantomData<fn() -> T>);
impl<'de> Visitor<'de> for ConvertToI32<i32>
{
type Value = i32;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("an integer between -2^31 and 2^31")
}
fn visit_i8<E>(self, value: i8) -> Result<Self::Value, E>
where
E: de::Error
{
Ok(i32::from(value))
}
fn visit_i16<E>(self, value: i16) -> Result<Self::Value, E>
where
E: de::Error
{
Ok(i32::from(value))
}
fn visit_i32<E>(self, value: i32) -> Result<Self::Value, E>
where
E: de::Error
{
Ok(value)
}
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where
E: de::Error
{
use std::i32;
if value >= i64::from(i32::MIN) && value <= i64::from(i32::MAX) {
Ok(value as i32)
} else {
Err(E::custom(format!("i32 out of range: {}", value)))
}
}
fn visit_u8<E>(self, value: u8) -> Result<Self::Value, E>
where
E: de::Error
{
Ok(i32::from(value))
}
fn visit_u16<E>(self, value: u16) -> Result<Self::Value, E>
where
E: de::Error
{
Ok(i32::from(value))
}
fn visit_u32<E>(self, value: u32) -> Result<Self::Value, E>
where
E: de::Error
{
use std::{i32, u32};
if value <= i32::MAX as u32 {
Ok(value as i32)
} else {
Err(E::custom(format!("i32 out of range: {}", value)))
}
}
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error
{
use std::{i32, u64};
if value <= i32::MAX as u64 {
Ok(value as i32)
} else {
Err(E::custom(format!("i32 out of range: {}", value)))
}
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error
{
// https://brokenco.de/2020/08/03/serde-deserialize-with-string.html
value.parse::<i32>().map_err(serde::de::Error::custom)
}
}
deserializer.deserialize_any(ConvertToI32(PhantomData))
}

View File

@ -8,25 +8,13 @@ use std::collections::BTreeMap;
extern crate serde_json;
extern crate reqwest;
fn fix_gallery(body: &mut serde_json::Value) -> Result<(), serde_json::Error> {
if body["id"].is_string() {
body["id"] = serde_json::json!(
body["id"].as_str().unwrap().parse::<i32>().unwrap()
);
}
Ok(())
}
pub async fn get_sauce_info(client: reqwest::Client, sauce: i32) -> Result<structs::GalleryInfo, reqwest::Error> {
let mut uri = String::from("https://nhentai.net/api/gallery/");
uri.push_str(&sauce.to_string());
let resp = client.get(&uri)
.send()
.await?;
let body = resp.text().await?;
let mut body: serde_json::Value = serde_json::from_str(&body).unwrap();
fix_gallery(&mut body).unwrap();
Ok(serde_json::from_str(&serde_json::to_string(&body).unwrap()).unwrap())
Ok(serde_json::from_str(&resp.text().await?).unwrap())
}
pub async fn get_search_info(client: reqwest::Client, search_query: &str) -> Result<structs::SearchInfo, reqwest::Error> {
@ -35,13 +23,7 @@ pub async fn get_search_info(client: reqwest::Client, search_query: &str) -> Res
.query(&[("query", search_query)])
.send()
.await?;
assert!(resp.status().is_success());
let body = resp.text().await?;
let mut body: serde_json::Value = serde_json::from_str(&body).unwrap();
for i in 0..body["result"].as_array().unwrap().len() {
fix_gallery(&mut body["result"][i]).unwrap();
}
Ok(serde_json::from_str(&serde_json::to_string(&body).unwrap()).unwrap())
Ok(serde_json::from_str(&resp.text().await?).unwrap())
}
pub async fn download_file(client: reqwest::Client, url: &str, file_name: &str) -> Result<(), reqwest::Error> {