UNPKG

growtopia-api

Version:

An unofficial Growtopia API to gather item informations, name matches, sprites and server status.

86 lines (85 loc) 4 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = require("axios"); const cheerio_1 = require("cheerio"); function itemInfo(nameItem) { var _a; return __awaiter(this, void 0, void 0, function* () { try { const itemList = yield axios_1.default .get("https://growtopia.fandom.com/api/v1/SearchSuggestions/List?query=" + nameItem) .then((res) => { var _a; return (_a = res.data) === null || _a === void 0 ? void 0 : _a.items; }); if (itemList.length === 0) return undefined; const itemName = itemList[0].title; const link = `https://growtopia.wikia.com/wiki/${itemName}`; const getData = yield axios_1.default.get(link).then((res) => res.data); const $ = (0, cheerio_1.load)(getData); const Description = $(".card-text").first().text(); const Properties = $("#mw-content-text > div > div.gtw-card.item-card > div:nth-child(4)") .text() .trim() .split(/[\.+\!]/) .filter((d) => d !== ""); const Sprite = $("div.card-header .growsprite > img").attr("src"); const Color = (_a = $(".seedColor > div").text().trim()) === null || _a === void 0 ? void 0 : _a.split(" "); const Rarity = $(".card-header b > small").text().match(/(\d+)/); const Recipe = $(".recipebox table.content") .last() .text() .trim() .split(/[\r\n\x0B\x0C\u0085\u2028\u2029]+/) .map((el) => el.trim()); const Splice = $(".bg-splice").text(); const Info = $("#mw-content-text > div > p:nth-child(3)").text().trim(); const Type = $("table.card-field tr:nth-child(1) > td") .text() .split(" ") .pop(); const dataList = { Name: itemName, URL: link.replace(/ /g, "_"), Description, Properties: Properties.length > 0 ? Properties : undefined, Sprite, Color, Rarity: Rarity !== null ? parseInt(Rarity[0]) : undefined, Recipe: (Recipe === null || Recipe === void 0 ? void 0 : Recipe.length) > 0 ? { type: Recipe.shift() || "", recipe: Recipe, } : undefined, Splice: (Splice === null || Splice === void 0 ? void 0 : Splice.length) > 0 ? Splice : undefined, Info, Type, }; if (itemList.length > 1 && nameItem.toLowerCase() !== itemName.toLowerCase()) { const matches = itemList.map((i) => i.title); dataList.matches = matches; } return dataList; } catch (error) { if (axios_1.default.isAxiosError(error)) { console.log(error); throw error === null || error === void 0 ? void 0 : error.response; } else { throw error; } } }); } exports.default = itemInfo;