anime-search
Version:
An anime search engine that scrapes data from famous anime sites to find your favorite anime.
61 lines (60 loc) • 3.04 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAnimeFromZoro = void 0;
const cheerio_1 = require("cheerio");
const closest_match_1 = require("closest-match");
const node_fetch_1 = __importDefault(require("node-fetch"));
function getAnimeFromZoro(animeName) {
return __awaiter(this, void 0, void 0, function* () {
const name = typeof animeName === "function" ? animeName() : animeName;
const data = yield (0, node_fetch_1.default)(`https://zoro.to/search?keyword=${name}`).then((res) => res.text());
const $ = (0, cheerio_1.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 = (0, closest_match_1.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];
});
}
exports.getAnimeFromZoro = getAnimeFromZoro;
function animeSearch(animeName) {
return __awaiter(this, void 0, void 0, function* () {
return yield getAnimeFromZoro(typeof animeName === "function" ? animeName() : animeName);
});
}
exports.default = animeSearch;