/
atom.go
275 lines (211 loc) · 6.83 KB
/
atom.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package feedparser
import (
"encoding/xml"
)
// AtomFeed represents an atom web feed.
type AtomFeed struct {
// XMLName.
XMLName xml.Name `xml:"feed"`
// Universally unique feed ID (required).
ID string `xml:"id"`
// Human readable title for the feed (required).
Title AtomText `xml:"title"`
// Last time the feed was significantly modified (required).
Updated string `xml:"updated"`
// Entries for the feed (required).
Entries []AtomEntry `xml:"entry"`
// Authors of the feed (recommended).
Authors []AtomPerson `xml:"author"`
// Links which identify related web pages (recommended).
Links []AtomLink `xml:"link"`
// Categories the feed belongs to (optional).
Categories []AtomCategory `xml:"category"`
// Contributors to the feed (optional).
Contributors []AtomPerson `xml:"contributor"`
// Software used to generate the feed (optional).
Generator AtomGenerator `xml:"generator"`
// Small icon used for visual identification (optional).
Icon string `xml:"icon"`
// Larger logo for visual identification (optional).
Logo string `xml:"logo"`
// Information about rights, for example copyrights (optional).
Rights AtomText `xml:"rights"`
// Human readable description or subtitle (optional).
Subtitle AtomText `xml:"subtitle"`
}
// AtomEntry represents an atom entry.
type AtomEntry struct {
// Universally unique feed ID (required).
ID string `xml:"id"`
// Human readable title for the entry (required).
Title AtomText `xml:"title"`
// Last time the feed was significantly modified (required).
Updated string `xml:"updated"`
// Authors of the entry (recommended).
Authors []AtomPerson `xml:"author"`
// Content of the entry (recommended).
Content AtomText `xml:"content"`
// Links which identify related web pages (recommended).
Links []AtomLink `xml:"link"`
// Short summary, abstract or excerpt of the entry (recommended).
Summary AtomText `xml:"summary"`
// Categories the entry belongs too (optional).
Categories []AtomCategory `xml:"category"`
// Contributors to the entry (optional).
Contributors []AtomPerson `xml:"contributor"`
// Time of the initial creation of the entry (optional).
Published string `xml:"published"`
// FIXME
// Feed's metadata, only used when entry was copied from another feed (optional).
// Source AtomFeed `xml:"source"`
// Information about rights, for example copyrights (optional).
Rights AtomText `xml:"rights"`
}
// AtomLink represents the atom link tag.
type AtomLink struct {
// Hypertext reference (required).
Href string `xml:"href,attr"`
// Single Link relation type (optional).
Rel string `xml:"rel,attr"`
// Media type of the resource (optional).
Type string `xml:"type,attr"`
// Language of referenced resource (optional).
HrefLang string `xml:"hreflang,attr"`
// Human readable information about the link (optional).
Title string `xml:"title,attr"`
// Length of the resource in bytes (optional).
Length string `xml:"length,attr"`
}
// AtomPerson represents a person, corporation, et cetera.
type AtomPerson struct {
// Human readable name for the person (required).
Name string `xml:"name"`
// Home page for the person (optional).
URI string `xml:"uri"`
// Email address for the person (optional).
Email string `xml:"email"`
}
// AtomCategory identifies the category.
type AtomCategory struct {
// Identifier for this category (required).
Term string `xml:"term,attr"`
// Categorization scheme via a URI (optional).
Scheme string `xml:"scheme,attr"`
// Human readable label for display (optional).
Label string `xml:"label,attr"`
}
// AtomGenerator identifies the generator.
type AtomGenerator struct {
// Generator name (required).
Name string `xml:",chardata"`
// URI for this generator (optional).
URI string `xml:"uri,attr"`
// Version for this generator (optional).
Version string `xml:"version,attr"`
}
// AtomText identifies human readable text.
type AtomText struct {
// Text body (required).
Body string `xml:",chardata"`
// InnerXML data (optional).
InnerXML string `xml:",innerxml"`
// Text type (optional).
Type string `xml:"type,attr"`
// URI where the content can be found (optional for <content>).
URI string `xml:"uri,att"`
}
// parseAtom parses an atom feed and returns a generic feed.
func parseAtom(data []byte) (f Feed, err error) {
var origFeed AtomFeed
if err = unmarshal(data, &origFeed); err != nil {
return
}
f = Feed{
Type: "atom",
Title: origFeed.Title.Body,
Link: findLink(origFeed.Links).Href,
Description: origFeed.Subtitle.Body,
Image: origFeed.Logo,
Generator: origFeed.Generator.Name,
Rights: origFeed.Rights.Body,
}
if len(origFeed.Authors) > 0 {
f.Author = origFeed.Authors[0].Email
}
f.Updated, err = parseTime(origFeed.Updated)
if err != nil {
return
}
for _, category := range origFeed.Categories {
f.Categories = append(f.Categories, category.Term)
}
for _, entry := range origFeed.Entries {
item := Item{
ID: entry.ID,
Title: entry.Title.Body,
Link: findLink(entry.Links).Href,
Content: entry.Content.Body,
Attachment: findAttachment(entry.Links).Href,
}
if len(entry.Authors) > 0 {
item.Author = entry.Authors[0].Email
}
for _, category := range entry.Categories {
item.Categories = append(item.Categories, category.Term)
}
timeStr := entry.Updated
if len(entry.Published) > 0 {
timeStr = entry.Published
}
item.PubDate, err = parseTime(timeStr)
if err != nil {
return
}
f.Items = append(f.Items, item)
}
return
}
// findLink attempts to find the most relevant link.
func findLink(links []AtomLink) AtomLink {
var score int
var match AtomLink
for _, link := range links {
switch {
case link.Rel == "alternate" && link.Type == "text/html":
return link
case score < 3 && link.Type == "text/html":
score = 3
match = link
case score < 2 && link.Rel == "self":
score = 2
match = link
case score < 1 && link.Rel == "":
score = 1
match = link
case &match == nil:
match = link
}
}
return match
}
// findAttachment attempts to find a link which represents an attachment.
func findAttachment(links []AtomLink) AtomLink {
for _, link := range links {
if link.Rel == "enclosure" {
return link
}
}
return AtomLink{}
}