root/src/booru/moe.rs

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

//#[path = "../booru.rs"] mod booru;
use crate::booru::http::http_client;
use crate::booru::{Booru, Boorus, Post, Posts, WikiEntry};
use anyhow::{Result, bail};
use serde::Deserialize;

#[allow(dead_code)]
#[derive(Deserialize, Debug, Clone)]
pub struct MoebooruPost {
    id: u64,
    tags: String,
    preview_url: String,
    file_url: String,
    sample_url: String,
    jpeg_url: String,
    width: u32,
    height: u32,
    md5: String,
    #[serde(skip_deserializing)]
    domain: String,
}

impl Post for MoebooruPost {
    fn id(&self) -> u64 {
        self.id
    }
    fn sort_id(&self) -> u64 {
        self.id
    }
    fn width(&self) -> u32 {
        self.width
    }
    fn height(&self) -> u32 {
        self.height
    }
    fn sample_url(&self) -> String {
        self.jpeg_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 {
        let dot_pos = self.full_url().rfind('.').unwrap_or(self.full_url().len());
        let ext = self.full_url()[dot_pos..].to_owned();
        ["", &self.md5, &ext].join("")
    }
    fn tags(&self) -> Vec<&str> {
        self.tags.split(' ').collect()
    }
    fn web_url(&self) -> String {
        format!("https://{}/post/show/{}", 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::Moebooru(self.clone())
    }
}

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

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

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

    pub async fn fetch_index(
        &self,
        search: &str,
        pid: u32,
    ) -> Result<std::vec::Vec<Box<dyn Post>>> {
        let url = [
            "https://",
            &self.domain[..],
            "/post.json?page=",
            &format!("{}", pid + 1), // first page starts at 1 not 0
            "&tags=",
            search,
        ]
        .join("");
        println!("fetching {}", url);
        let posts = http_client().get(&url).send().await?;
        let posts = posts.json::<Vec<Box<MoebooruPost>>>().await?;
        Ok(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.json?query={}", self.domain, search);
        let request = http_client().get(&url).send().await?;
        let entries = request.json::<Vec<WikiEntry>>().await?;
        if let Some(entry) = entries.into_iter().find(|e| e.title == search) {
            Ok(entry)
        } else {
            bail!("Couldn't find Wiki page");
        }
    }

    pub async fn fetch_artist(&self, search: &str) -> Result<WikiEntry> {
        let url = format!("https://{}/artist.json?name={}", self.domain, search);
        let request = http_client().get(&url).send().await?;
        let entries = request.json::<Vec<Artist>>().await?;
        if let Some(entry) = entries.into_iter().find(|e| e.name == search) {
            Ok(WikiEntry {
                title: entry.name,
                body: "".to_string(),
                links: entry.urls,
                other_names: Vec::new(),
            })
        } else {
            bail!("Couldn't find Artist page");
        }
    }
}

#[derive(Deserialize, Debug, Clone)]
struct Artist {
    name: String,
    #[serde(default)]
    urls: Vec<String>,
}