get_last_tracks_lastfm/main.go

46 lines
1.2 KiB
Go
Raw Permalink Normal View History

2024-11-20 17:01:51 +03:00
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
// api key тут https://www.last.fm/api/accounts
const apiKey = "API KEY" // Замените на ваш API-ключ
const username = "NAME" // Замените на ваше имя пользователя Last.fm
type LastFMResponse struct {
RecentTracks struct {
Track []struct {
Name string `json:"name"`
Artist struct {
Name string `json:"#text"`
} `json:"artist"`
Album struct {
Name string `json:"#text"`
} `json:"album"`
Date struct {
Unix string `json:"#text"`
} `json:"date"`
} `json:"track"`
} `json:"recenttracks"`
}
func getRecentTracks() {
url := fmt.Sprintf("https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=%s&api_key=%s&format=json", username, apiKey)
resp, _ := http.Get(url)
body, _ := io.ReadAll(resp.Body)
var lastFMResponse LastFMResponse
json.Unmarshal(body, &lastFMResponse)
for _, track := range lastFMResponse.RecentTracks.Track {
fmt.Printf("Track: %s, Artist: %s, Album: %s, Date: %s\n", track.Name, track.Artist.Name, track.Album.Name, track.Date.Unix)
}
resp.Body.Close()
}
func main() {
getRecentTracks()
}