-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss.go
More file actions
52 lines (42 loc) · 913 Bytes
/
rss.go
File metadata and controls
52 lines (42 loc) · 913 Bytes
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
package main
import (
"encoding/xml"
"io"
"net/http"
"time"
)
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"`
}
func urlToFeed(url string) (RssFeed, error) {
httpClient := http.Client{
Timeout: time.Second * 10,
}
resp, err := httpClient.Get(url)
if err != nil {
return RssFeed{}, err
}
defer resp.Body.Close()
dat, err :=io.ReadAll(resp.Body)
if err != nil {
return RssFeed{}, err
}
rssfeed := RssFeed{}
err = xml.Unmarshal(dat, &rssfeed)
if err != nil {
return RssFeed{}, err
}
return rssfeed, nil
}