I'm trying to use the last.fm API in javascript but I can't get it working. I found this GitHub reference which I thought would be useful: https://github.com/fxb/javascript-last.fm-api

var cache = new LastFMCache();

but this ^ causes an error "ReferenceError: LastFMCache is not defined." I've tried putting the files from https://github.com/fxb/javascript-last.fm-api in the same directory as the file I'm running. I'm running it with node.js - I don't know if that would be a problem. Running the file with node.js before was not a problem though. Any idea how I can remedy this problem? I can't tell if I'm missing some code or my files are in the wrong directory. Thanks for your help!

有帮助吗?

解决方案

You are using client-side JavaScript library.

If you will browse the NPM, there are plenty of Last.fm modules, the most popular is simple-lastfm

Here is the example taken from the docs:

var Lastfm = require('simple-lastfm');

var lastfm = new Lastfm({
    api_key: 'xxx',
    api_secret: 'xxx',
    username: 'xxx',
    password: 'xxx'
});

lastfm.getSessionKey(function(result) {
    console.log("session key = " + result.session_key);
    if(result.success) {
        lastfm.scrobbleNowPlayingTrack({
            artist: 'Ratatat',
            track: 'Seventeen Years',
            callback: function(result) {
                console.log("in callback, finished: ", result);
            }
        });
    } else {
        console.log("Error: " + result.error);
    }
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top