-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrss2.0.go
206 lines (175 loc) · 5.89 KB
/
rss2.0.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package feedreader
/*
RSS 2.0 parser
The elements below are not supported.
channel/category
channel/docs
channel/cloud
channel/rating
channel/textInput
channel/item/category
channel/item/source
*/
import "time"
import "strings"
import "fmt"
import "github.com/m3ng9i/go-utils/set"
// rss/image element
type Rss20Image struct {
Url string `xml:"url"`
Title string `xml:"title"`
Link string `xml:"link"`
Width uint `xml:"width"`
Height uint `xml:"height"`
Description string `xml:"description"`
}
// rss/item/enclosure element
type Rss20ItemEnclosure struct {
Url string `xml:"url,attr"`
Length uint64 `xml:"length,attr"`
Type string `xml:"type,attr"`
}
// rss/item/guid element
type Rss20ItemGuid struct {
Guid string `xml:",chardata"`
IsPermaLinkRaw string `xml:"isPermaLink,attr"`
IsPermaLink bool
}
// rss/item element
type Rss20Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
Author string `xml:"author"`
Comments string `xml:"comments"`
Enclosure *Rss20ItemEnclosure `xml:"enclosure"`
Guid *Rss20ItemGuid `xml:"guid"`
PubDateRaw string `xml:"pubDate"`
PubDate time.Time
}
// whole rss element
type Rss20 struct {
Version string `xml:"version,attr"`
Title string `xml:"channel>title"`
Link string
Description string `xml:"channel>description"`
Language string `xml:"channel>language"`
Copyright string `xml:"channel>copyright"`
ManagingEditor string `xml:"channel>managingEditor"`
WebMaster string `xml:"channel>webMaster"`
PubDateRaw string `xml:"channel>pubDate"`
PubDate time.Time
LastBuildDateRaw string `xml:"channel>lastBuildDate"`
LastBuildDate time.Time
Generator string `xml:"channel>generator"`
Ttl uint `xml:"channel>ttl"`
Image *Rss20Image `xml:"channel>image"`
SkipHours []uint8 `xml:"channel>skipHours>hours"`
SkipDaysRaw []string `xml:"channel>skipDays>days"`
SkipDays []time.Weekday
Item []*Rss20Item `xml:"channel>item"`
}
func weekdayToNumber(s string) (week time.Weekday, ok bool) {
ok = true
switch strings.ToLower(s) {
case "monday":
week = time.Monday
return
case "tuesday":
week = time.Tuesday
return
case "wednesday":
week = time.Wednesday
return
case "thursday":
week = time.Thursday
return
case "friday":
week = time.Friday
return
case "saturday":
week = time.Saturday
return
case "sunday":
week = time.Sunday
return
}
ok = false // error
return
}
// Parse rss xml data to *RSS20 structure.
// If returned error is not nil, it will be ParseError.
func Rss20Parse(b []byte) (rss *Rss20, err error) {
e := unmarshal(b, &rss)
if e != nil {
err = &ParseError{Err:e}
return
}
if rss.Version != "2.0" {
err = &ParseError{Err:fmt.Errorf("RSS version: %s is not supported.", rss.Version)}
return
}
rss.PubDate, _ = ParseTime(rss.PubDateRaw)
rss.LastBuildDate, _ = ParseTime(rss.LastBuildDateRaw)
days := set.New()
for _, i := range(rss.SkipDaysRaw) {
n, ok := weekdayToNumber(i)
if ok {
days.Add(n)
}
}
daysArray := make([]time.Weekday, 0, days.Len())
for _, i := range(days.List()) {
v, _ := i.(time.Weekday)
daysArray = append(daysArray, v)
}
rss.SkipDays = daysArray
for i := range(rss.Item) {
if rss.Item[i].Guid != nil {
rss.Item[i].Guid.IsPermaLink = strings.ToLower(
rss.Item[i].Guid.IsPermaLinkRaw) == "true"
}
rss.Item[i].PubDate, _ = ParseTime(rss.Item[i].PubDateRaw)
}
rss.Link, err = rss20ParseLink(b)
return
}
// Parse rss xml data to *RSS20 structure
// If returned error is not nil, it will be ParseError.
func Rss20ParseString(xmldata string) (*Rss20, error) {
return Rss20Parse([]byte(xmldata))
}
/*
Parse link element of rss.
The below rss example provide two link elements: one with empty namespace and another with namespace 'atom'.
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Title</title>
<description>Some description.</description>
<link>http://www.example.com/</link>
<atom:link href="http://www.example.com/feed.xml" rel="self" type="application/rss+xml"/>
...
</channel>
</rss>
If unmarshal the xml into the following struct:
Link string `xml:"channel>link"`
the second link(the one with namespace)'s value will override the first one, so the Link will be empty string, which is not expected.
This function could solve the problem.
*/
func rss20ParseLink(b []byte) (link string, err error) {
var t struct {
Link []string `xml:"channel>link"`
}
err = unmarshal(b, &t)
if err != nil {
return
}
for _, item := range t.Link {
if item != "" {
link = item
return
}
}
return
}