package mb import ( "encoding/json" "fmt" "net/http" "sort" "time" ) const ( API_URI = "https://musicbrainz.org/ws/2" ARTIST_SEARCH_ENDPOINT = "artist?query" ) type mb struct { Client http.Client artist mb_artist_search } func NewMusicBrainzSearch() mb { var mb_search mb timeout := time.Duration(10 * time.Second) mb_search.Client = http.Client{Timeout: timeout} return mb_search } func (m mb) get_artist_name() string { return m.artist.Artists[0].Name } func (m mb) search_for_artist(artist string) error { uri := fmt.Sprintf("%s/%s=%s", API_URI, ARTIST_SEARCH_ENDPOINT, artist) req, err := http.NewRequest( "GET", uri, nil) req.Header.Set( "User-Agent", "Go Application Development: Create spotify playlist based on artist") req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json") if err != nil { fmt.Printf("error creating request\n%v", err) return err } resp, err := m.Client.Do(req) if err != nil { fmt.Printf("error opening request\n%v", err) return err } defer resp.Body.Close() if err != nil { fmt.Printf("could not read body\n%v", err) return err } json.NewDecoder(resp.Body).Decode(&m.artist) return nil } func (m mb) get_artist_tags() []mb_tag { var t []mb_tag for _, elem := range m.artist.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 []mb_artists `json:"artists"` } type mb_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"` } type mb_alias 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"` } type mb_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 mb_area `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 []mb_alias `json:"aliases,omitempty"` Tags []mb_tag `json:"tags,omitempty"` Disambiguation string `json:"disambiguation,omitempty"` } 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] }