Skip to content

kellnerd/listenbrainz-ts

Repository files navigation

listenbrainz

TypeScript library to access the ListenBrainz API.

You can use it to submit your listening history to your ListenBrainz account with your own apps.

For end users, there is also the elbisaur CLI, which exposes many features of the library to the command line. In addition to showcasing the usage of the API client, it also has advanced features to manage listens.

Features

  • Submits listens and playing now notifications
  • Browses your listening history and allows you to delete listens
  • Handles ListenBrainz authorization and API errors
  • Adheres to rate limits and optionally retries failed requests
  • Provides generic GET and POST methods for (yet) unsupported API endpoints
  • Includes additional parsers to extract listens from text formats
  • Ships with type definitions and inline documentation

As this library only makes use of web standards, it is also compatible with modern browsers (after transpilation to JavaScript).

Usage

In order to submit listens, you have to specify a user token which you can obtain from your ListenBrainz settings page.

The following example instantiates a ListenBrainz client with a token from an environment variable and submits a playing now notification for a track:

import { ListenBrainzClient } from "@kellnerd/listenbrainz";

const client = new ListenBrainzClient({ userToken: Deno.env.get("LB_TOKEN") });
await client.playingNow({ artist_name: "John Doe", track_name: "Love Song" });

Parsers

All listen parsers accept text input and generate Listen objects as their output. These objects can then be used together with the ListenBrainz API and the LB client. None of the parsers performs any filtering of listens, so you have to detect potential duplicates and skipped listens yourself.

The parsers do not include any logic to access files to make them platform independent. You can pass them the content from a HTTP response body or from a file, for example.

The following parsers are available in the listenbrainz/parser/* submodules:

  • JSON: Accepts a JSON-serialized Listen object (as shown by the LB “Inspect listen” dialog) or an array of Listen objects as input.
  • JSONL: Multiple JSON-serialized Listen objects, separated by line breaks (format of listens files from a LB listening history export).
  • MusicBrainz: Accepts a release from the MusicBrainz JSON API and creates listens for the selected tracks.
  • .scrobbler.log: TSV table document which is generated by some portable music players for later submission to Last.fm.
    • Parser currently accepts files with AUDIOSCROBBLER/1.1 header which are generated by players with Rockbox firmware, possibly also by others.
    • Automatically converts timestamps from your local timezone to UTC (as Rockbox players are usually not timezone-aware).
    • Skipped listens are marked with track_metadata.additional_info.skipped = true.
  • Spotify: JSON files from an Extended Streaming History download.
    • Calculates the correct listen (start) timestamp from stream end time and duration.
    • Makes use of the “offline” timestamp to detect extreme outliers where Spotify has only logged a stream as ended when the app was next opened.
    • Skipped listens can be detected by their track_metadata.additional_info attributes skipped, reason_end and a too short duration_ms.