|
@@ -13,34 +13,29 @@ const (
|
|
|
artistSearchEndpoint = "artist?query"
|
|
|
)
|
|
|
|
|
|
-//MB holds the http client and the search results
|
|
|
+//MB holds the http client
|
|
|
type MB struct {
|
|
|
Client http.Client
|
|
|
+}
|
|
|
+
|
|
|
+//Search holds the search returns
|
|
|
+type Search struct {
|
|
|
artist artistSearch
|
|
|
}
|
|
|
|
|
|
-//NewMusicBrainzSearch returns a new Search object
|
|
|
-func NewMusicBrainzSearch() MB {
|
|
|
+//NewMusicBrainz returns a new Search object
|
|
|
+func NewMusicBrainz() MB {
|
|
|
|
|
|
- var mbSearch MB
|
|
|
+ var mb 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
|
|
|
+ mb.Client = http.Client{Timeout: timeout}
|
|
|
+ return mb
|
|
|
}
|
|
|
|
|
|
//SearchForArtist return a list of artists using `query` as parameter if none found returns error
|
|
|
-func (m *MB) SearchForArtist(query string) error {
|
|
|
-
|
|
|
+func (m MB) SearchForArtist(query string) (*Search, error) {
|
|
|
+ var ret Search
|
|
|
uri := fmt.Sprintf("%s/%s=%s", apiURI, artistSearchEndpoint, query)
|
|
|
|
|
|
req, err := http.NewRequest(
|
|
@@ -56,36 +51,46 @@ func (m *MB) SearchForArtist(query string) error {
|
|
|
|
|
|
if err != nil {
|
|
|
fmt.Printf("error creating request\n%v", err)
|
|
|
- return err
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
resp, err := m.Client.Do(req)
|
|
|
if err != nil {
|
|
|
fmt.Printf("error opening request\n%v", err)
|
|
|
- return err
|
|
|
+ return nil, err
|
|
|
}
|
|
|
defer resp.Body.Close()
|
|
|
if err != nil {
|
|
|
fmt.Printf("could not read body\n%v", err)
|
|
|
- return err
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- err = json.NewDecoder(resp.Body).Decode(&m.artist)
|
|
|
+ err = json.NewDecoder(resp.Body).Decode(&ret.artist)
|
|
|
if err != nil {
|
|
|
fmt.Printf("could not decode body\n%v", err)
|
|
|
- return err
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if ret.artist.Count == 0 {
|
|
|
+ return nil, fmt.Errorf("cound not found artist: %s", query)
|
|
|
}
|
|
|
|
|
|
- if m.artist.Count == 0 {
|
|
|
- return fmt.Errorf("cound not found artist: %s", query)
|
|
|
+ return &ret, nil
|
|
|
+}
|
|
|
+
|
|
|
+//GetArtistName get the artist with the highest score on the search
|
|
|
+func (s Search) GetArtistName() (string, error) {
|
|
|
+
|
|
|
+ if len(s.artist.Artists) == 0 {
|
|
|
+ return "", fmt.Errorf("could not find any artist")
|
|
|
}
|
|
|
- return nil
|
|
|
+ return s.artist.Artists[0].Name, nil
|
|
|
}
|
|
|
|
|
|
//GetArtistTags returns the tags in order of relevance of the current artist
|
|
|
-func (m MB) GetArtistTags() []Tags {
|
|
|
+func (s Search) GetArtistTags() []Tags {
|
|
|
var t []Tags
|
|
|
- for _, elem := range m.artist.Artists[0].Tags {
|
|
|
+ for _, elem := range s.artist.Artists[0].Tags {
|
|
|
if int(elem.Count) > 0 {
|
|
|
t = append(t, elem)
|
|
|
}
|