You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

36 lines
789 B

  1. package main
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/http"
  6. )
  7. type Article struct {
  8. Url string `json:"url"`
  9. }
  10. func LatestArticle(url string) (*Article, error) {
  11. jsonFeed := &struct {
  12. Items []Article `json:"items"`
  13. }{}
  14. req, err := http.NewRequest(http.MethodGet, url, nil)
  15. if err != nil {
  16. return nil, errors.New("failed to create req to get json feed")
  17. }
  18. req.Header.Add("User-Agent", "BunnyPurge")
  19. resp, err := http.DefaultClient.Do(req)
  20. if err != nil {
  21. return nil, errors.New("failed to get json feed")
  22. }
  23. err = json.NewDecoder(resp.Body).Decode(&jsonFeed)
  24. _ = resp.Body.Close()
  25. if err != nil {
  26. return nil, errors.New("failed to parse json feed")
  27. }
  28. if len(jsonFeed.Items) < 1 {
  29. return nil, errors.New("no articles in feed")
  30. }
  31. return &jsonFeed.Items[0], nil
  32. }