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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
mod note_list_query_builder;
mod post_list_query_builder;
mod tag_list_query_builder;

pub use self::{
    note_list_query_builder::NotesListQueryBuilder,
    post_list_query_builder::PostListQueryBuilder,
    tag_list_query_builder::TagListQueryBuilder,
};
#[cfg(feature = "scrape")]
use crate::HtmlPost;
use crate::{
    DeletedImageList,
    Error,
};
use reqwest::header::{
    HeaderMap,
    HeaderValue,
};
#[cfg(feature = "scrape")]
use scraper::Html;
use std::{
    num::NonZeroU64,
    time::Duration,
};
use url::Url;

// Default Header values
static USER_AGENT_VALUE: HeaderValue = HeaderValue::from_static("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4514.0 Safari/537.36");
static REFERER_VALUE: HeaderValue = HeaderValue::from_static("https://rule34.xxx/");
static ACCEPT_LANGUAGE_VALUE: HeaderValue = HeaderValue::from_static("en,en-US;q=0,5");
static ACCEPT_VALUE: HeaderValue = HeaderValue::from_static("*/*");

/// A Rule34 Client
#[derive(Debug, Clone)]
pub struct Client {
    /// The inner http client.
    ///
    /// This probably shouldn't be used by you.
    pub client: reqwest::Client,
}

impl Client {
    /// Make a new [`Client`]
    pub fn new() -> Self {
        let mut default_headers = HeaderMap::new();
        default_headers.insert(reqwest::header::USER_AGENT, USER_AGENT_VALUE.clone());
        default_headers.insert(
            reqwest::header::ACCEPT_LANGUAGE,
            ACCEPT_LANGUAGE_VALUE.clone(),
        );
        default_headers.insert(reqwest::header::ACCEPT, ACCEPT_VALUE.clone());
        default_headers.insert(reqwest::header::REFERER, REFERER_VALUE.clone());

        let client = reqwest::Client::builder()
            .default_headers(default_headers)
            .connect_timeout(Duration::from_secs(10))
            .build()
            .expect("failed to build rule34 client");

        Client { client }
    }

    /// Send a GET web request to a `url` and get the result as a [`String`].
    async fn get_text(&self, url: &str) -> Result<String, Error> {
        Ok(self
            .client
            .get(url)
            .timeout(Duration::from_secs(90))
            .send()
            .await?
            .error_for_status()?
            .text()
            .await?)
    }

    /// Send a GET web request to a `uri` and get the result as [`Html`],
    /// then use the given func to process it.
    #[cfg(feature = "scrape")]
    async fn get_html<F, T>(&self, uri: &str, f: F) -> Result<T, Error>
    where
        F: FnOnce(Html) -> T + Send + 'static,
        T: Send + 'static,
    {
        let text = self.get_text(uri).await?;
        let ret =
            tokio::task::spawn_blocking(move || f(Html::parse_document(text.as_str()))).await?;
        Ok(ret)
    }

    /// Send a GET web request to a `uri` and get the result as xml, deserializing it to the given type.
    async fn get_xml<T>(&self, uri: &str) -> Result<T, Error>
    where
        T: serde::de::DeserializeOwned + Send + 'static,
    {
        let text = self.get_text(uri).await?;
        let ret = tokio::task::spawn_blocking(move || quick_xml::de::from_str(&text)).await??;
        Ok(ret)
    }

    /// Create a builder to list posts from rule34.
    pub fn list_posts(&self) -> PostListQueryBuilder {
        PostListQueryBuilder::new(self)
    }

    /// Get a [`HtmlPost`] by `id`.
    #[cfg(feature = "scrape")]
    pub async fn get_html_post(&self, id: NonZeroU64) -> Result<HtmlPost, Error> {
        let url = crate::post_id_to_html_post_url(id);
        let ret = self
            .get_html(url.as_str(), |html| HtmlPost::from_html(&html))
            .await??;

        Ok(ret)
    }

    /// Get a list of deleted images.
    ///
    /// Only include ids over `last_id`. Use `None` for no limit.
    ///
    /// # Warning
    /// Due to current technical limitations,
    /// this function is not very memory efficient depending on `last_id`.
    /// This will require buffering ~30MB into memory.
    /// You should probably limit its use with a semaphore or similar.
    pub async fn list_deleted_images(
        &self,
        last_id: Option<NonZeroU64>,
    ) -> Result<DeletedImageList, Error> {
        let mut url = Url::parse_with_params(
            crate::API_BASE_URL,
            &[
                ("page", "dapi"),
                ("s", "post"),
                ("q", "index"),
                ("deleted", "show"),
            ],
        )?;
        if let Some(last_id) = last_id {
            let mut last_id_buf = itoa::Buffer::new();
            url.query_pairs_mut()
                .append_pair("last_id", last_id_buf.format(last_id.get()));
        }
        // Parse on a threadpool since the full returned string is currently around 30 megabytes in size,
        // and we need to run in under a few milliseconds.
        // We need to buffer this all in memory though, since `quick_xml` does not provide a streaming api.
        self.get_xml(url.as_str()).await
    }

    /// Get a builder to list tags.
    pub fn list_tags(&self) -> TagListQueryBuilder {
        TagListQueryBuilder::new(self)
    }

    /// Get a builder to list notes.
    ///
    /// This is undocumented.
    pub fn list_notes(&self) -> NotesListQueryBuilder {
        NotesListQueryBuilder::new(self)
    }
}

impl Default for Client {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[tokio::test]
    async fn search() {
        let client = Client::new();
        let res = client
            .list_posts()
            .tags(Some("rust"))
            .execute()
            .await
            .expect("failed to search rule34 for `rust`");
        dbg!(&res);
        assert!(!res.posts.is_empty());
    }

    async fn get_top_post(query: &str) {
        let client = Client::new();
        let response = client
            .list_posts()
            .tags(Some(query))
            .limit(Some(crate::POST_LIST_LIMIT_MAX))
            .execute()
            .await
            .unwrap_or_else(|error| panic!("failed to search rule34 for \"{query}\": {error}"));
        assert!(!response.posts.is_empty(), "no posts for \"{query}\"");

        dbg!(&response);

        #[cfg(feature = "scrape")]
        {
            let first = response.posts.first().expect("missing first entry");
            let post = client
                .get_html_post(first.id)
                .await
                .expect("failed to get first post");
            dbg!(post);
        }
    }

    #[tokio::test]
    async fn it_works() {
        let list = [
            "rust",
            "fbi",
            "gif",
            "corna",
            "sledge",
            "roadhog",
            "deep_space_waifu",
            "aokuro",
        ];

        for item in list {
            get_top_post(item).await;
        }
    }

    #[tokio::test]
    async fn deleted_images_list() {
        let client = Client::new();
        let result = client
            .list_deleted_images(Some(NonZeroU64::new(826_550).unwrap())) // Just choose a high-ish post id here and update to keep the download limited
            .await
            .expect("failed to get deleted images");
        dbg!(result);
    }

    #[tokio::test]
    async fn tags_list() {
        let client = Client::new();
        let result = client
            .list_tags()
            .limit(Some(crate::TAGS_LIST_LIMIT_MAX))
            .order(Some("name"))
            .execute()
            .await
            .expect("failed to list tags");
        assert!(!result.tags.is_empty());
        // dbg!(result);
    }

    #[tokio::test]
    async fn bad_tags_list() {
        let tags = [
            "swallow_(pokémon_move)",
            "akoúo̱_(rwby)",
            "miló_(rwby)",
            "las_tres_niñas_(company)",
            "ooparts♥love",
            "almáriel",
            "kingdom_hearts_union_χ_[cross]",
            "gen¹³",
            "nancy’s_face_is_deeper_in_carrie’s_ass",
            "…",
            "cleaning_&_clearing_(blue_archive)",
            "watashi_ga_suki_nara_\"suki\"_tte_itte!",
            "<3",
            ">_<",
            "dr—worm",
            "master_hen'tai",
        ];

        let client = Client::new();
        for expected_tag_name in tags {
            let tags = client
                .list_tags()
                .name(Some(expected_tag_name))
                .execute()
                .await
                .expect("failed to get tag")
                .tags;
            let tags_len = tags.len();

            assert!(
                tags_len == 1,
                "failed to get tags for \"{expected_tag_name}\", tags does not have one tag, it has {tags_len} tags"
            );
            let tag = tags.first().expect("tag list is empty");
            let actual_tag_name = &*tag.name;

            assert!(
                actual_tag_name == expected_tag_name,
                "\"{actual_tag_name}\" != \"{expected_tag_name}\""
            );
        }
    }

    #[tokio::test]
    async fn notes_list() {
        let client = Client::new();
        let result = client
            .list_notes()
            .execute()
            .await
            .expect("failed to list notes");
        assert!(!result.notes.is_empty());
        dbg!(result);
    }

    #[tokio::test]
    async fn source() {
        let client = Client::new();

        let response_1 = client
            .list_posts()
            .id(NonZeroU64::new(1))
            .execute()
            .await
            .expect("failed to get post 1");
        let post_1 = response_1.posts.first().expect("missing post");
        assert!(post_1.id.get() == 1);
        assert!(post_1.source.is_none());

        let response_3 = client
            .list_posts()
            .id(NonZeroU64::new(3))
            .execute()
            .await
            .expect("failed to get post 3");
        let post_3 = response_3.posts.first().expect("missing post");
        assert!(post_3.id.get() == 3);
        assert!(post_3.source.as_deref() == Some("https://www.pixiv.net/en/artworks/12972758"));
    }
}