-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss.go
63 lines (51 loc) · 1.25 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
59
60
61
62
63
/*
Simple RSS parser, tested with Wordpress feeds.
*/
package rss
import (
"encoding/xml"
"io/ioutil"
"net/http"
)
type Channel struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
Language string `xml:"language"`
LastBuildDate string `xml:"lastBuildDate"`
Item []*Item `xml:"item"`
}
type ItemEnclosure struct {
URL string `xml:"url,attr"`
Type string `xml:"type,attr"`
}
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Comments string `xml:"comments"`
PubDate string `xml:"pubDate"`
GUID string `xml:"guid"`
Category []string `xml:"category"`
Enclosure *ItemEnclosure `xml:"enclosure"`
Description string `xml:"description"`
Content string `xml:"content"`
}
func Read(url string) (*Channel, error) {
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
text, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
var rss struct {
Channel *Channel `xml:"channel"`
}
err = xml.Unmarshal(text, &rss)
if err != nil {
return nil, err
}
return rss.Channel, nil
}