123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package mb
- import (
- "encoding/json"
- "fmt"
- "net/http"
- "sort"
- "time"
- )
- const (
- apiURI = "https://musicbrainz.org/ws/2"
- artistSearchEndpoint = "artist?query"
- )
- //MB holds the http client and the search results
- type MB struct {
- Client http.Client
- artist artistSearch
- }
- //NewMusicBrainzSearch returns a new Search object
- func NewMusicBrainzSearch() MB {
- var mbSearch MB
- timeout := time.Duration(10 * time.Second)
- mbSearch.Client = http.Client{Timeout: timeout}
- return mbSearch
- }
- //GetArtistName get the artist with the highest score on the search
- func (m MB) GetArtistName() (string, error) {
- if len(m.artist.Artists) == 0 {
- return "", fmt.Errorf("could not find any artist")
- }
- return m.artist.Artists[0].Name, nil
- }
- //SearchForArtist return a list of artists using `query` as parameter if none found returns error
- func (m *MB) SearchForArtist(query string) error {
- uri := fmt.Sprintf("%s/%s=%s", apiURI, artistSearchEndpoint, query)
- 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
- }
- err = json.NewDecoder(resp.Body).Decode(&m.artist)
- if err != nil {
- fmt.Printf("could not decode body\n%v", err)
- return err
- }
- if m.artist.Count == 0 {
- return fmt.Errorf("cound not found artist: %s", query)
- }
- return nil
- }
- //GetArtistTags returns the tags in order of relevance of the current artist
- func (m MB) GetArtistTags() []Tags {
- var t []Tags
- for _, elem := range m.artist.Artists[0].Tags {
- if int(elem.Count) > 0 {
- t = append(t, elem)
- }
- }
- sort.Sort(ByInverseCount(t))
- return t
- }
- type artistSearch struct {
- Created time.Time `json:"created"`
- Count int `json:"count"`
- Offset int `json:"offset"`
- Artists []artists `json:"artists"`
- }
- type 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 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 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 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 []alias `json:"aliases,omitempty"`
- Tags []Tags `json:"tags,omitempty"`
- Disambiguation string `json:"disambiguation,omitempty"`
- }
- //Tags represent Artist tags (eg. Rock, Punk, Late 2000s)
- type Tags struct {
- Count int `json:"count"`
- Name string `json:"name"`
- }
- //ByInverseCount return the tags in relevance order
- type ByInverseCount []Tags
- 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] }
|