sonjek/go-lastfm is a fork of shkh/lastfm-go that hasn't been maintained since the end of 2019.
Golang wrapper for the Last.fm API 2.0
Get the source codes from github:
go get github.com/sonjek/go-lastfm/lastfm
Import the package:
import "github.com/sonjek/go-lastfm/lastfm"
First, create an API instance with your API KEY
and API SECRET
.
api := lastfm.New(ApiKey, ApiSecret)
Note that some API methods require your user's permission, so make sure that your requests are authenticated before calling these methods. See "Authentication" section.
API instances contain the structs which represent API classes, and each struct has methods corresponding to their API methods.
So you can call artist.getTopTracks
for example as following:
// with discarding error
result, _ := api.Artist.GetTopTracks(lastfm.P{"artist": "Avicii"})
for _, track := range result.Tracks {
fmt.Println(track.Name)
}
Methods that fetch some data return their result as a struct named ClassMethod
(e.g. api.User.GetInfo returns its result of type UserGetInfo).
They can be found in class_result.go
.
Please look at the file to see which fields are exported.
You can use lastfm.P
for arguments.
It's just an alias to map[string]interface{}
, but values must be string
, int
, int64
(for unix timestamp) or []string
.
Slice of string, []string, can be used for passing multiple values for a key.
// album.addTags (auth required)
api.Album.AddTags(lastfm.P{
"artist": "Kaene",
"album": "Strangeland",
"tags": []string{"britpop", "alternative rock", "2012"},
})
There are three ways to authenticate your requests, which to choose depends on what kind of application you are making.
- for Mobile Apps
err := api.Login(username, password)
- for Desktop Apps
token, _ := api.GetToken()
authUrl := api.GetAuthTokenUrl(token)
// Send your user to "authUrl"
// Once the user grant permission, then authorize the token.
api.LoginWithToken(token)
- for Web Apps
callback := "https://spam.hum"
authUrl, _ := api.GetAuthRequestUrl(callback)
// Send your user to "authUrl"
// Get the token embeded in the redirected URL, then authorize the token.
api.LoginWithToken(token)
More usage examples can be found in examples directory.
- artist.addTags
- artist.getCorrection
- artist.getInfo
- artist.getSimilar
- artist.getTags
- artist.getTopAlbums
- artist.getTopTags
- artist.getTopTracks
- artist.removeTag
- artist.search
- tag.getInfo
- tag.getSimilar
- tag.getTopAlbums
- tag.getTopArtists
- tag.getTopTags
- tag.getTopTracks
- tag.getWeeklyChartList
- track.addTags
- track.getCorrection
- track.getInfo
- track.getSimilar
- track.getTags
- track.getTopTags
- track.love
- track.removeTag
- track.scrobble
- track.search
- track.unlove
- track.updateNowPlaying
- user.getFriends
- user.getInfo
- user.getLovedTracks
- user.getPersonalTags
- user.getRecentTracks
- user.getTopAlbums
- user.getTopArtists
- user.getTopTags
- user.getTopTracks
- user.getWeeklyAlbumChart
- user.getWeeklyArtistChart
- user.getWeeklyChartList
- user.getWeeklyTrackChart
MIT Licenced. See LICENCE.