-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchItems.js
71 lines (64 loc) · 2.04 KB
/
fetchItems.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
"use strict";
const axios = require("axios");
const fs = require("fs");
const { pause, Log, readFile } = require("./utils");
const options = require("./config.json");
const {
OUTPUT_FOLDER,
ID_FILE_NAME,
OUTPUT_FILE_NAME,
CALLS_PER_SESSION,
TIMEOUT_BETWEEN_CALLS,
TIMEOUT_BETWEEN_ERROR,
} = options;
const handleFileData = function (data) {
readFile(`./${OUTPUT_FOLDER}/${OUTPUT_FILE_NAME}.json`, (currentItems) => {
const newItems = [];
let lastIDIndex = parseInt(currentItems.lastIDIndex);
const itemLimit = lastIDIndex + CALLS_PER_SESSION;
Log.pending(`fetching from ${lastIDIndex} to ${itemLimit}`);
Log.timestamp();
const ids = [];
for (let i = lastIDIndex; i < itemLimit; i += 1) {
if (i < data.length) {
const id = data[i];
ids.push(id);
} else {
Log.success("all items have been fetched");
return;
}
}
lastIDIndex += CALLS_PER_SESSION;
const url = `https://api.guildwars2.com/v2/items?ids=${ids.toString()}`;
axios
.get(url)
.then((response) => {
const { data: items } = response;
items.forEach((item) => {
const { name, id, icon, rarity } = item;
newItems.push({ name, id, icon, rarity });
});
currentItems.items = [...currentItems.items, ...newItems];
currentItems.lastIDIndex = lastIDIndex;
fs.writeFileSync(
`./${OUTPUT_FOLDER}/${OUTPUT_FILE_NAME}.json`,
JSON.stringify(currentItems, null, 2)
);
Log.success(
`item data fetch successful - ${
data.length - lastIDIndex
} items remain`
);
pause(() => handleFileData(data), TIMEOUT_BETWEEN_CALLS);
})
.catch((err) => {
Log.failure(`failed to fetch item data, retrying (${lastIDIndex})`);
console.error(err);
pause(() => handleFileData(data), TIMEOUT_BETWEEN_ERROR);
});
});
};
const fetchItems = () => {
readFile(`./${OUTPUT_FOLDER}/${ID_FILE_NAME}.json`, handleFileData);
};
module.exports = fetchItems;