-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuggester.js
50 lines (47 loc) · 1.69 KB
/
suggester.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
var _ = require('underscore');
var lastfm = require('./lastfm');
var queue = require('./queue');
// Gets collated and ordered suggestions for `tracks`, firing `callback` with the suggestion list
// `suggestions` is an array of objects with keys `track`, `artist`, and `frequency`, sorted descending by frequency.
var suggest = function (tracks, callback) {
var suggestions = [];
var lastfmQueue = queue.create();
_.each(tracks, function (track) {
lastfmQueue.add(function (done) {
console.log(track.name, track.artist);
lastfm.getSimilarTracks(track.name, track.artist, '10', function (data) {
_.each(data, function (similarTrack) {
suggestions.push(similarTrack);
});
done();
});
});
// add a 1-sec delay to be nice to lastfm
lastfmQueue.add(queue.delay(1));
});
lastfmQueue.add(queue.triggerFor(function () {
// calculate frequency of each suggestion in the list
_.each(suggestions, function (suggestion) {
suggestion.frequency = _.filter(suggestions, function (s) {
return s.name === suggestion.name && s.artist === suggestion.artist;
}).length;
});
// sort by frequency, descending
suggestions.sort(function (a, b) {
return b.frequency - a.frequency;
});
// remove duplicate suggestions
suggestions = _.unique(suggestions, false, function (track) { return track.name+'\t'+track.artist; });
// remove suggestions that we already have
suggestions = _.reject(suggestions, function (s) {
return _.find(tracks, function (track) {
return track.name.toLowerCase() === s.name.toLowerCase() && track.artist.toLowerCase() === s.artist.toLowerCase();
});
});
// fire callback
callback(suggestions);
}));
};
_.extend(exports, {
suggest: suggest
});