root/src/booru/dan.rs

// dan.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

// TODO notes:
// {domain}/notes?search[post_id]={id}

use crate::booru::http::http_client;
use crate::booru::{Booru, Boorus, Post, Posts, TagDB, WikiEntry};
use anyhow::{Result, bail};
use serde::Deserialize;
use serde::de::IntoDeserializer;

#[allow(dead_code)]
#[derive(Deserialize, Debug, Clone)]
pub struct DanbooruPost {
    id: u64,
    md5: String,
    file_ext: String,
    file_url: String,
    large_file_url: String,
    preview_file_url: String,
    tag_string: String,
    tag_string_character: String,
    tag_string_copyright: String,
    tag_string_artist: String,
    tag_string_general: String,
    tag_string_meta: String,
    image_width: u32,
    image_height: u32,
    #[serde(skip_deserializing)]
    domain: String,
}

impl Post for DanbooruPost {
    fn id(&self) -> u64 {
        self.id
    }
    fn sort_id(&self) -> u64 {
        self.id
    }
    fn width(&self) -> u32 {
        self.image_width
    }
    fn height(&self) -> u32 {
        self.image_height
    }
    fn sample_url(&self) -> String {
        self.large_file_url.to_owned()
    }
    fn thumb_url(&self) -> String {
        self.preview_file_url.to_owned()
    }
    fn full_url(&self) -> String {
        self.file_url.to_owned()
    }
    fn filename(&self) -> String {
        ["", &self.md5, &self.file_ext].join("")
    }
    fn tags(&self) -> Vec<&str> {
        self.tag_string.split(' ').collect()
    }
    fn tags_character(&self, _db: &TagDB) -> Vec<&str> {
        self.tag_string_character.split(' ').collect()
    }
    fn tags_copyright(&self, _db: &TagDB) -> Vec<&str> {
        self.tag_string_copyright.split(' ').collect()
    }
    fn tags_artist(&self, _db: &TagDB) -> Vec<&str> {
        self.tag_string_artist.split(' ').collect()
    }
    fn tags_general(&self, _db: &TagDB) -> Vec<&str> {
        self.tag_string_general.split(' ').collect()
    }
    fn tags_meta(&self, _db: &TagDB) -> Vec<&str> {
        self.tag_string_meta.split(' ').collect()
    }
    fn tags_unknown(&self, _db: &TagDB) -> Vec<&str> {
        vec![]
    }
    fn web_url(&self) -> String {
        format!("https://{}/posts/{}", 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::Danbooru(self.clone())
    }
}

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

impl Booru for Danbooru {
    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 == 15156212039548072414
    }
    fn get_domain(&self) -> &String {
        &self.domain
    }
    fn imp(&self) -> Boorus {
        Boorus::Danbooru(self.clone())
    }
    fn has_wiki(&self) -> bool {
        true
    }
}

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

    pub async fn fetch_index(
        &self,
        search: &str,
        pid: u32,
    ) -> Result<std::vec::Vec<Box<dyn Post>>> {
        let url = [
            "https://",
            &self.domain[..],
            "/posts.json?page=",
            &format!("{}", pid + 1),
            "&tags=",
            search,
        ]
        .join("");
        println!("fetching {}", url);
        let request = http_client().get(&url).send().await?;
        // match request.as_ref().ok() {
        //     None => println!("failed request {:#?}", request),
        //     _ => (),
        // }
        let posts = {
            let values = request.json::<Vec<serde_json::Value>>().await?;
            values
                .into_iter()
                .map(|v| Box::<DanbooruPost>::deserialize(v.into_deserializer()))
                .filter_map(|v| v.ok())
                .map(|mut p| {
                    p.domain = self.domain.clone();
                    p
                })
                .collect::<std::vec::Vec<Box<DanbooruPost>>>()
        };

        Ok(posts
            .into_iter()
            .map(|p| Box::<dyn Post>::from(p))
            .collect())
    }

    pub async fn fetch_wiki(&self, search: &str) -> Result<WikiEntry> {
        let url = format!("https://{}/wiki_pages/{}.json", self.domain, search);
        let request = http_client().get(&url).send().await?;
        let entry = request.json::<WikiEntry>().await?;
        Ok(entry)
    }

    pub async fn fetch_artist(&self, artist_name: &str) -> Result<WikiEntry> {
        let mut entry = self.fetch_wiki(artist_name).await.unwrap_or_default();

        let url = format!(
            "https://{}/artist_urls.json?commit=Search&search%5Bartist%5D%5Bname%5D={}",
            self.domain, artist_name
        );
        let request = http_client().get(&url).send().await?;
        let artist_urls = request.json::<Vec<ArtistUrl>>().await?;

        let url = format!(
            "https://{}/artists.json?commit=Search&search[any_name_matches]={}&search[order]=post_count",
            self.domain, artist_name
        );
        let request = http_client().get(&url).send().await?;
        let artists = request.json::<Vec<Artist>>().await?;

        if let Some(mut artist) = artists.into_iter().next() {
            entry.other_names.append(&mut artist.other_names);

            for a_url in artist_urls {
                if a_url.artist_id == artist.id {
                    entry.links.push(a_url.url);
                }
            }
            Ok(entry)
        } else {
            bail!("no artist found");
        }
    }
}

#[derive(Deserialize, Debug, Clone)]
struct ArtistUrl {
    pub artist_id: u64,
    pub url: String,
    // "created_at":"2024-11-08T12:44:41.272-05:00"
    // "updated_at":"2024-11-08T12:44:41.272-05:00"
    pub is_active: bool,
}
#[derive(Deserialize, Debug, Clone)]
struct Artist {
    id: u64,
    // "created_at":"2008-10-05T09:54:14.801-04:00",
    name: String,
    // "updated_at":"2024-11-08T12:44:41.274-05:00",
    is_deleted: bool,
    // "group_name":"",
    is_banned: bool,
    other_names: Vec<String>,
}