-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.go
98 lines (89 loc) · 2.14 KB
/
feed.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package syndfeed
import (
"errors"
"io"
"net/http"
"time"
"github.com/antchfx/xmlquery"
)
// Feed is top-level feed object, <feed> in Atom 1.0 and
// <rss> in RSS 2.0.
type Feed struct {
Authors []*Person
BaseURL string
Categories []string
Contributors []*Person
Copyright string
Namespace map[string]string // map[namespace-prefix]namespace-url
Description string
Generator string
Id string
ImageURL string
Items []*Item
Language string
// LastUpdatedTime is the feed was last updated time.
LastUpdatedTime time.Time
Title string
Links []*Link
Version string
ElementExtensions []*ElementExtension
}
// Link represents a link within a syndication
// feed or item.
type Link struct {
MediaType string
URL string
Title string
RelType string
}
// Item is a feed item.
type Item struct {
BaseURL string
Authors []*Person
Contributors []*Person
Categories []string
Content string
Copyright string
Id string
// LastUpdatedTime is the feed item last updated time.
LastUpdatedTime time.Time
Links []*Link
// PublishDate is the feed item publish date.
PublishDate time.Time
Summary string
Title string
ElementExtensions []*ElementExtension
//CommentURL string
}
// Person is an author or contributor of the feed content.
type Person struct {
Name string
URL string
Email string
}
// ElementExtension is an syndication element extension.
type ElementExtension struct {
Name, Namespace, Value string
}
// Parse parses a syndication feed(RSS,Atom).
func Parse(r io.Reader) (*Feed, error) {
doc, err := xmlquery.Parse(r)
if err != nil {
return nil, err
}
if doc.SelectElement("rss") != nil {
return rss.parse(doc)
} else if doc.SelectElement("feed") != nil {
return atom.parse(doc)
}
return nil, errors.New("invalid syndication feed without <rss> or <feed> element")
}
// LoadURL loads a syndication feed URL.
func LoadURL(url string) (*Feed, error) {
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
return Parse(res.Body)
}