-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslator.go
42 lines (33 loc) · 846 Bytes
/
translator.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
package main
import (
"fmt"
"github.com/mmcdole/gofeed"
"github.com/mmcdole/gofeed/rss"
)
type CustomTranslator struct {
defaultRSSTranslator *gofeed.DefaultRSSTranslator
}
func NewCustomTranslator() *CustomTranslator {
t := &CustomTranslator{}
t.defaultRSSTranslator = &gofeed.DefaultRSSTranslator{}
return t
}
func (ct *CustomTranslator) Translate(feed interface{}) (*gofeed.Feed, error) {
rssFeed, found := feed.(*rss.Feed)
if !found {
return nil, fmt.Errorf("feed did not match expected type of *rss.Feed")
}
f, err := ct.defaultRSSTranslator.Translate(rssFeed)
if err != nil {
return nil, err
}
for i, item := range rssFeed.Items {
if item.Comments != "" {
if f.Items[i].Custom == nil {
f.Items[i].Custom = map[string]string{}
}
f.Items[i].Custom["comments"] = item.Comments
}
}
return f, nil
}