growtopia-api
Version:
An unofficial Growtopia API to gather item informations, name matches, sprites and server status.
43 lines (42 loc) • 2.01 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = require("axios");
const cheerio_1 = require("cheerio");
function getImage(input) {
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=" +
input)
.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)).data;
const $ = (0, cheerio_1.load)(getData);
const Sprite = $("div.card-header .growsprite > img").attr("src");
if (!Sprite)
return undefined;
return Sprite.replace("webp", "png");
}
catch (error) {
if (axios_1.default.isAxiosError(error)) {
throw error === null || error === void 0 ? void 0 : error.response;
}
else {
throw error;
}
}
});
}
exports.default = getImage;