UNPKG

4.85 kBMarkdownView Raw
1# lastfm-node
2
3Read 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### RecentTracksStream
21
22 lastfm.stream(username);
23
24Returns: a `RecentTracksStream` instance
25
26Methods:
27
28- *start()*
29
30 Start streaming recent track info.
31
32- *stop()*
33
34 Stop streaming recent track info.
35
36- *on(event, listener)*
37
38 Adds a listener for the specified event.
39
40- *removeListener(event, listener)*
41
42 Removes the listener for the specified event.
43
44Options:
45
46- *autostart*
47
48 Start streaming automatically. Defaults to false.
49
50- *lastPlayed*, *nowPlaying*, *scrobbled*, *stoppedPlaying*, *error*
51
52 Event listeners.
53
54Events:
55
56- *lastPlayed(track)*
57
58 The user's last scrobbled track.
59
60- *nowPlaying(track)*
61
62 Track the user is currently listening to.
63
64- *scrobbled(track)*
65
66 Now playing track has been scrobbled.
67
68- *stoppedPlaying(track)*
69
70 User stopped listening to current track.
71
72- *error(error)*
73
74 Ruh-roh.
75
76### LastFmSession
77
78 lastfm.session([user], [key]);
79
80Returns: a `LastFmSession` instance.
81
82If 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.
83
84Public properties:
85
86- *user*
87
88 The username of the Last.fm user associated with the session.
89
90- *key*
91
92 The session key. Either passed in or generated using authorise().
93
94Methods:
95
96- *authorise(token, [options])*
97
98 Authorises user with Last.fm api. See last.fm documentation.
99 Options:
100 - *authorised* : `function(session)`
101 Listener for *authorised* event. See below.
102
103 - *error* : `function(error)`
104 Listener for `error` event. See below.
105
106- *on(event, handler)*
107
108 Adds a listener for the specified event.
109
110- *removeListener(event, handler)*
111
112 Removes the listener for the specified event.
113
114Events:
115
116- *authorised(session)*
117
118 Authorisation of session was successful.
119 Note: Only emitted after a call to authorise(). Keys supplied in the constructor are assumed to be valid.
120
121- *error(track, error)*
122
123 Ruh-roh.
124
125### LastFmUpdate
126
127 lastfm.update(method, session, options);
128
129Returns a `LastFmUpdate` instance.
130
131Valid methods are 'nowplaying' and 'scrobble'.
132
133An authorised `LastFmSession` instance is required to make a successful update.
134
135Options:
136
137- *track*
138
139 Track for nowplaying and scrobble requests. Uses same format as returned by `RecentTracksStream` events.
140
141- *timestamp*
142
143 Required for scrobble requests. Timestamp is in unix time (seconds since 01-01-1970 and is in UTC time).
144
145- *success*
146
147 Listener for `success` event.
148
149- *error*
150
151 Listener for `error` event.
152
153Events:
154
155- *success(track)*
156
157 Update request was successful.
158
159- *error(track, error)*
160
161 Ruh-roh.
162
163### LastFmInfo
164
165 lastfm.info(itemtype, [options]);
166
167Returns: a `LastFmInfo` instance.
168
169Gets extended info about specified item.
170
171Public properties:
172
173- *itemtype*
174
175 Any Last.fm item with a getInfo method. eg user, track, artist, etc.
176
177Options:
178
179- *success*
180
181 Listener for `success` event.
182
183- *error*
184
185 Listener for `error` event.
186
187- *various*
188
189 Params as specified in Last.fm API, eg user: "username"
190
191Special cases:
192
193When requesting track info the `track` param can be either the track name or a track object as returned by `RecentTracksStream`.
194
195
196## Example
197
198 var LastFmNode = require('lastfm').LastFmNode;
199
200 var lastfm = new LastFmNode({
201 api_key: 'abc',
202 secret: 'secret'
203 });
204
205 var trackStream = lastfm.stream('username');
206
207 trackStream.on('lastPlayed', function(track) {
208 console.log('Last played: ' + track.name);
209 });
210
211 trackStream.on('nowPlaying', function(track) {
212 console.log('Now playing: ' + track.name);
213 });
214
215 trackStream.on('scrobbled', function(track) {
216 console.log('Scrobbled: ' + track.name);
217 });
218
219 trackStream.on('stoppedPlaying', function(track) {
220 console.log('Stopped playing: ' + track.name);
221 });
222
223 trackStream.on('error', function(error) {
224 console.log('Error: ' + error.message);
225 });
226
227 trackStream.start();
228
229 var session = lastfm.session();
230 session.authorise(token, {
231 authorised: function(session) {
232 lastfm.update('nowplaying', session, { track: track } );
233 lastfm.update('scrobble', session, { track: track, timestamp: 12345678 });
234 }
235 });
236
237## Influences
238
239Heavily drawn from technoweenie's twitter-node
240http://github.com/technoweenie/twitter-node