atom.go 884 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package feed
  2. import (
  3. "encoding/xml"
  4. "golang.org/x/tools/blog/atom"
  5. "io/ioutil"
  6. "net/http"
  7. )
  8. func getFeedContent(url string) ([]byte, error) {
  9. // create a HTTP client
  10. client := new(http.Client)
  11. // GET ATOM feed url
  12. req, err := http.NewRequest("GET", url, nil)
  13. if err != nil {
  14. return nil, err
  15. }
  16. // set custom header
  17. req.Header.Add("User-Agent", "knapzak feed aggregator - https://github.com/mavjs/goknapzak")
  18. res, err := client.Do(req)
  19. if err != nil {
  20. return nil, err
  21. }
  22. // read body of ATOM feed
  23. body, err := ioutil.ReadAll(res.Body)
  24. if err != nil {
  25. return nil, err
  26. }
  27. defer res.Body.Close()
  28. return body, err
  29. }
  30. func GetAtomFeed(url string) (atom.Feed, error) {
  31. var feed atom.Feed
  32. body, err := getFeedContent(url)
  33. if err != nil {
  34. return feed, err
  35. }
  36. err = xml.Unmarshal(body, &feed)
  37. if err != nil {
  38. return feed, err
  39. }
  40. return feed, err
  41. }