jlelse
/
jsonpub
Archived
1
Fork 0
This repository has been archived on 2020-04-25. You can view files and clone it, but cannot push or open issues or pull requests.
jsonpub/feed.go

36 lines
813 B
Go
Raw Normal View History

2020-02-04 20:25:57 +00:00
package main
import (
"encoding/json"
"errors"
2020-02-07 10:00:36 +00:00
"fmt"
2020-02-07 09:24:19 +00:00
"net/http"
2020-02-04 20:25:57 +00:00
)
2020-04-20 21:48:26 +00:00
func allFeedItems(url string) ([]string, error) {
2020-02-04 20:25:57 +00:00
jsonFeed := &struct {
Items []struct {
Url string `json:"url"`
} `json:"items"`
}{}
2020-02-07 10:00:36 +00:00
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, errors.New("failed to create req to get json feed")
}
req.Header.Add("User-Agent", fmt.Sprintf("%s %s", libName, version))
resp, err := http.DefaultClient.Do(req)
2020-02-04 20:25:57 +00:00
if err != nil {
return nil, errors.New("failed to get json feed")
}
2020-03-26 18:04:05 +00:00
err = json.NewDecoder(resp.Body).Decode(&jsonFeed)
2020-04-20 21:48:26 +00:00
_ = resp.Body.Close()
2020-02-04 20:25:57 +00:00
if err != nil {
return nil, errors.New("failed to parse json feed")
}
var allUrls []string
for _, item := range jsonFeed.Items {
allUrls = append(allUrls, item.Url)
}
2020-04-20 21:48:26 +00:00
return allUrls, nil
2020-02-04 20:25:57 +00:00
}