aur.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package aur
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. )
  8. const aurURL = "https://aur.archlinux.org/rpc.php?"
  9. type response struct {
  10. Error string `json:"error"`
  11. Version int `json:"version"`
  12. Type string `json:"type"`
  13. ResultCount int `json:"resultcount"`
  14. Results []Pkg `json:"results"`
  15. }
  16. // Pkg holds package information
  17. type Pkg struct {
  18. ID int `json:"ID"`
  19. Name string `json:"Name"`
  20. PackageBaseID int `json:"PackageBaseID"`
  21. PackageBase string `json:"PackageBase"`
  22. Version string `json:"Version"`
  23. Description string `json:"Description"`
  24. URL string `json:"URL"`
  25. NumVotes int `json:"NumVotes"`
  26. Popularity float64 `json:"Popularity"`
  27. OutOfDate int `json:"OutOfDate"`
  28. Maintainer string `json:"Maintainer"`
  29. FirstSubmitted int `json:"FirstSubmitted"`
  30. LastModified int `json:"LastModified"`
  31. URLPath string `json:"URLPath"`
  32. Depends []string `json:"Depends"`
  33. MakeDepends []string `json:"MakeDepends"`
  34. Conflicts []string `json:"Conflicts"`
  35. Replaces []string `json:"Replaces"`
  36. OptDepends []string `json:"OptDepends"`
  37. License []string `json:"License"`
  38. Keywords []string `json:"Keywords"`
  39. }
  40. func get(values url.Values) ([]Pkg, error) {
  41. values.Set("v", "5")
  42. resp, err := http.Get(aurURL + values.Encode())
  43. if err != nil {
  44. return nil, err
  45. }
  46. defer resp.Body.Close()
  47. dec := json.NewDecoder(resp.Body)
  48. result := new(response)
  49. err = dec.Decode(result)
  50. if err != nil {
  51. return nil, err
  52. }
  53. if len(result.Error) > 0 {
  54. return nil, fmt.Errorf(result.Error)
  55. }
  56. return result.Results, nil
  57. }
  58. // Search searches for packages by package name.
  59. func Search(query string) ([]Pkg, error) {
  60. v := url.Values{}
  61. v.Set("type", "search")
  62. v.Set("arg", query)
  63. return get(v)
  64. }
  65. // SearchByNameDesc searches for package by package name and description.
  66. func SearchByNameDesc(query string) ([]Pkg, error) {
  67. v := url.Values{}
  68. v.Set("type", "search")
  69. v.Set("by", "name-desc")
  70. v.Set("arg", query)
  71. return get(v)
  72. }
  73. // SearchByMaintainer searches for package by maintainer.
  74. func SearchByMaintainer(query string) ([]Pkg, error) {
  75. v := url.Values{}
  76. v.Set("type", "search")
  77. v.Set("by", "maintainer")
  78. v.Set("arg", query)
  79. return get(v)
  80. }
  81. // Info shows info for one or multiple packages.
  82. func Info(pkgs []string) ([]Pkg, error) {
  83. v := url.Values{}
  84. v.Set("type", "info")
  85. for _, arg := range pkgs {
  86. v.Add("arg[]", arg)
  87. }
  88. return get(v)
  89. }