/* credits: - github.com/ztrdiamond - https://whatsapp.com/channel/0029VagFeoY9cDDa9ulpwM0T */ import axios from "axios"; import * as cheerio from "cheerio"; class Anhmoe { #baseURL = "https://anh.moe"; #headers = { "Origin": "https://anh.moe", "Referer": "https://anh.moe/", "User-Agent": "Zanixon/1.0.0", }; #api; #validCategories = [ "sfw", "nsfw", "video-gore", "video-nsfw", "moe", "ai-picture", "hentai", ]; constructor() { this.#api = axios.create({ baseURL: this.#baseURL, timeout: 120000, headers: this.#headers, }); } async getCategory(category, page = null) { if (!this.#validCategories.includes(category)) { throw new Error(`Invalid category: ${category}. Valid options are: ${this.#validCategories.join(", ")}`); } const url = page || `/category/${category}`; const raw = page ? await axios.get(url, { headers: this.#headers }) : await this.#api(url); const $ = cheerio.load(raw.data); const $listItems = $(".list-item"); const items = []; $listItems.each((_, el) => { const $el = $(el); let data = {}; const rawData = $el.attr("data-object"); if (rawData) { try { data = JSON.parse(decodeURIComponent(rawData)); } catch { throw "can't parse data object"; } } const title = $el.find(".list-item-desc-title a").attr("title") || data.title; const viewLink = new URL($el.find(".list-item-image a").attr("href"), this.#baseURL).href; const uploadBy = $el.find(".list-item-desc-title div").text(); items.push({ type: data.type, title, viewLink, [data.type]: { ...data.image, sizeFormatted: data.size_formatted, width: data.width, height: data.height, uploaded: data.how_long_ago, }, uploadBy, }); }); const next = $("li.pagination-next a").attr("href") || null; const prev = $("li.pagination-prev a").attr("href") || null; const nextPage = next ? new URL(next, this.#baseURL).href : null; const prevPage = prev ? new URL(prev, this.#baseURL).href : null; return { category, contents: items, nextPage, prevPage, }; } } const anh = new Anhmoe(); anh.getCategory("sfw") .then((d) => console.log(JSON.stringify(d, null, 2))) .catch(console.error);