mb.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package mb
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "sort"
  7. "time"
  8. )
  9. const (
  10. API_URI = "https://musicbrainz.org/ws/2"
  11. ARTIST_SEARCH_ENDPOINT = "artist?query"
  12. )
  13. type mb struct {
  14. Client http.Client
  15. artist mb_artist_search
  16. }
  17. func NewMusicBrainzSearch() mb {
  18. var mb_search mb
  19. timeout := time.Duration(10 * time.Second)
  20. mb_search.Client = http.Client{Timeout: timeout}
  21. return mb_search
  22. }
  23. func (m mb) get_artist_name() string {
  24. return m.artist.Artists[0].Name
  25. }
  26. func (m mb) search_for_artist(artist string) error {
  27. uri := fmt.Sprintf("%s/%s=%s", API_URI, ARTIST_SEARCH_ENDPOINT, artist)
  28. req, err := http.NewRequest(
  29. "GET",
  30. uri,
  31. nil)
  32. req.Header.Set(
  33. "User-Agent",
  34. "Go Application Development: Create spotify playlist based on artist")
  35. req.Header.Set("Content-Type", "application/json")
  36. req.Header.Set("Accept", "application/json")
  37. if err != nil {
  38. fmt.Printf("error creating request\n%v", err)
  39. return err
  40. }
  41. resp, err := m.Client.Do(req)
  42. if err != nil {
  43. fmt.Printf("error opening request\n%v", err)
  44. return err
  45. }
  46. defer resp.Body.Close()
  47. if err != nil {
  48. fmt.Printf("could not read body\n%v", err)
  49. return err
  50. }
  51. json.NewDecoder(resp.Body).Decode(&m.artist)
  52. return nil
  53. }
  54. func (m mb) get_artist_tags() []mb_tag {
  55. var t []mb_tag
  56. for _, elem := range m.artist.Artists[0].Tags {
  57. if int(elem.Count) > 0 {
  58. t = append(t, elem)
  59. }
  60. }
  61. sort.Sort(ByInverseCount(t))
  62. return t
  63. }
  64. type mb_artist_search struct {
  65. Created time.Time `json:"created"`
  66. Count int `json:"count"`
  67. Offset int `json:"offset"`
  68. Artists []mb_artists `json:"artists"`
  69. }
  70. type mb_area struct {
  71. ID string `json:"id"`
  72. Type string `json:"type"`
  73. TypeID string `json:"type-id"`
  74. Name string `json:"name"`
  75. SortName string `json:"sort-name"`
  76. LifeSpan struct {
  77. Ended interface{} `json:"ended"`
  78. } `json:"life-span"`
  79. }
  80. type mb_alias struct {
  81. SortName string `json:"sort-name"`
  82. TypeID string `json:"type-id,omitempty"`
  83. Name string `json:"name"`
  84. Locale interface{} `json:"locale"`
  85. Type string `json:"type"`
  86. Primary interface{} `json:"primary"`
  87. BeginDate interface{} `json:"begin-date"`
  88. EndDate interface{} `json:"end-date"`
  89. }
  90. type mb_artists struct {
  91. ID string `json:"id"`
  92. Type string `json:"type,omitempty"`
  93. TypeID string `json:"type-id,omitempty"`
  94. Score int `json:"score"`
  95. Name string `json:"name"`
  96. SortName string `json:"sort-name"`
  97. Country string `json:"country,omitempty"`
  98. Area mb_area `json:"area,omitempty"`
  99. BeginArea struct {
  100. ID string `json:"id"`
  101. Type string `json:"type"`
  102. TypeID string `json:"type-id"`
  103. Name string `json:"name"`
  104. SortName string `json:"sort-name"`
  105. LifeSpan struct {
  106. Ended interface{} `json:"ended"`
  107. } `json:"life-span"`
  108. } `json:"begin-area,omitempty"`
  109. Isnis []string `json:"isnis,omitempty"`
  110. LifeSpan struct {
  111. Begin string `json:"begin"`
  112. Ended interface{} `json:"ended"`
  113. } `json:"life-span,omitempty"`
  114. Aliases []mb_alias `json:"aliases,omitempty"`
  115. Tags []mb_tag `json:"tags,omitempty"`
  116. Disambiguation string `json:"disambiguation,omitempty"`
  117. }
  118. type mb_tag struct {
  119. Count int `json:"count"`
  120. Name string `json:"name"`
  121. }
  122. type ByInverseCount []mb_tag
  123. func (t ByInverseCount) Len() int { return len(t) }
  124. func (t ByInverseCount) Less(i, j int) bool { return t[i].Count >= t[j].Count }
  125. func (t ByInverseCount) Swap(i, j int) { t[i], t[j] = t[j], t[i] }