npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lastfm-node-client

v2.2.0

Published

JavaScript library for interfacing with the Last.fm API

Downloads

780

Readme

Last.Fm Node Client

npm Build Status Coverage Status

This is a Node.js JavaScript library for interfacing with the Last.fm API.

  • Consistent: Providing you a uniform set of abstractions that allows you to use the interface predictably and reliably.
  • Versatile: Adapting it's communication with the API, complying with it's specifications without you needing to adapt your code.
  • Low level: Delivering you the response as it comes directly from the API, using it's native format parameter, to deliver a consistent JSON response.

See the Last.Fm API web page for information about the Last.Fm API, including details on how to register an account and get your API key, shared secret, and session key.

Installation

$ npm install lastfm-node-client

Node.js 8.10.0 or later is required.

Usage

First, you must instantiate the LastFm Class with arguments containing the details of your API account. apiKey is required, however since many endpoints of the API do not require authentication, secret and sessionKey are optional.

const LastFm = require("lastfm-node-client");

const lastFm = new LastFm("API_KEY", "SECRET", "SESSION_KEY");

Making Requests

The Last.fm API is structured into packages and methods, accessed as Package.method. The LastFm Class contains directly corresponding methods for each package method, written as lastFm.packageMethod(). For example, endpoint User.getRecentTracks is accessed as lastFm.userGetRecentTracks().

Parameters can be passed to the API through the params argument as an object that will be sent directly with the request, either as a query for a GET request, or a body for a POST request. The property names will not be transformed or abstracted, and so they must match the endpoint parameters exactly.

lastFm.userGetRecentTracks({
    user: "USER"
});

Notes

  • lastFm.authGetToken() and lastFm.tagGetTopTags() do not accept a params argument, as endpoints Auth.getToken and Tag.getTopTags do not require additional parameters.

  • lastFm.userGetInfo() params.user and params argument are optional and will default to the authenticated user in accordance with endpoint User.getInfo.

Capturing Responses and Handling Errors

Every method returns a promise of the pending request by default. To access the response, you can chain .then() to the method, or use await. Errors thrown while making a request or errors returned by the API will reject the promise.

Chaining .then():

lastFm.userGetRecentTracks({
    user: "USER"
})
.then(data => {
    console.log(data);
};

Using await:

const data = await lastFm.userGetRecentTracks({
    user: "USER"
});

console.log(data);

An optional callback can be passed as the last argument. It is invoked with conventional (err, data) arguments; err being any errors thrown while making a request or errors returned by the API, data containing the JSON response of the API upon success.

lastFm.userGetRecentTracks({
    user: "USER"
},
(err, data)) => {
    console.log(data);
});

When callback is passed, methods do not return a promise, instead return the LastFm instance the method was called on. This allows you to chain requests.

Utility Methods

These methods do not correspond to an exact API endpoint, but are abstractions of the already provided methods to provide easier usage.

lastFm.trackScrobbleMany

This method takes an array of objects that match the params parameter accepted by lastFm.trackScrobble. It allows for multiple tracks to be submitted to the API in one request.

lastFm.trackScrobbleMany([
    {
        artist: "ARTIST",
        album: "ALBUM",
        track: "TRACK",
        timestamp: "TIMESTAMP"
    },
    {
        artist: "ARTIST",
        album: "ALBUM",
        track: "TRACK",
        timestamp: "TIMESTAMP"
    }
]);