-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch.go
91 lines (72 loc) · 2.2 KB
/
fetch.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
package feedreader
import "errors"
import "regexp"
import "github.com/m3ng9i/go-utils/xml"
import httphelper "github.com/m3ng9i/go-utils/http"
import enc "github.com/m3ng9i/go-utils/encoding"
// Fetch feed content from a url, return []byte
// If returned error is not nil, it will be FetchError.
func FetchByte(url string, fetcher ...*httphelper.Fetcher) (data []byte, err error) {
var ft *httphelper.Fetcher
if len(fetcher) > 0 && fetcher[0] != nil {
ft = fetcher[0]
} else {
ft = defaultFetcher
}
if ft == nil {
err = errors.New("Fetcher is nil")
return
}
var e error
data, e = ft.FetchAll(url)
if e != nil {
err = &FetchError{Url: url, Err: e}
return
}
// Check if the first 80 bytes of the xml doc contains 'gbk' or 'gb2312'
// just like: <?xml version="1.0" encoding="gbk"?>, or <?xml version="1.0" encoding="gb2312"?>
// if true, convert the bytes to utf-8.
isGbk, _ := regexp.Match("(?i)gbk|gb2312", data[:80])
if isGbk {
// convert to utf-8
data, err = enc.GbkToUtf8(data)
if err != nil {
return
}
}
// Remove invalid xml characters
data = xml.RemoveInvalidChars(data)
return
}
// fetch feed content from a url, return string
// If returned error is not nil, it will be FetchError.
func FetchString(url string, fetcher ...*httphelper.Fetcher) (s string, err error) {
var ft *httphelper.Fetcher
if len(fetcher) > 0 && fetcher[0] != nil {
ft = fetcher[0]
} else {
ft = defaultFetcher
}
if ft == nil {
err = errors.New("Fetcher is nil")
return
}
data, err := FetchByte(url, ft)
if err != nil {
return
}
s = string(data)
return
}
// Grap rss or atom feed and return a *Feed struct
// If returned error is not nil, it will be FetchError or ParseError.
func Fetch(feedlink string, fetcher ...*httphelper.Fetcher) (feed *Feed, err error) {
// If err is not nil, it will be FetchError.
data, err := FetchByte(feedlink, fetcher...)
if err != nil {
return
}
// If err is not nil, it will be ParseError.
feed, err = Parse(data, feedlink)
return
}