-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.go
73 lines (66 loc) · 1.7 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
package main
import (
"encoding/xml"
"io/ioutil"
"path/filepath"
"regexp"
)
type rssRoot struct {
XMLName xml.Name `xml:"rss"`
Version string `xml:"version,attr"`
XmlnsItunes string `xml:"xmlns:itunes,attr"`
Channel rssChannel `xml:"channel"`
}
type rssChannel struct {
ID string `xml:"id,attr"`
Title string `xml:"title"`
Author string `xml:"author"`
Description string `xml:"description"`
Image rssImage `xml:"image"`
Language string `xml:"language"`
Link string `xml:"link"`
Items []rssItem `xml:"item"`
}
type rssImage struct {
URL string `xml:"url"`
Title string `xml:"title"`
Link string `xml:"link"`
}
type rssItem struct {
Title string `xml:"title"`
Description string `xml:"description"`
PubDate string `xml:"pubDate"`
Enclosure rssEnclosure `xml:"enclosure"`
Duration string `xml:"itunes:duration"`
GUID string `xml:"guid"`
}
type rssEnclosure struct {
URL string `xml:"url,attr"`
Type string `xml:"type,attr"`
Length int64 `xml:"length,attr"`
}
func getCurrentFeeds() ([]rssRoot, error) {
var results []rssRoot
files, err := ioutil.ReadDir(cfg.DataDir)
if err != nil {
return results, err
}
re := regexp.MustCompile("^feed-[a-z0-9]+\\.xml$")
for _, file := range files {
fileName := file.Name()
if !re.MatchString(fileName) {
continue
}
xmlString, err := ioutil.ReadFile(filepath.Join(cfg.DataDir, file.Name()))
if err != nil {
return results, err
}
var xmlObj rssRoot
err = xml.Unmarshal(xmlString, &xmlObj)
if err != nil {
return results, err
}
results = append(results, xmlObj)
}
return results, nil
}