import { getSettingConfig } from "../settings"; import { download } from "./downloader"; import { existsSync } from "fs"; import select from "@inquirer/select"; import { join } from "path"; enum ImageFormat { Png = "p", Jpg = "j", Gif = "g", } enum TagType { Tag = "tag", Category = "category", Language = "language", Artist = "artist", Parody = "parody", Character = "character", } export type Image = { t: ImageFormat; w: number; h: number; }; type Tag = { id: number; type: TagType; name: string; url: string; count: number; }; export class ArchieDoujinAPI { id: number; media_id: string; title: { english: string; japanese: string; pretty: string; }; images: { pages: Image[]; cover: Image; thumbnail: Image; }; /** ๆŽƒๆ็š„ไบบ */ scanlator: string; upload_date: number; tags: Tag[]; num_pages: number; num_favorites: number; constructor(data: Record) { this.id = data["id"]; this.media_id = data["media_id"]; this.title = data["title"]; this.images = data["images"]; this.scanlator = data["scanlator"]; this.upload_date = data["upload_date"]; this.tags = data["tags"]; this.num_pages = data["num_pages"]; this.num_favorites = data["num_favorites"]; } async download(option?: { concurrency?: number; retryLimit?: number }) { const { download_path, download_router } = getSettingConfig(); const { concurrency, retryLimit } = option ?? {}; const downloadFolder = join(download_path, this.title.pretty); if (existsSync(downloadFolder)) { const answer = await select({ message: "You already have this doujin, do you want to re-download it?", choices: [ { name: "Yes (overwrite)", value: "yes" }, { name: "No (skip)", value: "no" }, ], }); if (answer === "no") return; } await download(downloadFolder, `${download_router}/${this.media_id}`, this.images.pages, { concurrency, retryLimit, }); } static fromSearchResult(data: Record): ArchieDoujinAPI { return new ArchieDoujinAPI(data); } } export const fetchDoujin = async (id: number) => { const url = `https://nhentai.net/api/gallery/${id}`; const response = await fetch(url); if (!response.ok) { throw new Error(`fetchDoujin error when try to fetch ${url}, status: ${response.status}`); } return new ArchieDoujinAPI(await response.json()); };