-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsonglocator-spotify.js
131 lines (121 loc) · 4.36 KB
/
songlocator-spotify.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
//@ sourceMappingURL=songlocator-spotify.map
// Generated by CoffeeScript 1.6.1
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.Spotify = factory(SongLocator);
});
} else {
return root.SongLocator.Spotify = factory(SongLocator);
}
})(this, function(_arg) {
var BaseResolver, Resolver, extend;
BaseResolver = _arg.BaseResolver, extend = _arg.extend;
Resolver = (function(_super) {
__extends(Resolver, _super);
function Resolver() {
return Resolver.__super__.constructor.apply(this, arguments);
}
Resolver.prototype.name = 'spotify';
Resolver.prototype.options = extend(BaseResolver.prototype.options, {
resolveImageURL: true
});
Resolver.prototype.parseImageURL = function(data) {
data = (/<img[^>]+>/.exec(data))[0];
return data = (/src="([^\"]+)"/.exec(data))[1];
};
Resolver.prototype.resolveImageURL = function(id, callback) {
var _this = this;
return this.request({
method: 'GET',
url: "http://open.spotify.com/track/" + (id.split(':')[2]),
rawResponse: true,
callback: function(error, data) {
if (error || !data) {
callback(void 0);
}
return callback(_this.parseImageURL(data));
}
});
};
Resolver.prototype.search = function(qid, query) {
var _this = this;
return this.request({
method: 'GET',
url: 'http://ws.spotify.com/search/1/track.json',
params: {
q: query
},
callback: function(error, data) {
var imagesResolved, r, results, tracks;
if (error != null) {
return;
}
if (data.tracks.length === 0) {
return;
}
tracks = data.tracks.slice(0, _this.options.searchMaxResults);
imagesResolved = 0;
results = (function() {
var _i, _len, _results,
_this = this;
_results = [];
for (_i = 0, _len = tracks.length; _i < _len; _i++) {
r = tracks[_i];
_results.push((function(r) {
var a, result, _ref;
result = {
title: r.name,
artist: ((function() {
var _j, _len1, _ref, _results1;
_ref = r.artists;
_results1 = [];
for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
a = _ref[_j];
_results1.push(a.name);
}
return _results1;
})()).join(', '),
album: ((_ref = r.album) != null ? _ref.name : void 0) != null,
source: _this.name,
id: r.href,
linkURL: "http://open.spotify.com/track/" + (r.href.split(':')[2]),
imageURL: void 0,
audioURL: void 0,
audioPreviewURL: void 0,
mimetype: void 0,
duration: r.length
};
if (_this.options.resolveImageURL) {
_this.resolveImageURL(result.id, function(imageURL) {
result.imageURL = imageURL;
imagesResolved = imagesResolved + 1;
if (imagesResolved === tracks.length) {
return _this.results(qid, results);
}
});
}
return result;
})(r));
}
return _results;
}).call(_this);
if (!_this.options.resolveImageURL) {
return _this.results(qid, results);
}
}
});
};
return Resolver;
})(BaseResolver);
return {
Resolver: Resolver
};
});