master
serr 2024-11-20 17:01:51 +03:00
commit c9e20526f2
2 changed files with 48 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module last.fm_get_recent
go 1.23.2

45
main.go Normal file
View File

@ -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()
}