-
Notifications
You must be signed in to change notification settings - Fork 0
/
igdbHelperFunctions.js
199 lines (175 loc) · 5.11 KB
/
igdbHelperFunctions.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
async function getCoverURL(id) {
let url = 'https://upload.wikimedia.org/wikipedia/commons/d/d1/Image_not_available.png';
await fetch(
'https://api.igdb.com/v4/covers',
{ method: 'POST',
headers: {
'Accept': 'application/json',
'Client-ID': `${process.env.igdbClientId}`,
'Authorization': `Bearer ${process.env.igdbAccessToken}`,
},
body: `where id = ${id}; fields url;`,
})
.then(response => response.json())
.then(response => {
if (response[0]) {
url = 'https:'.concat(response[0].url);
}
})
.then(url = url.replace('t_thumb', 't_1080p_2x'))
.catch(err => {
console.error(err);
});
return url;
}
async function getPlatformID(platform) {
await fetch(
'https://api.igdb.com/v4/platforms',
{ method: 'POST',
headers: {
'Accept': 'application/json',
'Client-ID': `${process.env.igdbClientId}`,
'Authorization': `Bearer ${process.env.igdbAccessToken}`,
},
body: `where name = "${platform}", alternative_name = "${platform}"; fields id;`,
})
.then(response => response.json())
.then(response => {
return response;
})
.catch(err => {
console.error(err);
});
}
async function getGameJson(body) {
let res;
await fetch(
'https://api.igdb.com/v4/games',
{ method: 'POST',
headers: {
'Accept': 'application/json',
'Client-ID': `${process.env.igdbClientId}`,
'Authorization': `Bearer ${process.env.igdbAccessToken}`,
},
body: body,
})
.then(response => response.json())
.then(response => {
res = response;
})
.catch(err => {
console.error(err);
});
return res;
}
async function getReleaseDates(id) {
let date;
await fetch(
'https://api.igdb.com/v4/release_dates',
{ method: 'POST',
headers: {
'Accept': 'application/json',
'Client-ID': `${process.env.igdbClientId}`,
'Authorization': `Bearer ${process.env.igdbAccessToken}`,
},
body: `where id = ${id}; fields category,checksum,created_at,date,game,human,m,platform,region,status,updated_at,y;`,
})
.then(response => response.json())
.then(response => {
date = response[0].human;
})
.catch(err => {
console.error(err);
});
return date;
}
async function getCompanyInfo(id) {
let involved_company;
await fetch(
'https://api.igdb.com/v4/involved_companies',
{ method: 'POST',
headers: {
'Accept': 'application/json',
'Client-ID': `${process.env.igdbClientId}`,
'Authorization': `Bearer ${process.env.igdbAccessToken}`,
},
body: `where id = ${id}; fields *;`,
})
.then(response => response.json())
.then(response => {
involved_company = response[0];
})
.catch(err => {
return console.error(err);
});
let developer;
await fetch(
'https://api.igdb.com/v4/companies',
{ method: 'POST',
headers: {
'Accept': 'application/json',
'Client-ID': `${process.env.igdbClientId}`,
'Authorization': `Bearer ${process.env.igdbAccessToken}`,
},
body: `where id = ${involved_company.company}; fields *;`,
})
.then(response => response.json())
.then(response => {
developer = response[0];
})
.catch(err => {
console.error(err);
});
return developer;
}
async function getGenres(id) {
let genre;
await fetch(
'https://api.igdb.com/v4/genres',
{ method: 'POST',
headers: {
'Accept': 'application/json',
'Client-ID': `${process.env.igdbClientId}`,
'Authorization': `Bearer ${process.env.igdbAccessToken}`,
},
body: `where id = ${id}; fields *;`,
})
.then(response => response.json())
.then(response => {
genre = response[0];
})
.catch(err => {
console.error(err);
});
return genre.name;
}
async function getFranchise(id) {
let franchise;
await fetch(
'https://api.igdb.com/v4/franchises',
{ method: 'POST',
headers: {
'Accept': 'application/json',
'Client-ID': `${process.env.igdbClientId}`,
'Authorization': `Bearer ${process.env.igdbAccessToken}`,
},
body: `where id = ${id}; fields *;`,
})
.then(response => response.json())
.then(response => {
franchise = response[0];
})
.catch(err => {
console.error(err);
});
return franchise.name;
}
module.exports = {
getCoverURL,
getPlatformID,
getGameJson,
getReleaseDates,
getCompanyInfo,
getGenres,
getFranchise,
};