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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use std::{fs::create_dir_all, io, path::PathBuf};
pub use bincode;
use bincode::deserialize;
pub use colored;
pub use directories;
pub use log;
pub use reqwest;
pub use serde;
pub use serde_json;
pub use tokio;
pub use zstd;
use auth::{AuthError, ImageboardConfig};
use directories::ProjectDirs;
use log::{debug, error, warn};
use serde::{Deserialize, Serialize};
use tokio::fs::{read, remove_file};
pub mod auth;
pub mod macros;
pub mod post;
#[derive(Debug, Copy, Clone, Ord, PartialOrd, PartialEq, Eq, Serialize, Deserialize)]
pub enum ImageBoards {
Danbooru,
E621,
Rule34,
Realbooru,
Konachan,
Gelbooru,
}
impl ToString for ImageBoards {
fn to_string(&self) -> String {
match self {
ImageBoards::Danbooru => String::from("danbooru"),
ImageBoards::E621 => String::from("e621"),
ImageBoards::Rule34 => String::from("rule34"),
ImageBoards::Realbooru => String::from("realbooru"),
ImageBoards::Konachan => String::from("konachan"),
ImageBoards::Gelbooru => String::from("gelbooru"),
}
}
}
impl ImageBoards {
#[inline]
pub fn user_agent(self) -> String {
let app_name = "Rust Imageboard Downloader";
let variant = match self {
ImageBoards::Danbooru => " (by danbooru user FerrahWolfeh)",
ImageBoards::E621 => " (by e621 user FerrahWolfeh)",
_ => "",
};
let ua = format!("{}/{}{}", app_name, env!("CARGO_PKG_VERSION"), variant);
debug!("Using user-agent: {}", ua);
ua
}
#[inline]
pub fn extractor_user_agent(self) -> String {
let app_name = "Rust Imageboard Post Extractor";
let variant = match self {
ImageBoards::Danbooru => " (by danbooru user FerrahWolfeh)",
ImageBoards::E621 => " (by e621 user FerrahWolfeh)",
_ => "",
};
let ua = format!("{}/{}{}", app_name, env!("CARGO_PKG_VERSION"), variant);
debug!("Using user-agent: {}", ua);
ua
}
#[inline]
pub fn post_url(&self) -> &'static str {
match self {
ImageBoards::Danbooru => "https://danbooru.donmai.us/posts.json",
ImageBoards::E621 => "https://e621.net/posts.json",
ImageBoards::Rule34 => {
"https://api.rule34.xxx/index.php?page=dapi&s=post&q=index&json=1"
}
ImageBoards::Konachan => "https://konachan.com/post.json",
ImageBoards::Realbooru => {
"http://realbooru.com/index.php?page=dapi&s=post&q=index&json=1"
}
ImageBoards::Gelbooru => {
"http://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1"
}
}
}
#[inline]
pub fn max_post_limit(self) -> usize {
match self {
ImageBoards::Danbooru => 200,
ImageBoards::E621 => 320,
ImageBoards::Rule34 | ImageBoards::Realbooru => 1000,
ImageBoards::Konachan | ImageBoards::Gelbooru => 100,
}
}
#[inline]
pub fn auth_url(self) -> &'static str {
match self {
ImageBoards::Danbooru => "https://danbooru.donmai.us/profile.json",
ImageBoards::E621 => "https://e621.net/users/",
_ => "",
}
}
#[inline]
pub fn auth_cache_dir() -> Result<PathBuf, io::Error> {
let cdir = ProjectDirs::from("com", "FerrahWolfeh", "imageboard-downloader").unwrap();
let cfold = cdir.config_dir();
if !cfold.exists() {
create_dir_all(cfold)?;
}
Ok(cfold.to_path_buf())
}
pub async fn read_config_from_fs(&self) -> Result<Option<ImageboardConfig>, AuthError> {
let cfg_path = Self::auth_cache_dir()?.join(PathBuf::from(self.to_string()));
if let Ok(config_auth) = read(&cfg_path).await {
debug!("Authentication cache found");
if let Ok(decompressed) = zstd::decode_all(config_auth.as_slice()) {
debug!("Authentication cache decompressed.");
return if let Ok(rd) = deserialize::<ImageboardConfig>(&decompressed) {
debug!("Authentication cache decoded.");
debug!("User id: {}", rd.user_data.id);
debug!("Username: {}", rd.user_data.name);
debug!("Blacklisted tags: '{:?}'", rd.user_data.blacklisted_tags);
Ok(Some(rd))
} else {
warn!(
"{}",
"Auth cache is invalid or empty. Running without authentication"
);
Ok(None)
};
}
debug!("Failed to decompress authentication cache.");
debug!("Removing corrupted file");
remove_file(cfg_path).await?;
error!("{}", "Auth cache is corrupted. Please authenticate again.");
};
debug!("Running without authentication");
Ok(None)
}
}