|
@@ -0,0 +1,120 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "net/http"
|
|
|
+ "fmt"
|
|
|
+ "os"
|
|
|
+ "time"
|
|
|
+ "encoding/json"
|
|
|
+ "sort"
|
|
|
+)
|
|
|
+
|
|
|
+func main(){
|
|
|
+
|
|
|
+ timeout := time.Duration( 10 * time.Second )
|
|
|
+ client := http.Client { Timeout: timeout }
|
|
|
+
|
|
|
+ req, err := http.NewRequest("GET", "https://musicbrainz.org/ws/2/artist?query=Metallica", nil)
|
|
|
+ req.Header.Set("User-Agent", "Go Application Development")
|
|
|
+ req.Header.Set("Content-Type", "application/json")
|
|
|
+ req.Header.Set("Accept", "application/json")
|
|
|
+
|
|
|
+ if (err != nil) {
|
|
|
+ fmt.Println("error creating request\n%v", err)
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
+
|
|
|
+ resp, err := client.Do(req)
|
|
|
+ if (err != nil) {
|
|
|
+ fmt.Println("error opening request\n%v", err)
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+ if (err != nil) {
|
|
|
+ fmt.Println("could not read body\n%v", err)
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
+ var result mb_artist_search
|
|
|
+ json.NewDecoder(resp.Body).Decode(&result)
|
|
|
+
|
|
|
+ fmt.Println(result.Artists[0].Name)
|
|
|
+
|
|
|
+ for _, element := range get_artist_tags(&result){
|
|
|
+ fmt.Printf("\ntag: %2d: %s", element.Count, element.Name)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func get_artist_tags (res *mb_artist_search) []mb_tag{
|
|
|
+ var t []mb_tag
|
|
|
+ for _, elem := range res.Artists[0].Tags {
|
|
|
+ if int(elem.Count) > 0 {
|
|
|
+ t = append(t, elem)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sort.Sort(ByInverseCount(t))
|
|
|
+ return t
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+type mb_artist_search struct {
|
|
|
+ Created time.Time `json:"created"`
|
|
|
+ Count int `json:"count"`
|
|
|
+ Offset int `json:"offset"`
|
|
|
+ Artists []struct {
|
|
|
+ ID string `json:"id"`
|
|
|
+ Type string `json:"type,omitempty"`
|
|
|
+ TypeID string `json:"type-id,omitempty"`
|
|
|
+ Score int `json:"score"`
|
|
|
+ Name string `json:"name"`
|
|
|
+ SortName string `json:"sort-name"`
|
|
|
+ Country string `json:"country,omitempty"`
|
|
|
+ Area struct {
|
|
|
+ ID string `json:"id"`
|
|
|
+ Type string `json:"type"`
|
|
|
+ TypeID string `json:"type-id"`
|
|
|
+ Name string `json:"name"`
|
|
|
+ SortName string `json:"sort-name"`
|
|
|
+ LifeSpan struct {
|
|
|
+ Ended interface{} `json:"ended"`
|
|
|
+ } `json:"life-span"`
|
|
|
+ } `json:"area,omitempty"`
|
|
|
+ BeginArea struct {
|
|
|
+ ID string `json:"id"`
|
|
|
+ Type string `json:"type"`
|
|
|
+ TypeID string `json:"type-id"`
|
|
|
+ Name string `json:"name"`
|
|
|
+ SortName string `json:"sort-name"`
|
|
|
+ LifeSpan struct {
|
|
|
+ Ended interface{} `json:"ended"`
|
|
|
+ } `json:"life-span"`
|
|
|
+ } `json:"begin-area,omitempty"`
|
|
|
+ Isnis []string `json:"isnis,omitempty"`
|
|
|
+ LifeSpan struct {
|
|
|
+ Begin string `json:"begin"`
|
|
|
+ Ended interface{} `json:"ended"`
|
|
|
+ } `json:"life-span,omitempty"`
|
|
|
+ Aliases []struct {
|
|
|
+ SortName string `json:"sort-name"`
|
|
|
+ TypeID string `json:"type-id,omitempty"`
|
|
|
+ Name string `json:"name"`
|
|
|
+ Locale interface{} `json:"locale"`
|
|
|
+ Type string `json:"type"`
|
|
|
+ Primary interface{} `json:"primary"`
|
|
|
+ BeginDate interface{} `json:"begin-date"`
|
|
|
+ EndDate interface{} `json:"end-date"`
|
|
|
+ } `json:"aliases,omitempty"`
|
|
|
+ Tags []mb_tag `json:"tags,omitempty"`
|
|
|
+ Disambiguation string `json:"disambiguation,omitempty"`
|
|
|
+ } `json:"artists"`
|
|
|
+}
|
|
|
+
|
|
|
+type mb_tag struct{
|
|
|
+ Count int `json:"count"`
|
|
|
+ Name string `json:"name"`
|
|
|
+}
|
|
|
+type ByInverseCount []mb_tag
|
|
|
+func (t ByInverseCount) Len() int { return len(t) }
|
|
|
+func (t ByInverseCount) Less (i, j int) bool { return t[i].Count >= t[j].Count }
|
|
|
+func (t ByInverseCount) Swap (i,j int) { t[i], t[j] = t[j], t[i] }
|