-
Notifications
You must be signed in to change notification settings - Fork 0
/
songlocator-soundcloud.js
207 lines (191 loc) · 7.57 KB
/
songlocator-soundcloud.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
//@ sourceMappingURL=songlocator-soundcloud.map
// Generated by CoffeeScript 1.6.1
/*
SongLocator resolver for SoundCloud.
2013 (c) Andrey Popp <8mayday@gmail.com>
Based on Tomahawk YouTube resolver.
2012 (c) Thierry Göckel <thierry@strayrayday.lu>
*/
var __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; };
(function(root, factory) {
var SongLocator;
if (typeof exports === 'object') {
SongLocator = require('songlocator-base');
return module.exports = factory(SongLocator);
} else if (typeof define === 'function' && define.amd) {
return define(function(require) {
SongLocator = require('songlocator-base');
return root.SongLocator.SoundCloud = factory(SongLocator);
});
} else {
return root.SongLocator.SoundCloud = factory(SongLocator);
}
})(this, function(_arg) {
var BaseResolver, Resolver, capitalize, extend, unquote;
BaseResolver = _arg.BaseResolver, extend = _arg.extend;
capitalize = function(s) {
return s.replace(/(^|\s)([a-z])/g, function(m, p1, p2) {
return p1 + p2.toUpperCase();
});
};
unquote = function(s) {
return s.replace('"', '').replace("'", "");
};
Resolver = (function(_super) {
__extends(Resolver, _super);
function Resolver() {
return Resolver.__super__.constructor.apply(this, arguments);
}
Resolver.prototype.name = 'soundcloud';
Resolver.prototype.options = extend({
consumerKey: 'TiNg2DRYhBnp01DA3zNag'
}, BaseResolver.prototype.options);
Resolver.prototype.getTrack = function(found, orig) {
if (!this.options.includeCovers && found.search(/cover/i) !== -1 && orig.search(/cover/i) === -1) {
return null;
} else if (!this.options.includeRemixes && found.search(/(re)*mix/i) !== -1 && orig.search(/(re)*mix/i) === -1) {
return null;
} else if (!this.options.includeLive && found.search(/live/i) !== -1 && orig.search(/live/i) === -1) {
return null;
} else {
return found;
}
};
Resolver.prototype.betterArtwork = function(url) {
if (url != null) {
return url.replace('-large', '-t500x500');
}
};
Resolver.prototype.resolve = function(qid, track, artist, album) {
var query,
_this = this;
query = ("" + artist + " " + track).trim();
return this.request({
url: 'http://api.soundcloud.com/tracks.json',
params: {
consumer_key: this.options.consumerKey,
filter: 'streamable',
q: query
},
callback: function(error, resp) {
var r, result, results;
if (error != null) {
return;
}
if (resp.length === 0) {
return;
}
results = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = resp.length; _i < _len; _i++) {
r = resp[_i];
if (!(r != null ? r.streamable : void 0)) {
continue;
}
if ((r.title != null) && (r.title.toLowerCase().indexOf(artist.toLowerCase()) === -1 || r.title.toLowerCase().indexOf(track.toLowerCase()) === -1)) {
continue;
}
if (!this.getTrack(r.title, track)) {
continue;
}
_results.push(result = {
title: track,
artist: artist,
album: void 0,
source: this.name,
id: r.id,
linkURL: r.permalink_url,
imageURL: this.betterArtwork(r.artwork_url),
audioURL: "" + r.stream_url + ".json?client_id=" + this.options.consumerKey,
audioPreviewURL: void 0,
mimetype: "audio/mpeg",
duration: r.duration / 1000
});
}
return _results;
}).call(_this);
return _this.results(qid, [results[0]]);
}
});
};
Resolver.prototype.search = function(qid, searchString) {
var _this = this;
return this.request({
url: 'http://api.soundcloud.com/tracks.json',
params: {
consumer_key: this.options.consumerKey,
filter: 'streamable',
limit: this.options.searchMaxResults,
q: unquote(searchString)
},
callback: function(error, resp) {
var r, result, results, track;
if (error != null) {
return;
}
if (resp.length === 0) {
return;
}
results = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = resp.length; _i < _len; _i++) {
r = resp[_i];
if (r == null) {
continue;
}
if (!this.getTrack(r.title, '')) {
continue;
}
result = {
album: void 0,
source: this.name,
id: r.id,
linkURL: r.permalink_url,
imageURL: this.betterArtwork(r.artwork_url),
audioURL: "" + r.stream_url + ".json?client_id=" + this.options.consumerKey,
audioPreviewURL: void 0,
mimetype: 'audio/mpeg',
duration: r.duration / 1000
};
track = r.title;
if (track.indexOf(" - ") !== -1 && track.slice(track.indexOf(" - ") + 3).trim() !== "") {
result.title = track.slice(track.indexOf(" - ") + 3).trim();
result.artist = track.slice(0, track.indexOf(" - ")).trim();
} else if (track.indexOf(" -") !== -1 && track.slice(track.indexOf(" -") + 2).trim() !== "") {
result.title = track.slice(track.indexOf(" -") + 2).trim();
result.artist = track.slice(0, track.indexOf(" -")).trim();
} else if (track.indexOf(": ") !== -1 && track.slice(track.indexOf(": ") + 2).trim() !== "") {
result.title = track.slice(track.indexOf(": ") + 2).trim();
result.artist = track.slice(0, track.indexOf(": ")).trim();
} else if (track.indexOf("-") !== -1 && track.slice(track.indexOf("-") + 1).trim() !== "") {
result.title = track.slice(track.indexOf("-") + 1).trim();
result.artist = track.slice(0, track.indexOf("-")).trim();
} else if (track.indexOf(":") !== -1 && track.slice(track.indexOf(":") + 1).trim() !== "") {
result.title = track.slice(track.indexOf(":") + 1).trim();
result.artist = track.slice(0, track.indexOf(":")).trim();
} else if (track.indexOf("\u2014") !== -1 && track.slice(track.indexOf("\u2014") + 2).trim() !== "") {
result.title = track.slice(track.indexOf("\u2014") + 2).trim();
result.artist = track.slice(0, track.indexOf("\u2014")).trim();
} else if (r.title !== "" && r.user.username !== "") {
result.title = r.title;
result.artist = r.user.username;
} else {
continue;
}
_results.push(result);
}
return _results;
}).call(_this);
return _this.results(qid, results);
}
});
};
return Resolver;
})(BaseResolver);
return {
Resolver: Resolver
};
});