-
Notifications
You must be signed in to change notification settings - Fork 1
/
LastFmDao.js
143 lines (129 loc) · 3.63 KB
/
LastFmDao.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
var _ = require("underscore");
var winston = require("winston");
var LastFmDao = (function () {
var lastfmNode;
function LastFmDao(lfmNode) {
lastfmNode = lfmNode;
}
LastFmDao.prototype.getSession = function (token, callback) {
if (!token) {
callback("Invalid token", null);
return;
}
lastfmNode.session({
token: token,
handlers: {
success: function(session) {
callback(null, session);
return;
},
error: function(error) {
winston.error('getSession error', error);
callback(error, null);
return;
}
}
});
};
LastFmDao.prototype.getUserInfo = function (username, callback) {
lastfmNode.request("user.getInfo", {
user: username,
handlers: {
success: function(data) {
var lastfmProfileImage = null;
if (data && data.user) {
_.each(data.user.image, function(image) {
if (image.size == 'large') {
lastfmProfileImage = image['#text'];
return;
}
});
}
callback(null, { lastfmProfileImage: lastfmProfileImage });
},
error: function(err) {
winston.error('getUserInfo error', err);
callback(err, null);
}
}
});
};
var makeTrackData = function(data) {
if (data) {
var artist = data.artist ? data.artist['#text'] : null;
var name = data.name;
var url = data.url;
var nowPlaying = data['@attr'] && data['@attr']['nowplaying'] == 'true';
var image = null;
_.each(data.image, function(curImage) {
if (curImage.size == 'small') {
image = curImage['#text'];
return; //break
}
});
if (artist && name) {
return { artist: artist, name: name, image: image, url: url, nowPlaying: nowPlaying };
}
}
return null;
};
// Returns 3 most recent tracks, not including duplicate now playing/last played
LastFmDao.prototype.getRecentTracks = function (username, callback) {
lastfmNode.request("user.getRecentTracks", {
user: username,
limit: "4", // 3 plus possible duplicate
handlers: {
success: function(data) {
if (!data || !data.recenttracks || !data.recenttracks.track) {
// Only log error, do not pass back error in callback so that async.map will continue with other requests
winston.error('getRecentTracks invalid data', data);
callback(null, null);
return;
}
var tracks = [];
_.each(data.recenttracks.track, function(track) {
var trackData = makeTrackData(track);
if (trackData) {
tracks.push(trackData);
}
});
// remove duplicate e.g. if now playing is the same as the last scrobble
if (tracks.length > 1 && tracks[0].artist == tracks[1].artist && tracks[0].name == tracks[1].name) {
tracks.splice(1, 1);
}
callback(null, tracks);
},
error: function(err) {
// Only log error, do not pass back error in callback so that async.map will continue with other requests
winston.error('getRecentTracks error', err);
callback(null, null);
}
}
});
};
LastFmDao.prototype.getTasteometer = function (users, callback) {
lastfmNode.request("tasteometer.compare", {
type1: 'user',
type2: 'user',
value1: users.user1,
value2: users.user2,
handlers: {
success: function(data) {
if (data && data.comparison && data.comparison.result) {
callback(null, data.comparison.result.score);
}
else {
callback(null, 0);
}
},
error: function(err) {
// Only log error, do not pass back error in callback so that async.map will continue with other requests
winston.error('getTasteometer error', err);
callback(null, 0);
}
}
});
};
return LastFmDao;
})();
exports.LastFmDao = LastFmDao;