mb.go 3.1 KB

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