adds getRecommended method to api module
This commit is contained in:
parent
5be2aaf7af
commit
4366bd1384
1 changed files with 48 additions and 1 deletions
49
lib/api.js
49
lib/api.js
|
|
@ -30,6 +30,7 @@ proto.refresh_token = false;
|
|||
proto.api_url = 'https://api.npr.org/';
|
||||
proto.auth_endpoint = 'authorization/v2/authorize';
|
||||
proto.token_endpoint = 'authorization/v2/token';
|
||||
proto.recommendation_endpoint = 'listening/v2/recommendations';
|
||||
|
||||
/***************************** AUTH *******************************/
|
||||
proto.authenticate = function(cb) {
|
||||
|
|
@ -70,7 +71,7 @@ proto.getAccessToken = function() {
|
|||
self.api_url,
|
||||
self.auth_endpoint,
|
||||
self.token_endpoint,
|
||||
{ 'User-Agent': 'NPR%20One/2 CFNetwork/711.4.6 Darwin/14.0.0'}
|
||||
{ 'User-Agent': 'Raspberry Pi'}
|
||||
);
|
||||
|
||||
var oauth_cb = function(err, access_token, refresh_token, results) {
|
||||
|
|
@ -103,6 +104,52 @@ proto.getAccessToken = function() {
|
|||
|
||||
/*********************** RECOMMENDATIONS **************************/
|
||||
|
||||
proto.getRecommendations = function(channel) {
|
||||
|
||||
var self = this;
|
||||
|
||||
channel = channel || 'npr';
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
|
||||
if(! self.access_token)
|
||||
return reject('access token not available');
|
||||
|
||||
var options = {
|
||||
url: self.api_url + self.recommendation_endpoint,
|
||||
json: true,
|
||||
qs: {
|
||||
channel: channel
|
||||
},
|
||||
headers: {
|
||||
'User-Agent': 'Raspberry Pi',
|
||||
'Authorization': 'Bearer ' + self.access_token
|
||||
}
|
||||
};
|
||||
|
||||
request(options, function(err, response, body) {
|
||||
|
||||
if(err)
|
||||
return reject(err);
|
||||
|
||||
if(response.statusCode !== 200)
|
||||
return reject(body);
|
||||
|
||||
var urls = body.items.map(function(item) {
|
||||
|
||||
return item.links.audio.map(function(a) {
|
||||
return a.href;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
urls = [].concat.apply([], urls);
|
||||
|
||||
resolve(urls);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue