gobble-fm

module
v1.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2025 License: MIT

README

Gobble.fm

Go Reference Tag Go Tests Go Report Card Last Commit

Gobble.fm is a Go (Golang) library for interacting with the Last.fm API.

Why Gobble.fm?

  • Comprehensive API coverage.
  • Package separation between unauthenticated and authenticated API methods.
  • Typed API parameter structs for URL encoding—no need to reference API docs or manually enter parameter names.
  • Typed response struct fields—no need to convert from strings.
  • Helper types and constants for easier API interaction.

Installation

go get github.com/twoscott/gobble-fm

Documentation

Usage

First you need to instatiate the Last.fm API. You can choose the level of abstraction you'd like to use to interact with the API:

import "github.com/twoscott/gobble-fm/api"
// Basic API client with only the API key. No access to auth methods.
fm := api.NewClientKeyOnly("API_KEY")
// Make calls to auth.[getMobileSession|getSession|getToken] methods.
fm := api.NewClient("API_KEY", "SECRET")
import "github.com/twoscott/gobble-fm/session"
// Authenticate API calls on behalf of a user.
fm := session.NewClient("API_KEY", "SECRET")
// Must authenticate a user first. e.g.,
fm.Login("USERNAME", "PASSWORD")
// or
fm.TokenLogin("AUTHORIZED_TOKEN")

Low-level abstractions:

import "github.com/twoscott/gobble-fm/api"
// Provides methods for making API requests such as Get, Post, and Request.
fm := api.New("API_KEY", "SECRET")
import "github.com/twoscott/gobble-fm/session"
// Provides methods for making authenticated API requests.
fm := session.New("API_KEY", "SECRET")
// Must authenticate a user first. e.g.,
// Obtain session key from one of the auth methods.
fm.SetSessionKey("SESSION_KEY")

Simple Example

package main

import (
	"errors"
	"fmt"
	"time"

	"github.com/twoscott/gobble-fm/api"
	"github.com/twoscott/gobble-fm/lastfm"
)

func main() {
	fm := api.NewClientKeyOnly("API_KEY")

	params := lastfm.RecentTracksParams{
		User:  "Username",
		Limit: 5,
		From:  time.Now().Add(-24 * time.Hour),
	}

	res, err := fm.User.RecentTracks(params)
	if err != nil {
		var fmerr *api.LastFMError
		if errors.As(err, &fmerr) {
			switch fmerr.Code {
			case api.ErrInvalidParameters:
				fmt.Println("Invalid parameters")
			case api.ErrOperationFailed:
				fmt.Println("Operation failed")
			default:
				fmt.Println(err)
				// ...
			}
		} else {
			fmt.Println(err)
		}

		return
	}

	for i, t := range res.Tracks {
		fmt.Printf("%d.\t%s by %s\n", i+1, t.Title, t.Artist.Name)

		if t.NowPlaying {
			fmt.Println("\tNow playing...")
		} else {
			ago := time.Since(t.ScrobbledAt.Time()).Truncate(time.Second)
			fmt.Printf("\tScrobbled %s ago\n", ago)
		}

		fmt.Printf("\n\tArt: %s\n", t.Image.OriginalURL())
		fmt.Println()
	}
}

More Examples

Directories

Path Synopsis
Package api provides a client for interacting with the Last.fm API.
Package api provides a client for interacting with the Last.fm API.
examples
add-tags command
multi-scrobble command
recent-tracks command
top-albums command
Package lastfm provides a set of types and constants for working with the Last.fm API.
Package lastfm provides a set of types and constants for working with the Last.fm API.
Package session provides functionality for managing authenticated sessions with the Last.fm API.
Package session provides functionality for managing authenticated sessions with the Last.fm API.
util

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL