ListenBrainz

A Ruby wrapper to the ListenBrainz API.

Installation

Install the gem and add to the application’s Gemfile by executing:

bundle add listenbrainz

If bundler is not being used to manage dependencies, install the gem by executing:

gem install listenbrainz

Usage

Configuration

The ListenBrainz gem can be configured either through environment variables or programatically.

Environment variables

Name Default Description
‘LISTENBRAINZ_API_ENDPOINT` api.listenbrainz.org` URL to the ListenBrainz API
‘LISTENBRAINZ_USER_TOKEN` Required Used for authentication. You can find your user token on [ListenBrainz’s settings page].

Programatically

ListenBrainz.configure do |config|
  config.endpoint = '<CUSTOM_LISTENBRAINZ_URL>'
  config.user_token = '<USER_TOKEN>'
end

Retrieving listens

# Retrieve listens for the current user.
ListenBrainz::Listen.get
ListenBrainz::Listen.get(count: 10)

# Retrieve listens for a different user
ListenBrainz::Listen.get('Bobby')
ListenBrainz::Listen.get('Bobby', count: 10)

# Retrieve "Now playing" listens for the current user
ListenBrainz::Listen.now_playing

# Retrieve "Now playing" listens for a different user
ListenBrainz::Listen.now_playing('Bobby')

Submitting listens

listen = ListenBrainz::Listen.new(
  listened_at: Time.now,
  track_metadata: {
    artist_name: 'Joan Jett and the Blackhearts',
    release_name: 'Sinner',
    track_name: 'Naked',
    additional_info: {
      listening_from: 'Plex',
      media_player: 'Plex',
      track_mbid: '2b66948e-fd60-3312-a8cb-22fdecbc98b9'
    }
  }
)

# Submit a single listen
listen.submit
listen.submit(mode: :single)

# Submit a "Now playing" listen
listen.submit(mode: :playing_now)

# Submit a batch of listens
ListenBrainz::Listen.sumbit([listen])

Retrieving artists

# Retrieve top artists for the current user
ListenBrainz::Artist.get
ListenBrainz::Artist.get(count: 5, offset: 10, range: :this_month)

# Retrieve top artists for a different user
ListenBrainz::Artist.get('Bobby')
ListenBrainz::Artist.get('Bobby', count: 5, offset: 10, range: :this_month)

# Retrieve site-wide top artists
ListenBrainz::Artist.sitewide
ListenBrainz::Artist.sitewide(count: 5, offset: 10, range: :this_month)

# Retrieve artist map for the current user
ListenBrainz::ArtistMap.get
ListenBrainz::ArtistMap.get(range: :this_week)

# Retrieve artist map for a different user
ListenBrainz::ArtistMap.get('Bobby')
ListenBrainz::ArtistMap.get('Bobby', range: :this_week)

# Retrieve site-wide artist map
ListenBrainz::ArtistMap.sitewide
ListenBrainz::ArtistMap.sitewide(range: :all_time)

Retrieving release groups

# Retrieve top release groups for the current user
ListenBrainz::ReleaseGroup.get
ListenBrainz::ReleaseGroup.get(count: 5, offset: 10, range: :this_month)

# Retrieve top release groups for a different user
ListenBrainz::ReleaseGroup.get('Bobby')
ListenBrainz::ReleaseGroup.get('Bobby', count: 5, offset: 10, range: :this_month)

# Retrieve site-wide top release groups
ListenBrainz::ReleaseGroup.sitewide
ListenBrainz::ReleaseGroup.sitewide(count: 5, offset: 10, range: :this_year)

Retrieving recordings

# Retrieve top recordings for the current user
ListenBrainz::Recording.get
ListenBrainz::Recording.get(count: 5, offset: 10, range: :all_time)

# Retrieve top recordings for a different user
ListenBrainz::Recording.get('Bobby')
ListenBrainz::Recording.get('Bobby', count: 5, offset: 10, range: :all_time)

# Retrieve site-wide top recordings
ListenBrainz::Recording.sitewide
ListenBrainz::Recording.sitewide(count: 5, offset: 10, range: :all_time)

Documentation

A complete YARD documentation is available below.

listenbrainz.richarddegenne.fr

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

Visual Studio Code workspace

The listenbrainz.code-workspace file contains recommended settings and extensions to work with Visual Studio Code. Open the workspace by running code listenbrainz.code-workspace.

Hot test runs

The development dependencies include Guard to run tests and generate documentation whenever you edit a file.

Run bundle exec guard to start watching.

Contributing

Bug reports and pull requests are welcome on GitLab at gitlab.com/Richard-Degenne/listenbrainz. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the ListenBrainz project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.