hikan.ru/mvc/models/lastfm.go

45 lines
1.0 KiB
Go

package models
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
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(username, apiKey string) (*LastFMResponse, error) {
url := fmt.Sprintf("https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=%s&api_key=%s&format=json", username, apiKey)
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("lastfm get resp err: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("lastfm read resp body err: %v", err)
}
var lastFMResponse LastFMResponse
if err := json.Unmarshal(body, &lastFMResponse); err != nil {
return nil, fmt.Errorf("lastfm unmarshal err: %v", err)
}
return &lastFMResponse, nil
}