-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
monsters.js
133 lines (121 loc) · 4.83 KB
/
monsters.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
const fetch = require("node-fetch");
const CONFIG = require("./config.js");
const authentication = require("./auth.js");
function getMonsterCount(cobaltId, searchTerm="", homebrew, homebrewOnly, sources) {
return new Promise((resolve, reject) => {
const headers = (authentication.CACHE_AUTH.exists(cobaltId).data !== null) ? {headers: {"Authorization": `Bearer ${authentication.CACHE_AUTH.exists(cobaltId).data}`}} : {};
const url = CONFIG.urls.monstersAPI(0,1, searchTerm, homebrew, homebrewOnly, sources);
fetch(url, headers)
.then(res => res.json())
.then(json => {
resolve(json.pagination.total);
})
.catch(error => {
console.log("Error retrieving monsters");
console.log(error);
reject(error);
});
});
}
function imageFiddleMonsters(monsters) {
const imageFiddledMonsters = monsters.map((monster) => {
const imageResizeRegEx = /\/thumbnails\/(\d*)\/(\d*)\/(\d*)\/(\d*)\/(\d*)\.(jpg|png|jpeg|webp|gif)/;
if (monster.largeAvatarUrl) {
const original = monster.largeAvatarUrl.replace(".com.com/", ".com/");
monster.largeAvatarUrl = original.replace(imageResizeRegEx, "/thumbnails/$1/$2/1000/1000/$5.$6");
}
if (monster.basicAvatarUrl) {
const original = monster.basicAvatarUrl.replace(".com.com/", ".com/");
monster.basicAvatarUrl = original.replace(imageResizeRegEx, "/thumbnails/$1/$2/1000/1000/$5.$6");
}
return monster;
});
return imageFiddledMonsters;
}
const extractMonsters = (cobaltId, searchTerm="", homebrew, homebrewOnly, sources) => {
return new Promise((resolve, reject) => {
console.log(`Retrieving monsters for ${cobaltId}`);
let monsters = [];
const headers = (authentication.CACHE_AUTH.exists(cobaltId).data !== null) ? {headers: {"Authorization": `Bearer ${authentication.CACHE_AUTH.exists(cobaltId).data}`}} : {};
let count = 0;
// fetch 100 monsters at a time - api limit
let take = 100;
getMonsterCount(cobaltId, searchTerm, homebrew, homebrewOnly, sources).then(async (total) => {
console.log(`Total monsters ${total}`);
const hardTotal = total;
while (total >= count && hardTotal >= count) {
console.log(`Fetching monsters ${count}`);
const url = CONFIG.urls.monstersAPI(count,take,searchTerm, homebrew, homebrewOnly, sources);
await fetch(url, headers)
.then(res => res.json())
.then(json => {
const availableMonsters = json.data.filter((monster) => {
const isHomebrew = (homebrew) ? monster.isHomebrew === true : false;
const available = monster.isReleased === true || isHomebrew;
return available;
});
const imageFiddledMonsters = imageFiddleMonsters(availableMonsters);
monsters.push(...imageFiddledMonsters);
})
.catch(error => {
console.log(`Error retrieving monsters at ${count}`);
console.log(error);
reject(error);
});
count += take;
}
return monsters;
}).then((data) => {
console.log(`Monster count: ${data.length}.`);
resolve(data);
}).catch(error => {
console.log("Error retrieving monsters");
console.log(error);
reject(error);
});
});
};
async function getIdCount(ids) {
return new Promise((resolve) => {
resolve(ids.length);
});
}
function extractMonstersById (cobaltId, ids) {
return new Promise((resolve, reject) => {
console.log(`Retrieving monsters for ${cobaltId} and ${ids}`);
let monsters = [];
let count = 0;
let take = 100;
getIdCount(ids).then(async (total) => {
const hardTotal = total;
while (total >= count && hardTotal >= count) {
const idSelection = ids.slice(count, count + take);
const headers
= authentication.CACHE_AUTH.exists(cobaltId).data !== null
? { headers: { Authorization: `Bearer ${authentication.CACHE_AUTH.exists(cobaltId).data}` } }
: {};
const url = CONFIG.urls.monsterIdsAPI(idSelection);
await fetch(url, headers)
.then((res) => res.json())
.then((json) => {
// console.log(json.data);
const availableMonsters = json.data.filter((monster) => monster.isReleased === true || monster.isHomebrew);
const imageFiddledMonsters = imageFiddleMonsters(availableMonsters);
monsters.push(...imageFiddledMonsters);
})
.catch((error) => {
console.log("Error retrieving monsters by id");
console.log(error);
reject(error);
});
count += take;
}
return monsters;
}).then((data) => {
console.log(`Monster count: ${data.length}.`);
resolve(data);
});
});
}
exports.extractMonsters = extractMonsters;
exports.extractMonstersById = extractMonstersById;