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

35 lines
728 B
Go
Raw Normal View History

2020-02-04 20:25:57 +00:00
package main
import (
"encoding/json"
"errors"
"io/ioutil"
2020-02-07 09:24:19 +00:00
"net/http"
2020-02-04 20:25:57 +00:00
)
func allFeedItems(url string) ([]string, error) {
jsonFeed := &struct {
Items []struct {
Url string `json:"url"`
} `json:"items"`
}{}
2020-02-07 09:24:19 +00:00
resp, err := http.DefaultClient.Get(url)
2020-02-04 20:25:57 +00:00
if err != nil {
return nil, errors.New("failed to get json feed")
}
defer func() { _ = resp.Body.Close() }()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("failed to read json feed")
}
err = json.Unmarshal(body, &jsonFeed)
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)
}
return allUrls, nil
}