|
@@ -1,22 +1,42 @@
|
|
-package main
|
|
|
|
|
|
+package mb
|
|
|
|
|
|
import (
|
|
import (
|
|
- "net/http"
|
|
|
|
- "fmt"
|
|
|
|
- "os"
|
|
|
|
- "time"
|
|
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
|
+ "fmt"
|
|
|
|
+ "net/http"
|
|
"sort"
|
|
"sort"
|
|
|
|
+ "time"
|
|
)
|
|
)
|
|
|
|
|
|
-func main(){
|
|
|
|
|
|
+const (
|
|
|
|
+ API_URI = "https://musicbrainz.org/ws/2"
|
|
|
|
+ ARTIST_SEARCH_ENDPOINT = "artist?query"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+type mb struct {
|
|
|
|
+ Client http.Client
|
|
|
|
+ artist mb_artist_search
|
|
|
|
+}
|
|
|
|
|
|
- timeout := time.Duration( 10 * time.Second )
|
|
|
|
- client := http.Client { Timeout: timeout }
|
|
|
|
|
|
+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(
|
|
req, err := http.NewRequest(
|
|
"GET",
|
|
"GET",
|
|
- "https://musicbrainz.org/ws/2/artist?query=Metallica",
|
|
|
|
|
|
+ uri,
|
|
nil)
|
|
nil)
|
|
|
|
|
|
req.Header.Set(
|
|
req.Header.Set(
|
|
@@ -25,34 +45,29 @@ func main(){
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
|
|
- if (err != nil) {
|
|
|
|
- fmt.Println("error creating request\n%v", err)
|
|
|
|
- os.Exit(1)
|
|
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Printf("error creating request\n%v", err)
|
|
|
|
+ return err
|
|
}
|
|
}
|
|
|
|
|
|
- resp, err := client.Do(req)
|
|
|
|
- if (err != nil) {
|
|
|
|
- fmt.Println("error opening request\n%v", err)
|
|
|
|
- os.Exit(1)
|
|
|
|
|
|
+ resp, err := m.Client.Do(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Printf("error opening request\n%v", err)
|
|
|
|
+ return err
|
|
}
|
|
}
|
|
defer resp.Body.Close()
|
|
defer resp.Body.Close()
|
|
- if (err != nil) {
|
|
|
|
- fmt.Println("could not read body\n%v", err)
|
|
|
|
- os.Exit(1)
|
|
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Printf("could not read body\n%v", err)
|
|
|
|
+ return err
|
|
}
|
|
}
|
|
- 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)
|
|
|
|
- }
|
|
|
|
|
|
+ json.NewDecoder(resp.Body).Decode(&m.artist)
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func get_artist_tags (res *mb_artist_search) []mb_tag{
|
|
|
|
|
|
+func (m mb) get_artist_tags() []mb_tag {
|
|
var t []mb_tag
|
|
var t []mb_tag
|
|
- for _, elem := range res.Artists[0].Tags {
|
|
|
|
|
|
+ for _, elem := range m.artist.Artists[0].Tags {
|
|
if int(elem.Count) > 0 {
|
|
if int(elem.Count) > 0 {
|
|
t = append(t, elem)
|
|
t = append(t, elem)
|
|
}
|
|
}
|
|
@@ -61,66 +76,70 @@ func get_artist_tags (res *mb_artist_search) []mb_tag{
|
|
return 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_artist_search struct {
|
|
|
|
- Created time.Time `json:"created"`
|
|
|
|
- Count int `json:"count"`
|
|
|
|
- Offset int `json:"offset"`
|
|
|
|
- Artists []struct {
|
|
|
|
|
|
+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"`
|
|
ID string `json:"id"`
|
|
- Type string `json:"type,omitempty"`
|
|
|
|
- TypeID string `json:"type-id,omitempty"`
|
|
|
|
- Score int `json:"score"`
|
|
|
|
|
|
+ Type string `json:"type"`
|
|
|
|
+ TypeID string `json:"type-id"`
|
|
Name string `json:"name"`
|
|
Name string `json:"name"`
|
|
SortName string `json:"sort-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 {
|
|
LifeSpan struct {
|
|
- Begin string `json:"begin"`
|
|
|
|
Ended interface{} `json:"ended"`
|
|
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"`
|
|
|
|
|
|
+ } `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 mb_tag struct {
|
|
|
|
+ Count int `json:"count"`
|
|
|
|
+ Name string `json:"name"`
|
|
}
|
|
}
|
|
type ByInverseCount []mb_tag
|
|
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] }
|
|
|
|
|
|
+
|
|
|
|
+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] }
|