-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
327 lines (260 loc) · 6.48 KB
/
api.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
const config = require('./config.js');
const https = require('https');
const querystring = require('querystring');
const API = function(config) {
if (!(this instanceof API)) {
return new API(config);
}
this.api_key = config.API_KEY;
this.maj_api_key = config.MAJ_API_KEY;
this.url = 'comicvine.gamespot.com';
this.options = {};
this.limit = 20;
this.format = 'json';
}
// utils
API.prototype.get_prefixe = function (path) {
switch(path.replace('/', '')) {
case 'issue':
return 4000;
case 'volume':
return 4050;
default:
return '';
}
return;
}
API.prototype.serialize = function (array, first = true) {
var str = '';
Object.keys(array).forEach(function(key) {
// first param
str += first ? '?' : '&';
first = false;
// filters
if (typeof array[key] === "object") {
str += (key + '=');
var filters = array[key];
var first_filter = true;
Object.keys(filters).forEach(function(filters_key) {
if (!first_filter) {
str += ',';
}
str += (filters_key + ':' + querystring.escape(filters[filters_key]));
first_filter = false;
});
str += str;
} else {
str += (key + '=' + querystring.escape(array[key]));
}
});
return str;
}
API.prototype.set_options = function(path, id, query_params) {
var prefixe = this.get_prefixe(path);
if (!query_params.format) {
query_params.format = this.format;
}
if (!query_params.limit && path.replace('/', '') === 'search') {
query_params.limit = this.limit;
}
//console.log(path, id, query_params)
// search filters
var field_list = '';
if (path.replace('/', '') === 'search') {
field_list = "&field_list=id,image,publisher,name,start_year,count_of_issues";
}
this.options = {
hostname: this.url,
path: '/api/' + path.replace('/', '') + '/'
+ ((id && prefixe) ? (prefixe + '-' + id + '/') : '') // {prefixe}-{id}
+ '?api_key=' + this.api_key
+ field_list
+ this.serialize(query_params, false),
headers: {
'User-Agent': 'Mozilla/5.0'
}
};
return;
}
// requests
API.prototype.get = function (path, params, callback, data, api) {
return this.request('GET', path, params, callback, data);
}
API.prototype.request = function (method, path, params, callback, data, api) {
var id = null
//console.log(method, path, params)
//console.log(typeof params)
if (typeof params === 'number') {
id = params;
params = {}
} else if (typeof params === 'function') {
callback = params
params = {}
}
//
var api = this;
// setting options
this.set_options(path, id, params);
console.log(this.options)
// volumes with more than 100 issues
const offset = params.offset || 0;
var content = '';
//console.log(this.options)
https.get(this.options, function(res) {
res.on('data', function(chunk) {
content += chunk;
}).on('end', function() {
var json = JSON.parse(content);
var results = json.results;
if (method === 'GET' && path === 'issues/') {
var number_of_page_results = json.number_of_page_results;
var number_of_total_results = json.number_of_total_results;
var results_offset = json.offset;
var results_limit = json.limit;
var checked_results = results_offset + number_of_page_results;
var is_last_page = checked_results >= number_of_total_results
if (data) {
results = data.concat(results);
}
if (!is_last_page) {
params.offset = offset + results_limit;
api.get(path, params, callback, results, api);
} else {
callback(results);
}
} else {
callback(path.replace('/', '') === 'search' ? json : json.results);
}
});
}).on("error", (err) => {
//console.log("Error: " + err.message);
});
return;
}
module.exports = new API(config)
/////////////
const API_tests = function() {
const api = new API(config)
// function
this.search = function() {
// params
var params = {
query: 'Avengers',
resources: 'volume'
}
//
//console.log("TEST: search()", 'search/', params)
// search
api.get('search/', params, function(data) {
// DISPLAY
// {name} {'Volume' || resource_type} {start_year} ({count_of_issues} issues) ({publisher.name})
// Avengers Volume 2018 (19 issues) (Marvel)
// {image->thumb_url}
// img : https://static.comicvine.com/uploads/square_mini/6/67663/6406863-01.jpg
//console.log(data);
})
return true;
}
this.issues = function() {
// params
var params = {
filter: {
volume: 110496
}
}
//
//console.log("TEST: issues()", 'issues/', params)
api.get('issues/', params, function(issues) {
// get issues in file
const volume = params.filter.volume;
issues.forEach(issue => {
// if aleady in the file
// check differences => changes + update
// else
// add to the volume entity
console.log({
id: issue.id,
name: issue.name,
issue_number: issue.issue_number,
image: issue.image.original_url,
store_date: issue.store_date,
added: false,
updated: false,
read: false
});
})
})
}
this.issue = function() {
// params
var issue = 668770
//
//console.log("TEST: issue()", 'issue/', issue)
api.get('issue/', issue, function(data) {
//console.log(data);
})
}
this.volume = function() {
// params
var volume = 110496
//
//console.log("TEST: volume()", 'volume/', volume)
api.get('volume/', volume, function(data) {
//console.log(data);
})
}
}
////////////
/*module.exports = {
api: new API(config),
tests: new API_tests(config)
}*/
//var api = new API(config);
// issues (looking for all the issues of a volume)
// todo: if there're 100+ issues, need pagination
// maj example
/*
//https://comicvine.gamespot.com/api/issues/?api_key=XXXXXX&filter=volume:110496&format=json
*/
/*
var express = require("express");
var app = express();
var fs = require('fs');
*/
// INIT
/*
var file_content;
var stats = fs.statSync("data.json");
if (!stats.isFile()) {
console.log("nonono");
var test = {
test: 'ok',
aaah: 'kkakaka',
h: 'afeknle'
};
fs.writeFile('data.json', JSON.stringify(test), function (err) {
if (err) throw err;
console.log('File is created successfully.');
});
}*/
/*
app.get('/', function(req, res) {
res.setHeader('Content-Type', 'text/plain');
res.send('Accueil');
var test = {
test: 'ok',
aaah: 'kkakaka',
h: 'afeknle'
};
console.log(
test,
JSON.stringify(test),
JSON.parse(JSON.stringify(test)));
});
app.get('/search', function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.render('test.ejs', {test: req.params.test});
res.send('test');
});
app.listen(3000);
*/