123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- 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] }
|