-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathItemInfo.ts
107 lines (95 loc) · 2.63 KB
/
ItemInfo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import axios from "axios";
import { load } from "cheerio";
interface DataList {
Name: string;
URL: string;
Description?: string;
Properties?: string[];
Sprite?: string;
Color?: string[];
Rarity?: number;
Recipe?: {
type: string;
recipe: string[];
};
Splice?: string;
Info?: string;
Type?: string;
matches?: string[];
}
interface Item {
title: string;
}
async function itemInfo(nameItem: string): Promise<DataList | undefined> {
try {
const itemList = await axios
.get(
"https://growtopia.fandom.com/api/v1/SearchSuggestions/List?query=" +
nameItem
)
.then((res) => res.data?.items);
if (itemList.length === 0) return undefined;
const itemName = itemList[0].title;
const link = `https://growtopia.wikia.com/wiki/${itemName}`;
const getData = await axios.get(link).then((res) => res.data);
const $ = 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 = $(".seedColor > div").text().trim()?.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: 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?.length > 0
? {
type: Recipe.shift() || "",
recipe: Recipe,
}
: undefined,
Splice: Splice?.length > 0 ? Splice : undefined,
Info,
Type,
};
if (
itemList.length > 1 &&
nameItem.toLowerCase() !== itemName.toLowerCase()
) {
const matches = itemList.map((i: Item) => i.title);
dataList.matches = matches;
}
return dataList;
} catch (error) {
if (axios.isAxiosError(error)) {
console.log(error);
throw error?.response;
} else {
throw error;
}
}
}
export default itemInfo;