/
atom.go
78 lines (67 loc) · 1.61 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
package feed
import (
"bytes"
"encoding/xml"
"github.com/capric98/t-rss/unit"
"golang.org/x/net/html/charset"
)
// AtomFeed :)
type AtomFeed struct {
// Required
Title string `xml:"title"`
Link string `xml:"href,attr"`
Description string `xml:"description"`
// Optional
PubDate string `xml:"updated"`
Generator string `xml:"generator"`
Items []AtomItem `xml:"entry"`
}
// AtomItem :)
type AtomItem struct {
Title string `xml:"title"`
Link string `xml:"-"`
Description string `xml:"subtitle"`
Author string `xml:"author"`
Category struct {
Domain string `xml:"term,attr"`
Name string `xml:"label,attr"`
} `xml:"category"`
Comments string `xml:"-"`
Enclosure struct {
URL string `xml:"rel,attr"`
Len int64 `xml:"-"`
Type string `xml:"href,attr"`
} `xml:"link"`
GUID struct {
IsPermaLink bool `xml:"-"`
Value string `xml:",chardata"`
} `xml:"id"`
SpubDate string `xml:"updated"`
Source string `xml:"-"`
}
func parseAtom(body []byte) (f []Item, e error) {
var feed []AtomFeed
decoder := xml.NewDecoder(bytes.NewReader(body))
decoder.CharsetReader = charset.NewReaderLabel
e = decoder.Decode(&feed)
if e != nil {
return
}
for k := range feed {
for _, v := range feed[k].Items {
f = append(f, Item{
Title: v.Title,
Link: v.Link,
Description: v.Description,
Author: v.Author,
URL: v.Enclosure.URL,
Len: v.Enclosure.Len,
Type: v.Enclosure.Type,
GUID: v.GUID.Value,
Date: unit.ParseTime(v.SpubDate),
Source: v.Source,
})
}
}
return
}