This repository has been archived by the owner on Dec 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
342 lines (314 loc) · 10.1 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
var http = require('http');
/**
* This callback type is called `requestCallback` and is displayed as a global symbol.
*
* @callback callback
* @param {object|string} result - Result of the request
*/
module.exports = {
config: {
domain: "api.spiget.org",
port: 80,
versionPath: "v1",
userAgent: "Spiget-NodeJSAPI/1.0"
},
/**
* Makes an API call
* @param {string} uri - Url which should be querried
* @param {Object[]} parameters - Parameters of the query
* @param {callback} cb - The callback that handles the response.
*/
call: function (uri, parameters, cb) {
var path = "/" + uri;
for (var i = 0; i < parameters.length; i++) {
if (i == 0) {
path += "?" + parameters[i].key + "=" + parameters[i].val;
} else {
path += "&" + parameters[i].key + "=" + parameters[i].val;
}
}
var options = {
host: this.config.domain,
port: this.config.port,
path: "/" + this.config.versionPath + path,
headers: {
"User-Agent": this.config.userAgent
}
};
http.get(options, function (resp) {
var result = "";
resp.on('data', function (chunk) {
result += chunk;
});
resp.on('end', function (chunk) {
try {
var parsed = JSON.parse(result);
if (cb)
cb(parsed);
} catch (err) {
if (cb)
cb(result);
}
});
}).on("error", function (e) {
console.log("Got error: " + e.message);
});
},
/**
* Returns a list of available Resources.
* @param {integer} size - Number of Resources which should be returned
* @param {callback} cb - The callback that handles the response.
*/
getResources: function (size, cb) {
if (typeof (size) == "function") {
cb = size;
size = 10;
} else if (typeof (size) == "undefined") {
size = 10;
cb = false;
}
this.call("resources", [
{
key: "size",
val: size
}
], cb);
},
/**
* Returns a list of new available Resources.
* @param {integer} size - Number of Resources which should be returned
* @param {callback} cb - The callback that handles the response.
*/
getNewResources: function (size, cb) {
if (typeof (size) == "function") {
cb = size;
size = 10;
} else if (typeof (size) == "undefined") {
size = 10;
cb = false;
}
this.call("resources/new", [
{
key: "size",
val: size
}
], cb);
},
/**
* Returns detailed information about a resource
* @param {string} search - Name or id of the resource
* @param {callback} cb - The callback that handles the response.
*/
getResource: function (search, cb) {
if (typeof (search) != "undefined") {
if (typeof (search) == "function") {
return false;
}
} else {
return false;
}
this.call("resources/" + search, [], cb);
},
/**
* Returns the list of available versions of the resource
* @param {string} search - Name or id of the resource
* @param {callback} cb - The callback that handles the response.
*/
getResourceVersions: function (search, cb) {
if (typeof (search) != "undefined") {
if (typeof (search) == "function") {
return false;
}
} else {
return false;
}
this.call("resources/" + search + "/versions", [], cb);
},
/**
* Returns detailed information about the version
* @param {string} search - Name or id of the resource
* @param {string} version - Version number
* @param {callback} cb - The callback that handles the response.
*/
getVersionDetails: function (search, version, cb) {
if (typeof (search) != "undefined") {
if (typeof (search) == "function") {
return false;
}
} else {
return false;
}
this.call("resources/" + search + "/versions/" + version, [], cb);
},
/**
* Returns the download link of the version
* @param {string} search - Name or id of the resource
* @param {string} version - Version number
* @param {callback} cb - The callback that handles the response.
*/
getVersionDownload: function (search, version, cb) {
if (typeof (search) != "undefined") {
if (typeof (search) == "function") {
return false;
}
} else {
return false;
}
this.call("resources/" + search + "/versions/" + version + "/download", [], cb);
},
/**
* Returns the author of the resource
* @param {string} search - Name or id of the resource
* @param {callback} cb - The callback that handles the response.
*/
getResourceAuthor: function (search, cb) {
if (typeof (search) != "undefined") {
if (typeof (search) == "function") {
return false;
}
} else {
return false;
}
this.call("resources/" + search + "/author", [], cb);
},
/**
* Returns a list of available categories.
* @param {callback} cb - The callback that handles the response.
*/
getCategories: function (cb) {
this.call("categories", [], cb);
},
/**
* Returns detailed information about a category
* @param {string} search - Name or id of the category
* @param {callback} cb - The callback that handles the response.
*/
getCategory: function (search, cb) {
if (typeof (search) != "undefined") {
if (typeof (search) == "function") {
return false;
}
} else {
return false;
}
this.call("categories/" + search, [], cb);
},
/**
* Returns a list of resources in the given category
* @param {string} search - Name or id of the category
* @param {integer} size - Number of Resources which should be returned
* @param {callback} cb - The callback that handles the response.
*/
getCategoryResources: function (search, size, cb) {
if (typeof (search) != "undefined") {
if (typeof (search) == "function") {
return false;
}
} else {
return false;
}
this.call("categories/" + search + "/resources", [{key: "size", val: size}], cb);
},
/**
* Returns a list of available authors.
* @param {integer} size - Number of authors which should be returned
* @param {callback} cb - The callback that handles the response.
*/
getAuthors: function (size, cb) {
if (typeof (size) == "function") {
cb = size;
size = 10;
} else if (typeof (size) == "undefined") {
size = 10;
cb = false;
}
this.call("authors", [
{
key: "size",
val: size
}
], cb);
},
/**
* Returns detailed information about an author
* @param {string} search - Name or id of the resource
* @param {callback} cb - The callback that handles the response.
*/
getAuthor: function (search, cb) {
if (typeof (search) != "undefined") {
if (typeof (search) == "function") {
return false;
}
} else {
return false;
}
this.call("authors/" + search, [], cb);
},
/**
* Returns a list of resources of the given author
* @param {string} search - Name or id of the author
* @param {callback} cb - The callback that handles the response.
*/
getAuthorResources: function (search, cb) {
if (typeof (search) != "undefined") {
if (typeof (search) == "function") {
return false;
}
} else {
return false;
}
this.call("authors/" + search + "/resources", [], cb);
},
/**
* Returns a list of new authors
* @param {integer} size - Number of authors which should be returned
* @param {callback} cb - The callback that handles the response.
*/
getNewAuthors: function (size, cb) {
if (typeof (size) == "function") {
cb = size;
size = 10;
} else if (typeof (size) == "undefined") {
size = 10;
cb = false;
}
this.call("authors/new", [
{
key: "size",
val: size
}
], cb);
},
/**
* Search for a resource
* @param {string} query - What to search for
* @param {string} field - The field that should be searched (only name and tag for resources)
* @param {callback} cb - The callback that handles the response.
*/
searchResource: function (query, field, cb) {
if (typeof (query) != "undefined") {
if (typeof (query) == "function") {
return false;
}
} else {
return false;
}
this.call("search/" + query + "/" + field, [], cb);
},
/**
* Search for a´n author
* @param {string} query - What to search for
* @param {string} field - The field that should be searched (only username for authors)
* @param {callback} cb - The callback that handles the response.
*/
searchAuthor: function (query, field, cb) {
if (typeof (query) != "undefined") {
if (typeof (query) == "function") {
return false;
}
} else {
return false;
}
this.call("search/authors/" + query + "/" + field, [], cb);
},
}