-
Notifications
You must be signed in to change notification settings - Fork 1
/
fanart.js
67 lines (57 loc) · 1.63 KB
/
fanart.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
'use strict';
module.exports = class FANART {
constructor(apiKey) {
this.engine = require('got');
this.config = require('./config.json');
this.checkError(apiKey, 'API key');
this.config.apiKey = apiKey;
this.movies = {
get: (id) => this.get('movies/', {id: id}),
latest: (dt) => this.get('movies/latest', {id: false, date: dt})
};
this.shows = {
get: (id) => this.get('tv/', {id: id}),
latest: (dt) => this.get('tv/latest', {id: false, date: dt})
};
this.music = {
artist: (id) => this.get('music/', {id: id}),
latest: (dt) => this.get('music/latest', {id: false, date: dt}),
album: (id) => this.get('music/albums/', {id: id}),
label: (id) => this.get('music/labels/', {id: id})
};
return { // expose
music: this.music,
shows: this.shows,
movies: this.movies,
config: this.config
};
}
checkError(key, message) {
if (!key && key !== false) throw new Error('Missing ' + message);
}
get(path, opts, count) {
this.checkError(opts.id, 'ID');
count = isNaN(count) ? 1 : count;
const url = [
this.config.url,
path,
opts.id ? opts.id : '',
'?api_key=',
this.config.apiKey,
opts.date ? '&date=' + opts.date : ''
].join('');
return this.engine(url, {
allowGetBody: true,
timeout: this.config.timeout
}).then(response => {
return response.body ? JSON.parse(response.body) : {}
}).catch(error => {
if (count < this.config.retries) {
count++;
return this.get(path, opts, count);
} else {
throw error;
}
});
}
};