root/src/booru/gel.rs

// gel.rs
//
// Copyright 2020 nee <nee-git@hidamari.blue>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#![allow(clippy::vec_box)]
use crate::booru::Booru;
use crate::booru::Boorus;
use crate::booru::Post;
use crate::booru::Posts;
use crate::booru::http::http_client;
use anyhow::Result;
use serde::Deserialize;

#[allow(dead_code)]
#[derive(Deserialize, Debug, Clone)]
pub struct GelbooruPost {
    source: String,
    directory: String,
    height: u32,
    id: u64,
    image: String,
    change: u64,
    owner: String,
    // parent_id: null,
    rating: String,
    sample: u32,
    sample_height: u32,
    sample_width: u32,
    score: u32,
    tags: String,
    width: i32,
    file_url: String,
    sample_url: String,
    preview_url: String,
    created_at: String,
    #[serde(skip_deserializing)]
    domain: String,
}
#[derive(Deserialize, Debug, Clone)]
struct Wrapper {
    // @attribute { limit: u32, offset: u32, count u64 }
    post: Vec<Box<GelbooruPost>>,
}

// type
// GENERAL = 0
// ARTIST = 1
// UNKNOWN = 2
// COPYRIGHT = 3
// CHARACTER = 4
// META = 5
// DEPRECATED = 6

// id   "602"
// tag  "midriff"
// count        "167242"
// type "tag"
// ambiguous    "0"
#[allow(dead_code)]
#[derive(Deserialize, Debug, Clone)]
struct Tag {
    id: String,
    tag: String,
    count: String,
    // type: String,
    ambiguous: String,
}

impl Post for GelbooruPost {
    fn id(&self) -> u64 {
        self.id
    }
    fn sort_id(&self) -> u64 {
        self.id
    }
    fn width(&self) -> u32 {
        self.width as u32
    }
    fn height(&self) -> u32 {
        self.height
    }
    fn sample_url(&self) -> String {
        self.sample_url.to_owned()
    }
    fn thumb_url(&self) -> String {
        self.preview_url.to_owned()
    }
    fn full_url(&self) -> String {
        self.file_url.to_owned()
    }
    fn filename(&self) -> String {
        self.image.to_owned()
    }
    fn tags(&self) -> Vec<&str> {
        self.tags.split(' ').collect()
    }
    fn web_url(&self) -> String {
        format!(
            "https://{}/index.php?page=post&s=view&id={}",
            self.domain(),
            self.id()
        )
    }
    fn domain(&self) -> &String {
        &self.domain
    }
    fn clone_post(&self) -> Box<dyn Post> {
        std::boxed::Box::new(self.clone())
    }
    fn imp(&self) -> Posts {
        Posts::Gelbooru(self.clone())
    }
}

#[derive(Deserialize, Debug, Clone)]
pub struct Gelbooru {
    domain: String,
}

impl Booru for Gelbooru {
    fn next_page(&self, current_page: u32, _last_result_count: u32) -> u32 {
        current_page + 1
    }
    fn clone_booru(&self) -> Box<dyn Booru> {
        Box::new(self.clone())
    }
    fn check_api_by_domain(&self, hash: u64, _domain: &str) -> bool {
        hash == 2593401490878829595
    }
    fn get_domain(&self) -> &String {
        &self.domain
    }
    fn imp(&self) -> Boorus {
        Boorus::Gelbooru(self.clone())
    }
}

impl Gelbooru {
    pub fn new(domain: String) -> Gelbooru {
        Gelbooru { domain }
    }

    pub async fn fetch_index(
        &self,
        search: &str,
        pid: u32,
    ) -> Result<std::vec::Vec<Box<dyn Post>>> {
        let url = [
            "https://",
            &self.domain[..],
            "/index.php?page=dapi&s=post&q=index&json=1&pid=",
            &format!("{}", pid),
            "&tags=",
            search,
        ]
        .join("");
        let json = http_client()
            .get(&url)
            .send()
            .await?
            .json::<Wrapper>()
            .await?;
        // if let Some(Err(e)) = json.as_ref() {
        //     println!("json {:#?}", e)
        // }

        Ok(json
            .post
            .into_iter()
            .map(|mut p| {
                p.domain = self.domain.clone();
                p
            })
            .map(|p| Box::<dyn Post>::from(p))
            .collect())
    }
}