Skip to content

twoscott/gobble-fm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gobble.fm

Go Reference Go Version 1.23+ 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

About

A Go (Golang) library for the Last.fm API

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •  

Languages