mb.go 3.2 KB

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