lastfm

package module
v0.0.0-...-d831b80 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2018 License: MIT Imports: 11 Imported by: 2

README

Go Report Card GoDoc

lastfm

Go package to interface with last.fm

Usage

Instantiate the API object with your API key and secret.

lfm := lastfm.New("yourkey", "yoursecret")
token, err := lfm.Authenticate("yourusername", "yourpassword")
if err != nil {
	lfm.SetSessionKey(token)
}

Search for an album (requiring a MusicBrainz ID) and scrobble the first result.

albums, err := lfm.AlbumSearch("slow riot for new zero kanada", true)
if err != nil {
	err = albums[0].Scrobble()
}

Obviously it makes sense to not just blindly trust that the first result is what you want. See https://github.com/Sigafoos/scrobble for a CLI utilizing this package.

Documentation

Overview

Package lastfm provides a way to communicate with last.fm's api in go.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Version is the SemVer representation of the module's state.
	Version = "0.6.1"
	// BaseURL is the root of all API calls.
	BaseURL = "https://ws.audioscrobbler.com/2.0/"
)
View Source
var SortOrder = []string{"0", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4", "5", "6", "7", "8", "9"}

SortOrder is the order array keys should be in to generate a valid signature. The "signature" for authenticated calls requires you to put the parameters in alphabetical order. This includes the arrays in track.scrobble, which have to be sorted according to the ASCII table, not as though the numbers were strings. By this method, 10 < 19 < 1. Much time was spent trying to make `sort.Sort` work with this until I decided this was perfectly fine.

Functions

This section is empty.

Types

type API

type API struct {
	// contains filtered or unexported fields
}

An API object provides the mechanism to authenticate and communicate with last.fm's system.

func New

func New(key, secret string) *API

New initializes an API object with the provided api key and shared secret. The user's session key is not required as you may need to contact the API in order to obtain one.

func (*API) AlbumSearch

func (a *API) AlbumSearch(album string, requireMBID bool) ([]Album, error)

AlbumSearch contacts last.fm and returns a list of albums that match the search query.

func (*API) Authenticate

func (a *API) Authenticate(username, password string) (string, error)

Authenticate accepts a username and password and uses the mobile authentication scheme to generate a session token. Using the mobile scheme avoids the need to handle oauth callbacks.

func (*API) Authenticated

func (a *API) Authenticated() bool

Authenticated returns whether a session key has been set.

func (*API) SetSessionKey

func (a *API) SetSessionKey(key string)

SetSessionKey adds the user's session key/token to the object, allowing for authenticated API calls.

func (*API) SetVerbose

func (a *API) SetVerbose(verbose bool)

SetVerbose sets whether more information (such as curl versions of the calls).

type Album

type Album struct {
	API           *API
	Name          string    `json:"name"`
	Artist        string    `json:"artist"`
	URL           string    `json:"url"`
	Images        []Image   `json:"image"`
	Streamable    string    `json:"streamable"`
	MusicBrainzID string    `json:"mbid"`
	Listeners     string    `json:"lisners"`
	Playcount     string    `json:"playcount"`
	TrackList     TrackList `json:"tracks"`
}

An Album represents an album, and implements the Scrobbler interface.

func (*Album) GetInfo

func (a *Album) GetInfo() error

GetInfo uses the information in the Album to retrieve more information (including the track list) from last.fm. It uses the MusicBrainz ID, if available, as that is more authoritative.

func (*Album) Scrobble

func (a *Album) Scrobble() error

Scrobble send a request to last.fm to scrobble all tracks on the album. It sends one API call using the batch format.

type Artist

type Artist struct {
	Name          string `json:"name"`
	URL           string `json:"url"`
	MusicBrainzID string `json:"mbid"`
}

An Artist represents the person or group responsible for creating a track or album.

type AttrList

type AttrList struct {
	TrackNumber int `json:"rank,string"`
}

An AttrList contains attributes about a track.

type Image

type Image struct {
	URL  string `json:"#text"`
	Size string `json:"small"`
}

An Image represents the cover art of an album.

type Scrobbler

type Scrobbler interface {
	Scrobble() error
}

A Scrobbler is a type that can scrobble itself, or record the track(s) it contains to last.fm.

type Track

type Track struct {
	API      *API
	Name     string   `json:"name"`
	URL      string   `json:"url"`
	Duration int      `json:"duration,string"`
	AttrList AttrList `json:"@attr"`
	Artist   Artist   `json:"artist"`
	Album    string
}

A Track represents a song and implements the Scrobbler interface.

func (*Track) Scrobble

func (t *Track) Scrobble() error

Scrobble records the track on the user's last.fm account.

type TrackList

type TrackList struct {
	Tracks []Track `json:"track"`
}

A TrackList contains a slice of Tracks.

Jump to

Keyboard shortcuts

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