-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss2go.go
88 lines (74 loc) · 2.1 KB
/
rss2go.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
package rss2go
import (
"encoding/xml"
"fmt"
"io"
"net/http"
"regexp"
)
type Feed struct {
XMLName xml.Name `xml:"rss"`
Channel Channel `xml:"channel"`
}
type Image struct {
XMLName xml.Name `xml:"image"`
Url string `xml:"url"`
Title string `xml:"title"`
Link string `xml:"link"`
Width uint16 `xml:"width"`
Height uint16 `xml:"height"`
}
type MediaContent struct {
XMLName xml.Name `xml:"content"`
Url string `xml:"url,attr"`
}
type Item struct {
XMLName xml.Name `xml:"item"`
Title string `xml:"title"`
Link string `xml:"link"`
Guid string `xml:"guid"`
Description string `xml:"description"`
MediaContent MediaContent `xml:"content"`
Category string `xml:"category"`
PubDate string `xml:"pubDate"`
}
type AtomLink struct {
XMLName xml.Name `xml:"http://www.w3.org/2005/Atom link"`
Href string `xml:"href,attr"`
Rel string `xml:"rel,attr"`
Type string `xml:"type,attr"`
}
type Channel struct {
XMLName xml.Name `xml:"channel"`
Title string `xml:"title"`
// Link string `xml:"link"` TODO: To solve -> It conflicts with AtomLink (<atom:link />)
Description string `xml:"description"`
Language string `xml:"language"`
Copyright string `xml:"copyright"`
AtomLink AtomLink `xml:"http://www.w3.org/2005/Atom link"`
Image Image `xml:"image"`
Items []Item `xml:"item"`
}
func Rss2Go(feedUrl string) (*Feed, error) {
var feed Feed
resp, err := http.Get(feedUrl)
if err != nil {
return nil, fmt.Errorf("GET error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status error: %v", resp.StatusCode)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read body: %v", err)
}
re := regexp.MustCompile(`<!\[CDATA\[(.*?)\]\]>(\n*|\s*)`)
newData := re.ReplaceAllString(string(data), "")
data = []byte(newData)
err = xml.Unmarshal(data, &feed)
if err != nil {
return nil, fmt.Errorf("something went wrong on unmarshal xml: %v", err)
}
return &feed, nil
}