UNPKG

anime-search

Version:

An anime search engine that scrapes data from famous anime sites to find your favorite anime.

42 lines (41 loc) 1.74 kB
#!/usr/bin/env node import { load } from "cheerio"; import { closestMatch } from "closest-match"; import fetch from "node-fetch"; async function getAnimeFromZoro(animeName) { const name = typeof animeName === "function" ? animeName() : animeName; const data = await fetch(`https://zoro.to/search?keyword=${name}`).then((res) => res.text()); const $ = load(data); const array = []; $("#main-content > section > div.tab-content > div > div.film_list-wrap") .find(".flw-item") .each((_index, elem) => { $(elem) .find("a") .each((_inde, element) => { let thumbnailUrl; for (const child of element.parent.children) { if (child.type === "tag" && child.name === "img") { thumbnailUrl = child.attribs["data-src"]; } } array.push({ name: element.attribs["title"], url: "https://zoro.to" + element.attribs["href"], thumbnail: thumbnailUrl, code: 200, platform: "https://zoro.to", }); }); }); const nameArray = array.map((element) => element.name); const closest = closestMatch(name, nameArray); const e = array.map((element) => (element.name === closest ? element : false)); const finalArray = e.filter((el) => el); return !finalArray[0] ? { code: 404, message: "Couldn't find the specified Anime" } : finalArray[0]; } async function animeSearch(animeName) { return await getAnimeFromZoro(typeof animeName === "function" ? animeName() : animeName); } export default animeSearch; export { getAnimeFromZoro };