1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use crate::utility::{api::url, error::Result};
use chrono::{serde::ts_seconds, DateTime, Utc};
use hyper::{
body::{self, Buf},
Client,
};
use hyper_tls::HttpsConnector;
use serde::Deserialize;
use serde_json::Value;
use std::{fs, path::PathBuf, str::FromStr};
#[derive(Deserialize, Debug)]
pub struct Title {
pub pretty: Option<String>,
pub english: Option<String>,
pub japanese: Option<String>,
}
#[derive(Deserialize)]
pub struct Image {
pub t: String,
pub w: u32,
pub h: u32,
}
#[derive(Deserialize)]
pub struct Images {
pub pages: Vec<Image>,
pub cover: Image,
pub thumbnail: Image,
}
#[derive(Deserialize, Debug)]
pub struct Tag {
pub id: u32,
pub count: u32,
pub url: String,
pub name: String,
#[serde(rename = "type")]
pub category: String,
}
#[derive(Deserialize)]
pub struct Doujin {
pub id: Value,
pub title: Title,
pub images: Images,
pub tags: Vec<Tag>,
pub num_pages: u32,
pub media_id: String,
pub scanlator: String,
pub num_favorites: u32,
#[serde(with = "ts_seconds")]
pub upload_date: DateTime<Utc>,
}
impl Doujin {
pub async fn new(id: u32) -> Result<Self> {
let https = HttpsConnector::new();
let client = Client::builder().build::<_, hyper::Body>(https);
let response = client
.get(hyper::Uri::from_str(&url::doujin(id))?)
.await?;
let content = body::aggregate(response)
.await?;
let result: Self = serde_json::from_reader(content.reader())?;
Ok(result)
}
pub async fn random() -> Result<u32> {
let https = HttpsConnector::new();
let client = Client::builder().build::<_, hyper::Body>(https);
let response = client
.get(hyper::Uri::from_str("https://nhentai.xxx/random")?)
.await?;
let segments = response
.headers()
.get(hyper::header::LOCATION)
.ok_or("Failed to retrieve url")?
.to_str()?
.split('/')
.collect::<Vec<_>>();
let code = segments
.iter()
.next_back()
.ok_or("Failed to parse url")?
.parse::<u32>()?;
Ok(code)
}
pub fn from_json(path: PathBuf) -> Result<Self> {
let data = fs::read_to_string(path)?;
let result: Self = serde_json::from_str(&data)?;
Ok(result)
}
}