-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
108 lines (93 loc) · 2.91 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
var Promise = require('promise');
var EventEmitter = require('events').EventEmitter;
var request = require('request');
var version = '1.0.2';
var Audiosearch = function (oauthKey, oauthSecret, oauthHost) {
var self = this;
if (!oauthKey || !oauthSecret) {
throw new Error('Audiosear.ch API requires key and secret to initialize.');
}
self._creds = {
key: oauthKey,
secret: oauthSecret,
host: oauthHost || 'https://www.audiosear.ch',
}
self._ee = new EventEmitter();
self._authorize().then(function (token) {
self._token = token;
self._ee.emit('authComplete');
});
}
Audiosearch.prototype._authorize = function () {
var self = this;
return new Promise(function (resolve, reject) {
var params = {'grant_type':'client_credentials'};
var unencoded_sig = self._creds.key + ':' + self._creds.secret;
var signature = new Buffer(unencoded_sig).toString('base64');
var options = {
url: self._creds.host + '/oauth/token',
qs: params,
headers: {
'Authorization': 'Basic ' + signature,
'Content-Type': 'application/x-www-form-urlencoded'
}
};
request.post(options, function (err, res, body) {
if (res.statusCode !== 200) {
console.warn(res);
reject();
}
var token = JSON.parse(body).access_token;
resolve(token);
});
});
};
Audiosearch.prototype.get = function (url, params) {
var self = this;
return new Promise(function (resolve, reject) {
var options = {
url: self._creds.host + '/api' + url,
qs: params,
headers: {
'Authorization': 'Bearer ' + self._token,
'User-Agent': 'request'
}
};
if (!self._token) {
self._ee.once('authComplete', function () {
return self.get(url, params).then(resolve);
});
} else {
request(options, function (err, res, body) {
if (err || res.statusCode !== 200) {
return reject(err);
}
resolve(JSON.parse(body));
});
}
});
};
Audiosearch.prototype.searchEpisodes = function (queryString, params) {
return this.get('/search/episodes/'+encodeURI(queryString), params);
};
Audiosearch.prototype.searchPeople = function (queryString, params) {
return this.get('/search/people/'+encodeURI(queryString), params);
};
Audiosearch.prototype.getShow = function (showId) {
return this.get('/shows/' + showId);
};
Audiosearch.prototype.getEpisode = function (episodeId) {
return this.get('/episodes/' + episodeId);
};
Audiosearch.prototype.getTrending = function () {
return this.get('/trending/');
};
Audiosearch.prototype.getRelated = function (id, _type) {
var type = _type || 'episodes';
return this.get('/'+type+'/'+id+'/related/');
};
Audiosearch.prototype.getTastemakers = function (_numResults) {
var numResults = _numResults === undefined ? 5 : _numResults;
return this.get('/tastemakers/episodes/' + numResults);
};
module.exports = Audiosearch;