1 | # lastfm-node
|
2 |
|
3 | Read and write to users recent plays on Last.fm.
|
4 |
|
5 | ## Installation
|
6 |
|
7 | npm install lastfm
|
8 |
|
9 | ## Usage
|
10 |
|
11 | var LastFmNode = require('lastfm').LastFmNode;
|
12 |
|
13 | var lastfm = new LastFmNode({
|
14 | api_key: 'apikey', // sign-up for a key at http://www.last.fm/api
|
15 | secret: 'secret'
|
16 | });
|
17 |
|
18 | ## Documentation
|
19 |
|
20 | ### LastFmRequest
|
21 |
|
22 | lastfm.request(method, options);
|
23 |
|
24 | Returns a `LastFmRequest` instance.
|
25 |
|
26 | Send request to Last.fm. Requests automatically include the API key and are signed and/or sent via POST as described in the Last.fm API documentation.
|
27 |
|
28 | Methods:
|
29 |
|
30 | Accepts any Last.fm API method name, eg "artist.getInfo".
|
31 |
|
32 | Options:
|
33 |
|
34 | All options are passed through to Last.fm with the exception of the following.
|
35 |
|
36 | - *write*
|
37 |
|
38 | Force request to act as a write method. Write methods are signed and sent via POST. Useful for new methods not yet recognised by lastfm-node.
|
39 |
|
40 | - *signed*
|
41 |
|
42 | Force request to be signed. See Last.fm API docs for signature details. Useful for new methods not yet recognised by lastfm-node.
|
43 |
|
44 | - *handlers*
|
45 |
|
46 | Default event handlers to attach to the request object on creation.
|
47 |
|
48 | Events:
|
49 |
|
50 | - *success(data)*
|
51 |
|
52 | Raw data returned by Last.fm.
|
53 |
|
54 | - *error(error)*
|
55 |
|
56 | Ruh-roh. Either a error returned by Last.fm or a transmission error.
|
57 |
|
58 | ### RecentTracksStream
|
59 |
|
60 | lastfm.stream(username);
|
61 |
|
62 | Returns: a `RecentTracksStream` instance
|
63 |
|
64 | Methods:
|
65 |
|
66 | - *start()*
|
67 |
|
68 | Start streaming recent track info.
|
69 |
|
70 | - *stop()*
|
71 |
|
72 | Stop streaming recent track info.
|
73 |
|
74 | - *on(event, listener)*
|
75 |
|
76 | Adds a listener for the specified event.
|
77 |
|
78 | - *removeListener(event, listener)*
|
79 |
|
80 | Removes the listener for the specified event.
|
81 |
|
82 | Options:
|
83 |
|
84 | - *autostart*
|
85 |
|
86 | Start streaming automatically. Defaults to false.
|
87 |
|
88 | - *handlers*
|
89 |
|
90 | Default event handlers to attach to the request object on creation.
|
91 |
|
92 | - *lastPlayed*, *nowPlaying*, *scrobbled*, *stoppedPlaying*, *error*
|
93 |
|
94 | **Deprecated:** Event listeners.
|
95 |
|
96 | Events:
|
97 |
|
98 | - *lastPlayed(track)*
|
99 |
|
100 | The user's last scrobbled track.
|
101 |
|
102 | - *nowPlaying(track)*
|
103 |
|
104 | Track the user is currently listening to.
|
105 |
|
106 | - *scrobbled(track)*
|
107 |
|
108 | Now playing track has been scrobbled.
|
109 |
|
110 | - *stoppedPlaying(track)*
|
111 |
|
112 | User stopped listening to current track.
|
113 |
|
114 | - *error(error)*
|
115 |
|
116 | Ruh-roh.
|
117 |
|
118 | ### LastFmSession
|
119 |
|
120 | lastfm.session([user], [key]);
|
121 |
|
122 | Returns: a `LastFmSession` instance.
|
123 |
|
124 | If no key is supplied then the authorise() method must be used before the session can be used to make authenticated calls. See the last.fm API documentation for more info.
|
125 |
|
126 | Public properties:
|
127 |
|
128 | - *user*
|
129 |
|
130 | The username of the Last.fm user associated with the session.
|
131 |
|
132 | - *key*
|
133 |
|
134 | The session key. Either passed in or generated using authorise().
|
135 |
|
136 | Methods:
|
137 |
|
138 | - *authorise(token, [options])*
|
139 |
|
140 | Authorises user with Last.fm api. See last.fm documentation.
|
141 | Options:
|
142 | - *handlers*
|
143 | Default event handlers to attach to the authorise call.
|
144 |
|
145 | - *on(event, handler)*
|
146 |
|
147 | Adds a listener for the specified event.
|
148 |
|
149 | - *removeListener(event, handler)*
|
150 |
|
151 | Removes the listener for the specified event.
|
152 |
|
153 | - *isAuthorised()*
|
154 |
|
155 | Returns true if the session has been authorised or a key was specified in the constructor.
|
156 |
|
157 | Events:
|
158 |
|
159 | - *authorised(session)*
|
160 |
|
161 | Authorisation of session was successful.
|
162 | Note: Only emitted after a call to authorise(). Keys supplied in the constructor are assumed to be valid.
|
163 |
|
164 | - *error(track, error)*
|
165 |
|
166 | Ruh-roh.
|
167 |
|
168 | ### LastFmUpdate
|
169 |
|
170 | lastfm.update(method, session, options);
|
171 |
|
172 | Returns a `LastFmUpdate` instance.
|
173 |
|
174 | Valid methods are 'nowplaying' and 'scrobble'.
|
175 |
|
176 | An authorised `LastFmSession` instance is required to make a successful update.
|
177 |
|
178 | Options:
|
179 |
|
180 | - *track*
|
181 |
|
182 | Track for nowplaying and scrobble requests. Uses same format as returned by `RecentTracksStream` events.
|
183 |
|
184 | - *timestamp*
|
185 |
|
186 | Required for scrobble requests. Timestamp is in unix time (seconds since 01-01-1970 and is in UTC time).
|
187 |
|
188 | - *handlers*
|
189 |
|
190 | Default event handlers to attach to the request object on creation.
|
191 |
|
192 | - *success*
|
193 |
|
194 | **Deprecated:** Listener for `success` event.
|
195 |
|
196 | - *error*
|
197 |
|
198 | **Deprecated:** Listener for `error` event.
|
199 |
|
200 | Events:
|
201 |
|
202 | - *success(track)*
|
203 |
|
204 | Update request was successful.
|
205 |
|
206 | - *error(track, error)*
|
207 |
|
208 | Ruh-roh.
|
209 |
|
210 | ### LastFmInfo
|
211 |
|
212 | lastfm.info(itemtype, [options]);
|
213 |
|
214 | Returns: a `LastFmInfo` instance.
|
215 |
|
216 | Gets extended info about specified item.
|
217 |
|
218 | Public properties:
|
219 |
|
220 | - *itemtype*
|
221 |
|
222 | Any Last.fm item with a getInfo method. eg user, track, artist, etc.
|
223 |
|
224 | Options:
|
225 |
|
226 | - *handlers*
|
227 |
|
228 | Event handlers to attach to object at creation.
|
229 |
|
230 | - *various*
|
231 |
|
232 | Params as specified in Last.fm API, eg user: "username"
|
233 |
|
234 | - *success*
|
235 |
|
236 | **Deprecated:** Listener for `success` event.
|
237 |
|
238 | - *error*
|
239 |
|
240 | **Deprecated:** Listener for `error` event.
|
241 |
|
242 | Special cases:
|
243 |
|
244 | When requesting track info the `track` param can be either the track name or a track object as returned by `RecentTracksStream`.
|
245 |
|
246 |
|
247 | ## Example
|
248 |
|
249 | var LastFmNode = require('lastfm').LastFmNode;
|
250 |
|
251 | var lastfm = new LastFmNode({
|
252 | api_key: 'abc',
|
253 | secret: 'secret'
|
254 | });
|
255 |
|
256 | var trackStream = lastfm.stream('username');
|
257 |
|
258 | trackStream.on('lastPlayed', function(track) {
|
259 | console.log('Last played: ' + track.name);
|
260 | });
|
261 |
|
262 | trackStream.on('nowPlaying', function(track) {
|
263 | console.log('Now playing: ' + track.name);
|
264 | });
|
265 |
|
266 | trackStream.on('scrobbled', function(track) {
|
267 | console.log('Scrobbled: ' + track.name);
|
268 | });
|
269 |
|
270 | trackStream.on('stoppedPlaying', function(track) {
|
271 | console.log('Stopped playing: ' + track.name);
|
272 | });
|
273 |
|
274 | trackStream.on('error', function(error) {
|
275 | console.log('Error: ' + error.message);
|
276 | });
|
277 |
|
278 | trackStream.start();
|
279 |
|
280 | var session = lastfm.session();
|
281 | session.authorise(token, {
|
282 | authorised: function(session) {
|
283 | lastfm.update('nowplaying', session, { track: track } );
|
284 | lastfm.update('scrobble', session, { track: track, timestamp: 12345678 });
|
285 | }
|
286 | });
|
287 |
|
288 | var request = lastfm.request("artist.getInfo", {
|
289 | artist: "The Mae Shi",
|
290 | handlers: {
|
291 | success: function(data) {
|
292 | console.log("Success: " + data);
|
293 | },
|
294 | error: function(error) {
|
295 | console.log("Error: " + error.message);
|
296 | }
|
297 | }
|
298 | });
|
299 |
|
300 | ## Influences
|
301 |
|
302 | Heavily drawn from technoweenie's twitter-node
|
303 | http://github.com/technoweenie/twitter-node
|