root/src/booru/e621.rs

// e621.rs
//
// Copyright 2024 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

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

#[allow(dead_code, clippy::vec_box)]
#[derive(Deserialize, Debug, Clone)]
pub struct Wrapper {
    posts: Vec<Box<E621Post>>,
}

#[allow(dead_code)]
#[derive(Deserialize, Debug, Clone)]
pub struct E621Post {
    id: u64,
    // "created_at":"2024-05-28T10:13:35.342-04:00",
    // "updated_at":"2024-05-28T10:13:35.342-04:00",
    file: File,
    preview: Preview,
    sample: Sample,
    // score: E621Score,
    tags: Tags,
    // "locked_tags":[],
    // change_seq:u64,
    // "flags":{"pending":true,"flagged":false,"note_locked":false,"status_locked":false,"rating_locked":false,"deleted":false},
    // "rating":"e",
    // "fav_count":0,
    sources: Vec<Option<String>>,
    // "pools":[],
    // relationships: Relationships,
    // "approver_id":null,
    // uploader_id: u64,
    description: Option<String>,
    // "is_favorited":false,
    // "has_notes":false,
    // "duration":null
    comment_count: u32,
    #[serde(skip_deserializing)]
    domain: String,
}

#[allow(dead_code)]
#[derive(Deserialize, Debug, Clone)]
pub struct File {
    pub width: u32,
    pub height: u32,
    pub ext: String, // png
    pub size: i32,   // in bytes
    pub md5: String,
    pub url: Option<String>,
}

#[allow(dead_code)]
#[derive(Deserialize, Debug, Clone)]
pub struct Preview {
    pub width: u32,
    pub height: u32,
    pub url: Option<String>,
}

#[allow(dead_code)]
#[derive(Deserialize, Debug, Clone)]
pub struct Sample {
    pub has: bool,
    pub width: u32,
    pub height: u32,
    pub url: Option<String>,
    // alternatives: HashMap<String, Url>?
}

// pub struct E621Score {
//     up: i32,
//     down: i32,
//     total: i32,
// }

#[allow(dead_code)]
#[derive(Deserialize, Debug, Clone)]
pub struct Tags {
    pub general: Vec<String>,
    pub artist: Vec<String>,
    pub copyright: Vec<String>,
    pub character: Vec<String>,
    pub species: Vec<String>,
    pub invalid: Vec<String>,
    pub meta: Vec<String>,
    pub lore: Vec<String>,
}

// pub struct Relationships {
//     parent_id: u64,
//     has_children: bool,
//     has_active_children: bool,
//     children: Vec<u64>
// }

impl Post for E621Post {
    fn id(&self) -> u64 {
        self.id
    }
    fn sort_id(&self) -> u64 {
        self.id
    }
    fn width(&self) -> u32 {
        self.file.width
    }
    fn height(&self) -> u32 {
        self.file.height
    }
    fn sample_url(&self) -> String {
        if let Some(url) = self.sample.url.as_ref() {
            url.to_owned()
        } else {
            "".to_owned()
        }
    }
    fn thumb_url(&self) -> String {
        if let Some(url) = self.preview.url.as_ref() {
            url.to_owned()
        } else {
            "".to_owned()
        }
    }
    fn full_url(&self) -> String {
        if let Some(url) = self.file.url.as_ref() {
            url.to_owned()
        } else {
            "".to_owned()
        }
    }
    fn filename(&self) -> String {
        ["", &self.file.md5, &self.file.ext].join("")
    }
    fn tags(&self) -> Vec<&str> {
        let mut result = Vec::new();
        result.append(&mut self.tags.general.iter().map(|x| x.as_ref()).collect());
        result.append(&mut self.tags.artist.iter().map(|x| x.as_ref()).collect());
        result.append(&mut self.tags.copyright.iter().map(|x| x.as_ref()).collect());
        result.append(&mut self.tags.character.iter().map(|x| x.as_ref()).collect());
        result.append(&mut self.tags.species.iter().map(|x| x.as_ref()).collect());
        result.append(&mut self.tags.invalid.iter().map(|x| x.as_ref()).collect());
        result.append(&mut self.tags.meta.iter().map(|x| x.as_ref()).collect());
        result.append(&mut self.tags.lore.iter().map(|x| x.as_ref()).collect());
        result
    }
    fn tags_character(&self, _db: &TagDB) -> Vec<&str> {
        self.tags.character.iter().map(|x| x.as_ref()).collect()
    }
    fn tags_copyright(&self, _db: &TagDB) -> Vec<&str> {
        self.tags.copyright.iter().map(|x| x.as_ref()).collect()
    }
    fn tags_artist(&self, _db: &TagDB) -> Vec<&str> {
        self.tags.artist.iter().map(|x| x.as_ref()).collect()
    }
    fn tags_general(&self, _db: &TagDB) -> Vec<&str> {
        self.tags.general.iter().map(|x| x.as_ref()).collect()
    }
    fn tags_meta(&self, _db: &TagDB) -> Vec<&str> {
        self.tags.meta.iter().map(|x| x.as_ref()).collect()
    }
    fn tags_unknown(&self, _db: &TagDB) -> Vec<&str> {
        let mut result = Vec::new();
        result.append(&mut self.tags.species.iter().map(|x| x.as_ref()).collect());
        result.append(&mut self.tags.invalid.iter().map(|x| x.as_ref()).collect());
        result.append(&mut self.tags.lore.iter().map(|x| x.as_ref()).collect());
        result
    }
    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::E621(self.clone())
    }
}

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

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

impl E621 {
    pub fn new(domain: String) -> E621 {
        E621 { 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("");
        let json = http_client()
            .get(&url)
            .send()
            .await?
            .json::<Wrapper>()
            .await?;

        Ok(json
            .posts
            .into_iter()
            .map(|mut p| {
                p.domain = self.domain.clone();
                p
            })
            .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://{}/artists.json?search[any_name_or_url_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);
            Ok(WikiEntry {
                title: artist.name,
                body: artist.notes,
                other_names: artist.other_names,
                links: artist.urls.into_iter().map(|u| u.url).collect(),
            })
        } 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_active: bool,
    // "group_name":"",
    notes: String,
    other_names: Vec<String>,
    urls: Vec<ArtistUrl>,
}