-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
431 lines (374 loc) · 11.9 KB
/
index.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Import the required modules
const express = require("express");
const axios = require("axios");
const cheerio = require("cheerio");
const moment = require("moment");
var admin = require("firebase-admin");
var serviceAccount = require("./firebase/tibiatoptrumps-firebase-adminsdk-rn9hj-055d43aae0.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://tibiatoptrumps-default-rtdb.firebaseio.com"
});
const db = admin.firestore();
// Create an instance of the Express application
const app = express();
const port = process.env.PORT || 3000; // Choose any port number you prefer
const cleanText = (text) => {
return text
.replace(/\n/g, "")
.replace(/days? ago/g, "")
.trim();
};
const getKilledYesterday = async () => {
try {
const url = "https://www.tibia-statistic.com/bosshunter/details/venebra";
const response = await axios.get(url);
const html = response.data;
const $ = cheerio.load(html);
const figcaptions = $("figcaption");
return figcaptions
.map((index, element) => {
return { name: cleanText($(element).text()) };
})
.get();
} catch (error) {
console.error("Error:", error.message);
return [];
}
};
const getBossListKillStatistic = async () => {
try {
const url = "https://www.tibia-statistic.com/bosshunter/details/venebra";
const response = await axios.get(url);
const html = response.data;
const $ = cheerio.load(html);
const bossList = [];
let isSecondTable = false;
// Replace 'table-hover' with the actual class name of the table
$(
"table.table-hover.table-dark.table-sm.table-bordered.table-striped.rounded tbody tr"
).each((index, element) => {
const tds = $(element).find("td");
const name = cleanText($(tds[1]).text());
const lastSeen = cleanText($(tds[2]).text());
// Check if the current row (element) has four columns, which means it's the second table
if (tds.length === 4) {
isSecondTable = true;
}
// Process the data differently based on whether it's the first table or second table
if (isSecondTable) {
bossList.push({ name, lastSeen, chance: "No chance" });
} else {
const chance = cleanText($(tds[3]).text());
bossList.push({ name, chance, lastSeen });
}
});
console.log(bossList);
return bossList;
} catch (error) {
console.error("Error:", error.message);
return [];
}
};
async function getGuildStatsBossList() {
try {
const url =
"https://guildstats.eu/bosses?world=Venebra&monsterName=&bossType=3&rook=0";
const response = await axios.get(url);
const html = response.data;
const $ = cheerio.load(html);
const bossList = [];
$("#myTable tbody tr").each((index, element) => {
const name = $(element).find("td:nth-child(2)").text().trim();
const killedYesterday = parseInt(
$(element).find("td:nth-child(4)").text().trim()
);
const lastSeen = $(element).find("td:nth-child(8)").text().trim();
const chanceText = $(element).find("td:nth-child(11)").text().trim();
const expectedIn = $(element).find("td:nth-child(12)").text().trim();
let chance = chanceText;
// Handle 'No' and '1Low' cases
if (chanceText === "0No") {
chance = "0";
} else if (chanceText === "1Low") {
chance = "0.01";
} else {
if (chanceText !== "-1") {
chance = chanceText.replace("%", "0");
chance = chance / 100;
}
}
// Exclude bosses with chance -1 from the list
if (chanceText !== "-1") {
bossList.push({
name,
killedYesterday,
lastSeen,
chance,
expectedIn: expectedIn || "0",
});
}
});
return bossList;
} catch (error) {
console.error("Error fetching data:", error);
return [];
}
}
async function getDuplicatedBosses(url) {
try {
const url = "https://www.tibiabosses.com/stats/?world=Venebra&mode=3";
const response = await axios.get(url);
const $ = cheerio.load(response.data);
const bosses = [
{
name: "White Pale",
cities: ["Edron", "Darashia", "Liberty Bay"],
count: 3,
},
{
name: "Rotworm Queen",
cities: ["Darashia", "Ab'dendriel", "Edron", "Liberty Bay"],
count: 4,
},
];
const result = [];
bosses.forEach((boss) => {
const bossDiv = $(`img[alt="${boss.name}"]`).parent();
const cityDivs = bossDiv.nextAll().slice(0, boss.count);
cityDivs.each((index, el) => {
const city = boss.cities[index];
const lastSeen = $(el).text().trim();
const chanceDiv = $(el).next();
// Get the chance value based on the color
let chanceText = chanceDiv.css("color");
let chances = 0;
if (chanceText === "green") {
chances = 1;
} else if (chanceText === "blue") {
chances = 0.01;
} else {
chances = 0;
}
result.push({
name: boss.name,
city: city,
lastSeen: lastSeen,
chance: chances,
});
});
});
return result;
} catch (error) {
console.error("Error fetching data:", error.message);
return [];
}
}
async function saveCreaturesToFirestore(creatures) {
const batch = db.batch();
creatures.forEach((creature) => {
const finalName = creature.name.replace(" ", "_");
const docRef = db.collection('creatures').doc(finalName);
batch.set(docRef, creature);
});
await batch.commit();
}
function getDifficultyNumber(difficultyString) {
switch (difficultyString) {
case "Inofensiva":
return 0;
case "Trivial":
return 1;
case "Fácil":
return 2;
case "Mediana":
return 3;
case "Difícil":
return 4;
case "Desafiador":
return 5;
default:
return -1;
}
}
async function fetchTibiaWikiCreaturesData(url) {
try {
// Fetch the HTML of the page
const { data } = await axios.get(url);
const $ = cheerio.load(data);
const URL_PREFIX = "https://www.tibiawiki.com.br";
// The array to hold our creatures data
const creatures = [];
// Find each table by ID and loop through its rows
$('table#tabelaDPL').first().each((_, table) => {
// Loop through each row of the table
$("tr", table).each((_, tr) => {
const tds = $("td", tr).filter((index, td) => index < $("td", tr).length - 1);
if (tds.length > 0) {
// Ensure there are columns to process
const creature = {
name: $(tds[0]).text().trim(),
image: `${URL_PREFIX}${$(tds[1]).find("img").attr("src") || ""}`,
hp: parseInt($(tds[2]).text().trim(), 10),
xp: parseInt($(tds[3]).text().trim(), 10),
charms: parseInt($(tds[4]).text().trim(), 10),
difficultyString: $(tds[5]).find("img").attr("title"),
difficulty: getDifficultyNumber($(tds[5]).find("img").attr("title")),
};
creatures.push(creature);
}
});
});
// Log or return the JSON data
// console.log(JSON.stringify(creatures, null, 2));
return creatures;
} catch (error) {
console.error("Error fetching data:", error);
}
}
async function generateTopTrumps(save) {
const urls = [
{ url: "https://www.tibiawiki.com.br/wiki/Anf%C3%ADbios" },
{ url: "https://www.tibiawiki.com.br/wiki/Aqu%C3%A1ticos" },
{ url: "https://www.tibiawiki.com.br/wiki/Aves" },
{ url: "https://www.tibiawiki.com.br/wiki/Constructos" },
{ url: "https://www.tibiawiki.com.br/wiki/Criaturas_M%C3%A1gicas" },
{ url: "https://www.tibiawiki.com.br/wiki/Dem%C3%B4nios" },
{ url: "https://www.tibiawiki.com.br/wiki/Drag%C3%B5es" },
{ url: "https://www.tibiawiki.com.br/wiki/Elementais" },
{ url: "https://www.tibiawiki.com.br/wiki/Extra_Dimensionais" },
{ url: "https://www.tibiawiki.com.br/wiki/Fadas" },
{ url: "https://www.tibiawiki.com.br/wiki/Gigantes" },
{ url: "https://www.tibiawiki.com.br/wiki/Humanos" },
{ url: "https://www.tibiawiki.com.br/wiki/Human%C3%B3ides" },
{ url: "https://www.tibiawiki.com.br/wiki/Licantropos" },
{ url: "https://www.tibiawiki.com.br/wiki/Mam%C3%ADferos" },
{ url: "https://www.tibiawiki.com.br/wiki/Mortos-Vivos" },
{ url: "https://www.tibiawiki.com.br/wiki/Plantas_(Criatura)" },
{ url: "https://www.tibiawiki.com.br/wiki/R%C3%A9pteis" },
{ url: "https://www.tibiawiki.com.br/wiki/Slimes" },
{ url: "https://www.tibiawiki.com.br/wiki/Vermes" },
]
try {
const promises = urls.map(item => fetchTibiaWikiCreaturesData(item.url));
const results = await Promise.all(promises);
return results.flat();
} catch (error) {
console.error("Error fetching creature data:", error);
}
}
async function getCreatures() {
try {
const creaturesRef = db.collection('creatures');
const snapshot = await creaturesRef.get();
const creatures = [];
snapshot.forEach(doc => {
creatures.push(doc.data());
});
console.log(creatures);
return creatures;
} catch (error) {
console.error("Error fetching creatures:", error);
throw error; // Propagate the error
}
}
async function testUrls() {
try {
const { data } = await axios.get("https://web.archive.org/web/20240201070048/https://www.tibiawiki.com.br/");
} catch (error) {
return [{error: error}]
}
return [{status: 200}]
}
app.get("/tibia-statistics", async (req, res) => {
try {
const bossList = await getBossListKillStatistic();
res.json(bossList);
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "Internal server error" });
}
});
app.get("/killed-yesterday", async (req, res) => {
try {
const killedYesterday = await getKilledYesterday();
res.json(killedYesterday);
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "Internal server error" });
}
});
app.get("/guild-stats", async (req, res) => {
try {
const bossList = await getGuildStatsBossList();
res.json(bossList);
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "Internal server error" });
}
});
app.get("/top-trumps-draw", async (req, res) => {
try {
const data = await drawCreatures();
res.json(data);
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "Internal server error" });
}
});
app.get("/tibiawiki-creatures", async (req, res) => {
try {
const data = await getCreatures();
res.json(data);
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "Internal server error" });
}
});
app.get("/top-trumps-crawler", async (req, res) => {
try {
const data = await generateTopTrumps();
res.json(data);
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "Internal server error" });
}
});
app.get("/top-trumps-save", async (req, res) => {
try {
const data = await generateTopTrumps();
await saveCreaturesToFirestore(data).catch(console.error);
res.json({
success: true
});
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "Internal server error" });
}
});
app.get("/test-urls", async (req, res) => {
try {
const data = await testUrls();
res.json(data);
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "Internal server error" });
}
});
app.get("/duplicated-bosses", async (req, res) => {
try {
const bossList = await getDuplicatedBosses();
res.json(bossList);
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: "Internal server error" });
}
});
// Start the server
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
// Export the serverless function
exports.handler = async (event, context) => {
return app;
};