### Install go-lastfm Package Source: https://github.com/sonjek/go-lastfm/blob/master/README.md This command installs the go-lastfm package, which is a Go wrapper for the Last.fm API. It fetches the source code from GitHub and makes it available for import in your Go projects. ```bash go get github.com/sonjek/go-lastfm/lastfm ``` -------------------------------- ### Complete Scrobbling Example using Go Source: https://context7.com/sonjek/go-lastfm/llms.txt Demonstrates a full scrobbling workflow with go-lastfm, including authentication, updating the 'now playing' status, and scrobbling a track. This example requires API credentials, user login details, and track information, with a delay to simulate listening time. ```go package main import ( "fmt" "time" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") // Authenticate err := api.Login("username", "password") if err != nil { fmt.Println("Login failed:", err) return } fmt.Println("Logged in successfully!") // Track info artist := "Pink Floyd" track := "Comfortably Numb" album := "The Wall" // Update Now Playing _, err = api.Track.UpdateNowPlaying(lastfm.P{ "artist": artist, "track": track, "album": album, }) if err != nil { fmt.Println("Now Playing update failed:", err) return } fmt.Printf("Now Playing: %s - %s\n", artist, track) // Wait for minimum scrobble time (typically 30 seconds or half the track) startTime := time.Now().Unix() time.Sleep(35 * time.Second) // Scrobble the track result, err := api.Track.Scrobble(lastfm.P{ "artist": artist, "track": track, "album": album, "timestamp": startTime, }) if err != nil { fmt.Println("Scrobble failed:", err) return } fmt.Printf("Scrobbled! Accepted: %s, Ignored: %s\n", result.Accepted, result.Ignored) } ``` -------------------------------- ### Track.UpdateNowPlaying and Track.Scrobble Source: https://context7.com/sonjek/go-lastfm/llms.txt This example demonstrates how to update the 'Now Playing' status for a track and then scrobble it to Last.fm. It includes authentication, setting track details, and handling the scrobbling process with a timestamp. ```APIDOC ## POST /track.updateNowPlaying and POST /track.scrobble ### Description Updates the 'Now Playing' status for a track and then scrobbles the track to Last.fm. This is useful for music players to report what the user is currently listening to and to log completed listening sessions. ### Method POST ### Endpoint /track.updateNowPlaying /track.scrobble ### Parameters #### Request Body (for both endpoints) - **artist** (string) - Required - The artist of the track. - **track** (string) - Required - The track name. - **album** (string) - Optional - The album name. - **timestamp** (integer) - Required for scrobble - The UNIX timestamp when the track finished playing. ### Request Example (UpdateNowPlaying) ```json { "artist": "Pink Floyd", "track": "Comfortably Numb", "album": "The Wall" } ``` ### Request Example (Scrobble) ```json { "artist": "Pink Floyd", "track": "Comfortably Numb", "album": "The Wall", "timestamp": 1678886400 } ``` ### Response #### Success Response (200) for UpdateNowPlaying (No specific fields, success is indicated by no error) #### Success Response (200) for Scrobble - **Accepted** (integer) - Number of tracks accepted. - **Ignored** (integer) - Number of tracks ignored. #### Response Example (Scrobble) ```json { "Accepted": 1, "Ignored": 0 } ``` ``` -------------------------------- ### Artist.GetInfo Error Handling Source: https://context7.com/sonjek/go-lastfm/llms.txt Demonstrates how to handle Last.fm API errors gracefully. This example shows how to check for specific `lastfm.LastfmError` types and extract detailed error information. ```APIDOC ## GET /artist.getInfo (Error Handling Example) ### Description Fetches information about an artist and demonstrates how to handle potential errors returned by the Last.fm API, specifically identifying `lastfm.LastfmError`. ### Method GET ### Endpoint /artist.getInfo ### Parameters #### Query Parameters - **artist** (string) - Required - The name of the artist. ### Request Example ```json { "artist": "NonExistentArtist12345" } ``` ### Response #### Error Response - **Code** (integer) - The error code from Last.fm. - **Message** (string) - A description of the error. - **Caller** (string) - The API key used for the request. #### Response Example (Error) ```json { "Code": 6, "Message": "Artist not found", "Caller": "YOUR_API_KEY" } ``` ``` -------------------------------- ### Get Similar Artists with Go Source: https://context7.com/sonjek/go-lastfm/llms.txt Finds artists that are similar to a specified artist. Requires API key and secret. Outputs the input artist's name and a list of similar artists with their match percentage. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Artist.GetSimilar(lastfm.P{ "artist": "Coldplay", "limit": 10, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Artists similar to %s:\n", result.Artist) for _, similar := range result.Similars { fmt.Printf(" - %s (match: %s%%)\n", similar.Name, similar.Match) } } ``` -------------------------------- ### Get Album Info with Go Last.fm Client Source: https://context7.com/sonjek/go-lastfm/llms.txt Retrieves detailed information about a specific album, including its name, artist, URL, listener count, and tracks. Requires an API key and secret. Outputs album details and track information. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Album.GetInfo(lastfm.P{ "artist": "Radiohead", "album": "OK Computer", "lang": "en", }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Album: %s by %s\n", result.Name, result.Artist) fmt.Printf("URL: %s\n", result.Url) fmt.Printf("Listeners: %s\n", result.Listeners) fmt.Println("\nTracks:") for _, track := range result.Tracks { fmt.Printf(" %s. %s (%ss)\n", track.Rank, track.Name, track.Duration) } if result.Wiki.Summary != "" { fmt.Printf("\nWiki: %s\n", result.Wiki.Summary) } } ``` -------------------------------- ### Get User Top Albums - Go Source: https://context7.com/sonjek/go-lastfm/llms.txt Fetches a user's top albums for a given period. It requires API credentials, username, period, and a limit. The output includes album rank, name, artist, and play count. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.User.GetTopAlbums(lastfm.P{ "user": "RJ", "period": "6month", "limit": 10, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Top albums for %s:\n", result.User) for _, album := range result.Albums { fmt.Printf(" #%s: %s by %s (plays: %s)\n", album.Rank, album.Name, album.Artist.Name, album.PlayCount) } } ``` -------------------------------- ### Create Last.fm API Instance and Get Artist Info (Go) Source: https://context7.com/sonjek/go-lastfm/llms.txt Initializes a new Last.fm API client with your API key and secret. It demonstrates making an unauthenticated request to retrieve artist information, including name and listener count. Optionally, a custom user agent can be set. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") // Set custom user agent (optional) api.SetUserAgent("MyMusicApp/1.0") // Get artist info without authentication result, err := api.Artist.GetInfo(lastfm.P{"artist": "Radiohead"}) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Artist: %s, Listeners: %s\n", result.Name, result.Stats.Listeners) } ``` -------------------------------- ### Get Artist Info with Go Source: https://context7.com/sonjek/go-lastfm/llms.txt Retrieves detailed information about an artist, including biography, statistics, and similar artists. Requires API key and secret. Outputs artist name, URL, listener counts, play counts, bio summary, similar artists, and tags. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Artist.GetInfo(lastfm.P{ "artist": "Daft Punk", "autocorrect": 1, "lang": "en", }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Artist: %s\n", result.Name) fmt.Printf("URL: %s\n", result.Url) fmt.Printf("Listeners: %s\n", result.Stats.Listeners) fmt.Printf("Play Count: %s\n", result.Stats.Plays) fmt.Printf("Bio: %s\n", result.Bio.Summary) fmt.Println("\nSimilar Artists:") for _, similar := range result.Similars { fmt.Printf(" - %s (%s)\n", similar.Name, similar.Url) } fmt.Println("\nTags:") for _, tag := range result.Tags { fmt.Printf(" - %s\n", tag.Name) } } ``` -------------------------------- ### Get Similar Tracks with Go Last.fm Client Source: https://context7.com/sonjek/go-lastfm/llms.txt Finds tracks that are similar to a given track based on artist and track name. It returns a list of similar tracks with their match score. Requires an API key and secret. Outputs the original track details and a list of similar tracks. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Track.GetSimilar(lastfm.P{ "artist": "Nirvana", "track": "Smells Like Teen Spirit", "limit": 10, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Tracks similar to '%s' by %s:\n", result.Track, result.Artist) for _, track := range result.Tracks { fmt.Printf(" - %s by %s (match: %s)\n", track.Name, track.Artist.Name, track.Match) } } ``` -------------------------------- ### Get Top Tags from Last.fm Chart (Go) Source: https://context7.com/sonjek/go-lastfm/llms.txt Fetches the top tags from the Last.fm charts. It requires Last.fm API credentials and returns a list of tags with their taggings and reach counts. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Chart.GetTopTags(lastfm.P{ "limit": 20, }) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Top Tags on Last.fm:") for i, tag := range result.Tags { fmt.Printf(" %d. %s (taggings: %s, reach: %s)\n", i+1, tag.Name, tag.Taggings, tag.Reach) } } ``` -------------------------------- ### Get Top Tracks from Last.fm Chart (Go) Source: https://context7.com/sonjek/go-lastfm/llms.txt Retrieves the top tracks from the Last.fm charts. Requires API key and secret. Outputs a list of tracks with their artist and play count. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Chart.GetTopTracks(lastfm.P{ "limit": 10, }) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Top Tracks on Last.fm:") for i, track := range result.Tracks { fmt.Printf(" %d. %s by %s (plays: %s)\n", i+1, track.Name, track.Artist.Name, track.PlayCount) } } ``` -------------------------------- ### Get Artist Correction with Go Source: https://context7.com/sonjek/go-lastfm/llms.txt Provides a corrected artist name if the input name is misspelled. Requires API key and secret. Outputs the corrected artist name and its MusicBrainz ID. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Artist.GetCorrection(lastfm.P{ "artist": "Guns and Roses", }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Corrected name: %s\n", result.Correction.Artist.Name) fmt.Printf("MusicBrainz ID: %s\n", result.Correction.Artist.Mbid) } ``` -------------------------------- ### Get Artists from User Library using Go Source: https://context7.com/sonjek/go-lastfm/llms.txt Fetches a list of artists from a specified user's library using the go-lastfm library. It requires API credentials and user information as input, returning artist details and play counts. Error handling is included for API requests. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Library.GetArtists(lastfm.P{ "user": "RJ", "limit": 10, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Artists in %s's library:\n", result.User) for _, artist := range result.Artists { fmt.Printf(" - %s (plays: %s)\n", artist.Name, artist.PlayCount) } } ``` -------------------------------- ### Get Artist Top Albums with Go Source: https://context7.com/sonjek/go-lastfm/llms.txt Retrieves the most popular albums for an artist. Requires API key and secret. Outputs the artist's name and a ranked list of their top albums with play counts. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Artist.GetTopAlbums(lastfm.P{ "artist": "Pink Floyd", "limit": 5, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Top albums for %s:\n", result.Artist) for _, album := range result.Albums { fmt.Printf(" #%s: %s (plays: %s)\n", album.Rank, album.Name, album.PlayCount) } } ``` -------------------------------- ### Get Track Info with Go Last.fm Client Source: https://context7.com/sonjek/go-lastfm/llms.txt Retrieves detailed information about a specific track, including its name, artist, album, duration, play count, and URL. Supports autocorrection for track names. Requires an API key and secret. Outputs track details and top tags. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Track.GetInfo(lastfm.P{ "artist": "Queen", "track": "Bohemian Rhapsody", "autocorrect": 1, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Track: %s\n", result.Name) fmt.Printf("Artist: %s\n", result.Artist.Name) fmt.Printf("Album: %s\n", result.Album.Title) fmt.Printf("Duration: %s ms\n", result.Duration) fmt.Printf("Play Count: %s\n", result.PlayCount) fmt.Printf("URL: %s\n", result.Url) fmt.Println("\nTop Tags:") for _, tag := range result.TopTags { fmt.Printf(" - %s\n", tag.Name) } } ``` -------------------------------- ### Get Tag Information (Go) Source: https://context7.com/sonjek/go-lastfm/llms.txt Retrieves detailed information about a specific tag. This function requires API credentials and accepts the tag name and language as parameters. It outputs the tag's name, total taggings, reach, and a summary from its wiki. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Tag.GetInfo(lastfm.P{ "tag": "electronic", "lang": "en", }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Tag: %s\n", result.Name) fmt.Printf("Total: %s\n", result.Total) fmt.Printf("Reach: %s\n", result.Reach) fmt.Printf("Wiki: %s\n", result.Wiki.Summary) } ``` -------------------------------- ### Get Top Artists for a Tag (Go) Source: https://context7.com/sonjek/go-lastfm/llms.txt Fetches the top artists associated with a given tag. This function requires API credentials and parameters for the tag name and limit. It returns a list of artist names. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Tag.GetTopArtists(lastfm.P{ "tag": "indie", "limit": 10, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Top 'indie' artists:\n") for i, artist := range result.Artists { fmt.Printf(" %d. %s\n", i+1, artist.Name) } } ``` -------------------------------- ### Get Top Artists by Country (Go) Source: https://context7.com/sonjek/go-lastfm/llms.txt Retrieves the top artists for a specified country. This function requires API credentials and accepts country and limit parameters. It outputs a list of artists with their listener counts. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Geo.GetTopArtists(lastfm.P{ "country": "Germany", "limit": 10, }) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Top Artists in Germany:") for i, artist := range result.Artists { fmt.Printf(" %d. %s (listeners: %s)\n", i+1, artist.Name, artist.Listeners) } } ``` -------------------------------- ### Get Artist Top Tracks with Go Source: https://context7.com/sonjek/go-lastfm/llms.txt Fetches the top tracks for a given artist, supporting pagination. Requires API key and secret. Outputs the artist's name, current page, total pages, and a list of tracks with their play counts and durations. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Artist.GetTopTracks(lastfm.P{ "artist": "Avicii", "limit": 10, "page": 1, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Top tracks for %s (Page %d of %d):\n", result.Artist, result.Page, result.TotalPages) for i, track := range result.Tracks { fmt.Printf("%d. %s (plays: %s, duration: %ss)\n", i+1, track.Name, track.PlayCount, track.Duration) } } ``` -------------------------------- ### Get Top Tracks by Country (Go) Source: https://context7.com/sonjek/go-lastfm/llms.txt Fetches the top tracks for a given country. This function requires API credentials and parameters for country and limit. It returns a list of tracks, including artist name and listener count. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Geo.GetTopTracks(lastfm.P{ "country": "United Kingdom", "limit": 10, }) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Top Tracks in United Kingdom:") for i, track := range result.Tracks { fmt.Printf(" %d. %s by %s (listeners: %s)\n", i+1, track.Name, track.Artist.Name, track.Listeners) } } ``` -------------------------------- ### Get Top Albums for a Tag (Go) Source: https://context7.com/sonjek/go-lastfm/llms.txt Fetches the top albums associated with a given tag. This function requires API credentials and parameters for the tag name and limit. It returns a list of albums, including their artist names. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Tag.GetTopAlbums(lastfm.P{ "tag": "psychedelic", "limit": 10, }) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Top 'psychedelic' albums:") for i, album := range result.Albums { fmt.Printf(" %d. %s by %s\n", i+1, album.Name, album.Artist.Name) } } ``` -------------------------------- ### Get User Top Tracks - Go Source: https://context7.com/sonjek/go-lastfm/llms.txt Retrieves a user's top tracks for a specified period. Requires API key, secret, username, period, and limit. Returns a list of tracks with their rank, name, artist, and play count. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.User.GetTopTracks(lastfm.P{ "user": "RJ", "period": "1month", "limit": 10, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Top tracks for %s:\n", result.User) for _, track := range result.Tracks { fmt.Printf(" #%s: %s by %s (plays: %s)\n", track.Rank, track.Name, track.Artist.Name, track.PlayCount) } } ``` -------------------------------- ### Get User Friends List - Go Source: https://context7.com/sonjek/go-lastfm/llms.txt Fetches a user's friends list. This function requires API credentials, username, limit, and an option to include recent tracks. The output includes the total number of friends and their names and play counts. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.User.GetFriends(lastfm.P{ "user": "RJ", "limit": 10, "recenttracks": 1, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Friends of %s (Total: %d):\n", result.For, result.Total) for _, friend := range result.Friends { fmt.Printf(" - %s (plays: %s)\n", friend.Name, friend.PlayCount) } } ``` -------------------------------- ### Get Last.fm User Information (Go) Source: https://context7.com/sonjek/go-lastfm/llms.txt Retrieves detailed information about a Last.fm user, including their real name, country, play count, registration date, and profile URL. This function requires the username as input and returns a structured object containing the user's profile data. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.User.GetInfo(lastfm.P{ "user": "RJ", }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("User: %s\n", result.Name) fmt.Printf("Real Name: %s\n", result.RealName) fmt.Printf("Country: %s\n", result.Country) fmt.Printf("Play Count: %s\n", result.PlayCount) fmt.Printf("Registered: %s\n", result.Registered.Time) fmt.Printf("Profile URL: %s\n", result.Url) } ``` -------------------------------- ### Get Top Tracks for a Tag (Go) Source: https://context7.com/sonjek/go-lastfm/llms.txt Retrieves the top tracks associated with a specific tag. This function requires API credentials and parameters for the tag name and limit. It outputs a list of tracks, including their artist names. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Tag.GetTopTracks(lastfm.P{ "tag": "rock", "limit": 10, }) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Top 'rock' tracks:") for i, track := range result.Tracks { fmt.Printf(" %d. %s by %s\n", i+1, track.Name, track.Artist.Name) } } ``` -------------------------------- ### Get User Weekly Chart List - Go Source: https://context7.com/sonjek/go-lastfm/llms.txt Retrieves a list of available weekly chart periods for a user. It requires the API key, secret, and username. The function returns a list of chart periods, each with a 'from' and 'to' date. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.User.GetWeeklyChartList(lastfm.P{ "user": "RJ", }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Weekly chart periods for %s:\n", result.User) // Show last 5 chart periods charts := result.Charts start := len(charts) - 5 if start < 0 { start = 0 } for _, chart := range charts[start:] { fmt.Printf(" From: %s, To: %s\n", chart.From, chart.To) } } ``` -------------------------------- ### Get User Weekly Track Chart - Go Source: https://context7.com/sonjek/go-lastfm/llms.txt Fetches a user's weekly track chart, optionally specifying 'from' and 'to' timestamps. Requires API key, secret, and username. The output includes the user, chart period, and a list of top tracks with their rank, name, artist, and play count. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.User.GetWeeklyTrackChart(lastfm.P{ "user": "RJ", // Optionally specify 'from' and 'to' timestamps }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Weekly track chart for %s (from %s to %s):\n", result.User, result.From, result.To) for _, track := range result.Tracks[:10] { // Show first 10 fmt.Printf(" #%s: %s by %s (plays: %s)\n", track.Rank, track.Name, track.Artist.Name, track.PlayCount) } } ``` -------------------------------- ### Get User's Top Artists on Last.fm (Go) Source: https://context7.com/sonjek/go-lastfm/llms.txt Retrieves a list of a Last.fm user's top artists over a specified period (e.g., overall, 7day, 1month, 3month, 6month, 12month). This function requires the username, period, and an optional limit. It returns the user's name, the type of period, and a ranked list of artists with their play counts. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") // period can be: overall, 7day, 1month, 3month, 6month, 12month result, err := api.User.GetTopArtists(lastfm.P{ "user": "RJ", "period": "3month", "limit": 10, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Top artists for %s (%s):\n", result.User, result.Type) for _, artist := range result.Artists { fmt.Printf(" #%s: %s (plays: %s)\n", artist.Rank, artist.Name, artist.PlayCount) } } ``` -------------------------------- ### Get User Loved Tracks - Go Source: https://context7.com/sonjek/go-lastfm/llms.txt Retrieves a list of a user's loved tracks. This function requires the API key, secret, username, and a limit for the number of tracks. It returns the total count of loved tracks and a list of track names and artists. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.User.GetLovedTracks(lastfm.P{ "user": "RJ", "limit": 10, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Loved tracks for %s (Total: %d):\n", result.User, result.Total) for _, track := range result.Tracks { fmt.Printf(" - %s by %s\n", track.Name, track.Artist.Name) } } ``` -------------------------------- ### Get User's Recent Tracks on Last.fm (Go) Source: https://context7.com/sonjek/go-lastfm/llms.txt Fetches a list of a Last.fm user's most recently played tracks. This function takes the username and an optional limit for the number of tracks to retrieve. It returns the total number of tracks and a list of track details, including artist and whether the track is currently playing. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.User.GetRecentTracks(lastfm.P{ "user": "RJ", "limit": 10, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Recent tracks for %s (Total: %d):\n", result.User, result.Total) for _, track := range result.Tracks { nowPlaying := "" if track.NowPlaying == "true" { nowPlaying = " [NOW PLAYING]" } fmt.Printf(" - %s by %s%s\n", track.Name, track.Artist.Name, nowPlaying) } } ``` -------------------------------- ### Get Global Top Artists - Go Source: https://context7.com/sonjek/go-lastfm/llms.txt Retrieves the top artists on Last.fm charts. This function requires API key, secret, and optional parameters like limit and page. It returns the current page number, total pages, and a list of artists with their names, listener counts, and play counts. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Chart.GetTopArtists(lastfm.P{ "limit": 10, "page": 1, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Top Artists on Last.fm (Page %d of %d):\n", result.Page, result.TotalPages) for i, artist := range result.Artists { fmt.Printf(" %d. %s (listeners: %s, plays: %s)\n", i+1, artist.Name, artist.Listeners, artist.PlayCount) } } ``` -------------------------------- ### Authenticate Web App with Callback Flow (Go) Source: https://context7.com/sonjek/go-lastfm/llms.txt Sets up a web server to handle OAuth-style authentication for web applications. It provides a login link and a callback endpoint to process the authorization token and complete the user login, then displays a welcome message. ```go package main import ( "fmt" "log" "net/http" "net/url" "github.com/sonjek/go-lastfm/lastfm" ) var api *lastfm.Api func main() { api = lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { callback := "http://localhost:8080/callback" authUrl := api.GetAuthRequestUrl(callback) fmt.Fprintf(w, "Login with Last.fm", authUrl) }) http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) { query, _ := url.ParseQuery(r.URL.RawQuery) token := query.Get("token") err := api.LoginWithToken(token) if err != nil { fmt.Fprintf(w, "Login failed: %v", err) return } user, _ := api.User.GetInfo(nil) fmt.Fprintf(w, "Welcome, %s!", user.Name) }) log.Println("Server running at http://localhost:8080") log.Fatal(http.ListenAndServe(":8080", nil)) } ``` -------------------------------- ### Authenticate Desktop App with Token Flow (Go) Source: https://context7.com/sonjek/go-lastfm/llms.txt Implements the token-based authentication flow for desktop applications. It involves obtaining a token, generating an authorization URL for the user, and then completing the login using the token after user authorization. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") // Get an authorization token token, err := api.GetToken() if err != nil { fmt.Println("Error getting token:", err) return } // Generate URL for user to authorize authUrl := api.GetAuthTokenUrl(token) fmt.Println("Please visit this URL to authorize:", authUrl) // After user authorizes, complete the login fmt.Println("Press Enter after authorizing...") fmt.Scanln() err = api.LoginWithToken(token) if err != nil { fmt.Println("Token login failed:", err) return } fmt.Println("Successfully authenticated!") } ``` -------------------------------- ### Authenticate Desktop App Session with Last.fm API in Go Source: https://github.com/sonjek/go-lastfm/blob/master/README.md This Go code demonstrates the authentication flow for desktop applications using the Last.fm API. It involves obtaining a token, redirecting the user to an authorization URL, and then authorizing the token to log in. ```go token, _ := api.GetToken() authUrl := api.GetAuthTokenUrl(token) // Send your user to "authUrl" // Once the user grant permission, then authorize the token. api.LoginWithToken(token) ``` -------------------------------- ### Import go-lastfm Package in Go Source: https://github.com/sonjek/go-lastfm/blob/master/README.md This Go code snippet demonstrates how to import the go-lastfm package into your project. This is a prerequisite for using the library's functionalities to interact with the Last.fm API. ```go import "github.com/sonjek/go-lastfm/lastfm" ``` -------------------------------- ### Initialize Last.fm API Client in Go Source: https://github.com/sonjek/go-lastfm/blob/master/README.md This Go code initializes a new Last.fm API client instance. It requires your Last.fm API Key and API Secret to authenticate requests. The created 'api' object will be used to call various Last.fm API methods. ```go api := lastfm.New(ApiKey, ApiSecret) ``` -------------------------------- ### Authenticate Mobile App User (Go) Source: https://context7.com/sonjek/go-lastfm/llms.txt Handles mobile application authentication by directly using the username and password. After successful login, it retrieves the session key and fetches the authenticated user's information. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") err := api.Login("username", "password") if err != nil { fmt.Println("Login failed:", err) return } // Session key is now stored, you can retrieve it sessionKey := api.GetSessionKey() fmt.Println("Authenticated with session:", sessionKey) // Now you can call authenticated methods result, _ := api.User.GetInfo(nil) // Gets info for authenticated user fmt.Println("Logged in as:", result.Name) } ``` -------------------------------- ### Restore Last.fm Session with Key (Go) Source: https://context7.com/sonjek/go-lastfm/llms.txt Allows restoring a previously saved session key to re-authenticate the user without requiring them to log in again. This is useful for maintaining user sessions across application restarts. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") // Restore a previously saved session key savedSessionKey := "your_saved_session_key" api.SetSession(savedSessionKey) // Now authenticated methods work result, err := api.User.GetInfo(nil) if err != nil { fmt.Println("Error:", err) return } fmt.Println("User:", result.Name) } ``` -------------------------------- ### Add Tags to an Album using go-lastfm in Go Source: https://github.com/sonjek/go-lastfm/blob/master/README.md This Go code demonstrates how to add tags to an album using the go-lastfm library. This operation requires authentication. It uses `lastfm.P` to pass artist, album, and a slice of tags as parameters. ```go api.Album.AddTags(lastfm.P{ "artist": "Kaene", "album": "Strangeland", "tags": []string{"britpop", "alternative rock", "2012"}, }) ``` -------------------------------- ### Search for Artists with Go Source: https://context7.com/sonjek/go-lastfm/llms.txt Performs a search for artists based on their name. Requires API key and secret. Outputs the total number of results and a list of matching artists with their listener counts. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Artist.Search(lastfm.P{ "artist": "Beatles", "limit": 5, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Found %d results:\n", result.TotalResults) for _, artist := range result.ArtistMatches { fmt.Printf(" - %s (listeners: %s)\n", artist.Name, artist.Listeners) } } ``` -------------------------------- ### Add Tags to Album with Go Last.fm Client Source: https://context7.com/sonjek/go-lastfm/llms.txt Adds tags to a specific album. This operation requires user authentication (login). It takes the artist, album, and a list of tags as input. Outputs a success message or an error. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") api.Login("username", "password") err := api.Album.AddTags(lastfm.P{ "artist": "Keane", "album": "Strangeland", "tags": []string{"britpop", "alternative rock", "2012"}, }) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Tags added to album!") } ``` -------------------------------- ### Search Tracks with Go Last.fm Client Source: https://context7.com/sonjek/go-lastfm/llms.txt Searches for tracks based on track and artist names. It returns a list of matching tracks along with their listener counts and the total number of results. Requires an API key and secret. Outputs the total number of results and a list of track names, artists, and listener counts. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Track.Search(lastfm.P{ "track": "Imagine", "artist": "John Lennon", "limit": 5, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Found %d tracks:\n", result.TotalResults) for _, track := range result.Tracks { fmt.Printf(" - %s by %s (listeners: %s)\n", track.Name, track.Artist, track.Listeners) } } ``` -------------------------------- ### Search Albums with Go Last.fm Client Source: https://context7.com/sonjek/go-lastfm/llms.txt Searches for albums based on a given album name. It returns a list of matching albums along with the total number of results. Requires an API key and secret. Outputs the total number of results and a list of album names and artists. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") result, err := api.Album.Search(lastfm.P{ "album": "Dark Side", "limit": 5, }) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Found %d albums:\n", result.TotalResults) for _, album := range result.AlbumMatches { fmt.Printf(" - %s by %s\n", album.Name, album.Artist) } } ``` -------------------------------- ### Add Tags to Artist with Go Source: https://context7.com/sonjek/go-lastfm/llms.txt Adds tags to an artist. This operation requires user authentication (login). It takes an artist name and a list of tags as input. Outputs a success message or an error. ```go package main import ( "fmt" "github.com/sonjek/go-lastfm/lastfm" ) func main() { api := lastfm.New("YOUR_API_KEY", "YOUR_API_SECRET") api.Login("username", "password") err := api.Artist.AddTags(lastfm.P{ "artist": "Muse", "tags": []string{"alternative rock", "british", "progressive"}, }) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Tags added successfully!") } ``` -------------------------------- ### Authenticate Web App Session with Last.fm API in Go Source: https://github.com/sonjek/go-lastfm/blob/master/README.md This Go code illustrates the authentication process for web applications with the Last.fm API. It generates an authorization URL with a callback, and after user authorization, the token from the redirected URL is used to log in. ```go 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) ``` -------------------------------- ### Fetch Artist Top Tracks using go-lastfm in Go Source: https://github.com/sonjek/go-lastfm/blob/master/README.md This Go code snippet shows how to fetch the top tracks for a given artist using the go-lastfm library. It demonstrates making an API call and iterating through the results to print track names. Error handling is omitted for brevity. ```go result, _ := api.Artist.GetTopTracks(lastfm.P{"artist": "Avicii"}) for _, track := range result.Tracks { fmt.Println(track.Name) } ```