123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package feed
- import (
- "encoding/xml"
- "golang.org/x/tools/blog/atom"
- "io/ioutil"
- "net/http"
- )
- func getFeedContent(url string) ([]byte, error) {
- // create a HTTP client
- client := new(http.Client)
- // GET ATOM feed url
- req, err := http.NewRequest("GET", url, nil)
- if err != nil {
- return nil, err
- }
- // set custom header
- req.Header.Add("User-Agent", "knapzak feed aggregator - https://github.com/mavjs/goknapzak")
- res, err := client.Do(req)
- if err != nil {
- return nil, err
- }
- // read body of ATOM feed
- body, err := ioutil.ReadAll(res.Body)
- if err != nil {
- return nil, err
- }
- defer res.Body.Close()
- return body, err
- }
- func GetAtomFeed(url string) (atom.Feed, error) {
- var feed atom.Feed
- body, err := getFeedContent(url)
- if err != nil {
- return feed, err
- }
- err = xml.Unmarshal(body, &feed)
- if err != nil {
- return feed, err
- }
- return feed, err
- }
|