-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrss.go
45 lines (39 loc) · 1.06 KB
/
rss.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
package rss
import (
"io"
"github.com/luraproject/lura/v2/encoding"
"github.com/mmcdole/gofeed"
)
func Register() error {
return encoding.GetRegister().Register(Name, DecoderFactory)
}
// Name is the key for the rss encoding
const Name = "rss"
func DecoderFactory(_ bool) func(io.Reader, *map[string]interface{}) error { return NewDecoder() }
// NewDecoder returns the RSS decoder
func NewDecoder() func(io.Reader, *map[string]interface{}) error {
fp := gofeed.NewParser()
return func(r io.Reader, v *map[string]interface{}) error {
feed, err := fp.Parse(r)
if err != nil {
return err
}
*(v) = map[string]interface{}{
"items": feed.Items,
"author": feed.Author,
"categories": feed.Categories,
"custom": feed.Custom,
"copyright": feed.Copyright,
"description": feed.Description,
"type": feed.FeedType,
"language": feed.Language,
"title": feed.Title,
"published": feed.Published,
"updated": feed.Updated,
}
if feed.Image != nil {
(*v)["img_url"] = feed.Image.URL
}
return nil
}
}