Created
March 8, 2015 16:15
-
-
Save aniketpant/771da06c331dc0956f97 to your computer and use it in GitHub Desktop.
Get last 50 loved tracks from Last.fm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
type jsonTracks struct { | |
LovedTracks struct { | |
Track []struct { | |
Name string `json:"name"` | |
Url string `json:"url"` | |
Date struct { | |
Text string `json:"#text"` | |
Timestamp string `json:"uts"` | |
} `json:"date"` | |
Artist struct { | |
Name string `json:"name"` | |
Url string `json:"url"` | |
} `json:"artist"` | |
Image []struct { | |
Url string `json:"#text"` | |
Size string `json:"size"` | |
} `json:"image"` | |
} `json:"track"` | |
} `json:"lovedtracks"` | |
} | |
func main() { | |
apiKey := "xxx" | |
userName := "xxx" | |
// apiSecret := "xxx" | |
url := "http://ws.audioscrobbler.com/2.0/?method=user.getlovedtracks&user=" + userName + "&api_key=" + apiKey + "&format=json" | |
res, err := http.Get(url) | |
if err != nil { | |
log.Fatal(err) | |
} | |
body, err := ioutil.ReadAll(res.Body) | |
res.Body.Close() | |
if err != nil { | |
log.Fatal(err) | |
} | |
if res.StatusCode != 200 { | |
log.Fatal("Unexpected status code", res.StatusCode) | |
} | |
var LovedTracks jsonTracks | |
err = json.Unmarshal(body, &LovedTracks) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for i := range LovedTracks.LovedTracks.Track { | |
fmt.Println(LovedTracks.LovedTracks.Track[i].Name + " by " + LovedTracks.LovedTracks.Track[i].Artist.Name) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment