-
Notifications
You must be signed in to change notification settings - Fork 0
/
songlocator-rdio.js
138 lines (119 loc) · 3.99 KB
/
songlocator-rdio.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
//@ sourceMappingURL=songlocator-rdio.map
// Generated by CoffeeScript 1.6.1
/*
SongLocator resolver for rdio.
2013 (c) Andrey Popp <[email protected]>
*/
var BaseResolver, OAuth, getArgs,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
BaseResolver = require('songlocator-base').BaseResolver;
OAuth = require('oauth').OAuth;
getArgs = function(argc) {
var args;
args = {
method: null,
params: null,
callback: null
};
if (argc.length >= 1) {
args['method'] = argc[0];
}
if (argc.length === 2) {
if (typeof argc[1] === 'function') {
args['callback'] = argc[1];
} else {
args['params'] = argc[1];
}
} else if (argc.length >= 3) {
args['params'] = argc[1];
args['callback'] = argc[2];
}
return args;
};
exports.Resolver = (function(_super) {
__extends(Resolver, _super);
Resolver.prototype.name = 'rdio';
Resolver.prototype.score = 0.9;
Resolver.prototype.endpoint = 'http://api.rdio.com/1/';
function Resolver() {
Resolver.__super__.constructor.apply(this, arguments);
this.oauth = (function() {
if ((this.options.consumerKey != null) && (this.options.consumerSecret != null)) {
return new OAuth('http://api.rdio.com/oauth/request_token', 'http://api.rdio.com/oauth/access_token', this.options.consumerKey, this.options.consumerSecret, '1.0', 'oob', 'HMAC-SHA1');
} else {
throw new Error('provide consumerKey and consumerSecret');
}
}).call(this);
}
Resolver.prototype.makeRequest = function(method, params, callback) {
var accessToken, args, callbackWrapper, oauthToken, oauthTokenSecret;
args = getArgs(arguments);
params = args.params || {};
params['method'] = args.method;
callback = args.callback || function() {};
callbackWrapper = function(error, results) {
try {
if (results !== null) {
results = JSON.parse(results);
}
} catch (e) {
}
return callback.call({}, error, results);
};
accessToken = {};
oauthToken = accessToken.oauthAccessToken || '';
oauthTokenSecret = accessToken.oauthAccessTokenSecret || '';
return this.oauth.post(this.endpoint, oauthToken, oauthTokenSecret, params, 'application/x-www-form-urlencoded', callbackWrapper);
};
Resolver.prototype.makeSearch = function(qid, query, search) {
var _this = this;
if (search == null) {
search = true;
}
return this.makeRequest('search', {
query: query,
types: 'Track'
}, function(error, response) {
var r, result, results;
if (error != null) {
return;
}
results = (function() {
var _i, _len, _ref, _results;
_ref = response.result.results.slice(0, this.options.maxSearchResults);
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
r = _ref[_i];
_results.push(result = {
title: r.name,
artist: r.artist,
album: r.album,
source: this.name,
id: r.key,
linkURL: r.shortUrl,
imageURL: r.icon,
audioURL: void 0,
audioPreviewURL: void 0,
mimetype: void 0,
duration: r.duration
});
}
return _results;
}).call(_this);
if (!search && results.length > 0) {
results = [results[0]];
}
return _this.results(qid, results);
});
};
Resolver.prototype.search = function(qid, query) {
return this.makeSearch(qid, query);
};
Resolver.prototype.resolve = function(qid, title, artist, album) {
var query;
query = "" + (title || '') + " " + (artist || '') + " " + (album || '');
return this.makeSearch(qid, query, false);
};
return Resolver;
})(BaseResolver);