-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss.go
58 lines (48 loc) · 1.14 KB
/
rss.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"encoding/xml"
"io"
"net/http"
"time"
)
// get marshalled XML RSS feed from a URL
func urlToFeed(url string) (RSSFeed, error){
// Set up an HTTP client to get RSS feed
httpClient := http.Client{
Timeout: 10 * time.Second,
}
// Make GET request to RSS url
resp, err := httpClient.Get(url)
if err != nil {
return RSSFeed{}, err
}
// defer to close response body till return (finally)
defer resp.Body.Close()
// Read all response body to byte arrays(slice)
data, err := io.ReadAll(resp.Body)
if err != nil {
return RSSFeed{}, err
}
// empty struct to hold values
rssFeed := RSSFeed{}
err = xml.Unmarshal(data, &rssFeed)
if err != nil {
return RSSFeed{}, err
}
return rssFeed, nil
}
type RSSFeed struct {
Channel struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
Language string `xml:"language"`
Item []RSSItem `xml:"item"`
} `xml:"channel"`
}
type RSSItem struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
PubDate string `xml:"pubDate"`
}