commit c9e20526f250dab9882905aa70ac666ddd66d2f0 Author: serr Date: Wed Nov 20 17:01:51 2024 +0300 ^_^ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c3be008 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module last.fm_get_recent + +go 1.23.2 diff --git a/main.go b/main.go new file mode 100644 index 0000000..cb02502 --- /dev/null +++ b/main.go @@ -0,0 +1,45 @@ +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() +}