|
5 | 5 | "errors" |
6 | 6 | "fmt" |
7 | 7 | "net/http" |
| 8 | + "net/url" |
8 | 9 | ) |
9 | 10 |
|
10 | 11 | // Client contains everything needed for a Last.FM client |
@@ -76,7 +77,8 @@ type TopArtist struct { |
76 | 77 |
|
77 | 78 | // GetTopArtists gets the top artist of a user from Last.FM |
78 | 79 | func (c *Client) GetTopArtists(username string, period string, limit int) ([]TopArtist, error) { |
79 | | - req, err := http.NewRequest("GET", fmt.Sprintf("http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=%s&api_key=%s&format=json&period=%s&limit=%d", username, c.APIKey, period, limit), nil) |
| 80 | + call := fmt.Sprintf("http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=%s&api_key=%s&format=json&period=%s&limit=%d", username, c.APIKey, period, limit) |
| 81 | + req, err := http.NewRequest("GET", call, nil) |
80 | 82 | if err != nil { |
81 | 83 | return nil, err |
82 | 84 | } |
@@ -146,9 +148,29 @@ func (c *Client) GetWeeklyArtistChart(username string, from, to int64, limit int |
146 | 148 | return tal, nil |
147 | 149 | } |
148 | 150 |
|
149 | | -// GetTopTags gets the top tags for an artist from the Last.FM API |
150 | | -func (c *Client) GetTopTags(mbid string) ([]string, error) { |
151 | | - req, err := http.NewRequest("GET", fmt.Sprintf("http://ws.audioscrobbler.com/2.0/?method=artist.gettoptags&mbid=%s&api_key=%s&format=json", mbid, c.APIKey), nil) |
| 151 | +// GetTopTags gets the top tags for an artist from the Last.FM API by mbid, or artist as a fallback |
| 152 | +func (c *Client) GetTopTags(mbid string, artist string) ([]string, error) { |
| 153 | + if mbid == "" { |
| 154 | + return c.getTopTags("artist", artist) |
| 155 | + } |
| 156 | + return c.getTopTags("mbid", mbid) |
| 157 | +} |
| 158 | + |
| 159 | +func (c *Client) getTopTags(by string, value string) ([]string, error) { |
| 160 | + call := fmt.Sprintf("http://ws.audioscrobbler.com/2.0/?method=artist.gettoptags&api_key=%s&format=json", c.APIKey) |
| 161 | + u, err := url.Parse(call) |
| 162 | + if err != nil { |
| 163 | + return nil, err |
| 164 | + } |
| 165 | + q := u.Query() |
| 166 | + switch by { |
| 167 | + case "mbid": |
| 168 | + q.Add("mbid", value) |
| 169 | + default: |
| 170 | + q.Add("artist", value) |
| 171 | + } |
| 172 | + u.RawQuery = q.Encode() |
| 173 | + req, err := http.NewRequest("GET", u.String(), nil) |
152 | 174 | if err != nil { |
153 | 175 | return nil, err |
154 | 176 | } |
|
0 commit comments