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
use async_trait::async_trait;
use reqwest::{header, Client};
use serde::{Deserialize, Serialize};

use crate::error::Error;

use super::{Item, Output, Source};

/// The [`SauceNao`] source.
/// Requires an API key to function.
///
/// Works with `saucenao.com`
#[derive(Debug)]
pub struct SauceNao {
    /// The API key to use.
    api_key: String,
}

#[async_trait]
impl Source for SauceNao {
    type Argument = String;

    async fn check(&self, url: &str) -> Result<Output, Error> {
        let client = Client::new();

        // Check whether we're dealing with an image
        let head = client.head(url).send().await?;

        let content_type = head.headers().get(header::CONTENT_TYPE);

        if let Some(content_type) = content_type {
            let content_type = content_type.to_str()?;

            if !content_type.contains("image") {
                return Err(Error::LinkIsNotImage);
            }
        } else {
            return Err(Error::LinkIsNotImage);
        }

        // Build the request

        let req = {
            client
                .get("https://saucenao.com/search.php")
                .query(&Query::default().url(url).api_key(&self.api_key))
                .header(header::ACCEPT_ENCODING, "utf-8")
        };

        // Send the request

        let resp = req.send().await?;

        // Parse the response

        let text = resp.text().await?;
        let json: ApiResponse = serde_json::from_str(&text)?;

        let mut result = Output {
            original_url: url.to_string(),
            items: Vec::new(),
        };

        for item in json.results {
            if let Some(links) = item.data.ext_urls {
                let item = Item {
                    similarity: item.header.similarity.parse::<f32>()?,
                    link: links[0].clone(),
                };

                result.items.push(item);
            }
        }

        Ok(result)
    }

    async fn create(arg: Self::Argument) -> Result<Self, Error> {
        Ok(Self { api_key: arg })
    }
}

#[derive(Debug, Serialize)]
struct Query {
    url: String,
    api_key: String,
    db: u16,
    output_type: u8,
    #[serde(rename = "testmode")]
    test_mode: u8,
    #[serde(rename = "numres")]
    num_res: u8,
}

impl Query {
    pub fn url(mut self, url: &str) -> Self {
        self.url = url.to_string();
        self
    }

    pub fn api_key(mut self, api_key: &str) -> Self {
        self.api_key = api_key.to_string();
        self
    }
}

impl Default for Query {
    fn default() -> Self {
        Self {
            url: String::new(),
            api_key: String::new(),
            db: 999,
            output_type: 2,
            test_mode: 1,
            num_res: 16,
        }
    }
}

#[derive(Debug, Deserialize)]
struct ApiResponse {
    results: Vec<ApiItem>,
}

#[derive(Debug, Deserialize)]
struct ApiItem {
    header: ApiItemHeader,
    data: ApiItemData,
}

#[derive(Debug, Deserialize)]
struct ApiItemHeader {
    similarity: String,
}

#[derive(Debug, Deserialize)]
struct ApiItemData {
    ext_urls: Option<Vec<String>>,
}