Gosource_rss.go -rw-r--r-- 951 B
1package main
2
3import (
4 "context"
5
6 "github.com/mmcdole/gofeed"
7)
8
9var _ Source = &RssFeed{}
10
11type RssFeed struct {
12 name string
13 url string
14 content string
15 language Language
16 feed *gofeed.Feed
17}
18
19func (r *RssFeed) Name() string {
20 return r.name
21}
22
23func (r *RssFeed) Language() Language {
24 return r.language
25}
26
27func (r *RssFeed) getFeed(ctx context.Context) error {
28 fp := gofeed.NewParser()
29 feed, err := fp.ParseURLWithContext(r.url, ctx)
30 if err != nil {
31 return err
32 }
33 r.feed = feed
34 return nil
35}
36
37func (r RssFeed) Len(ctx context.Context) (int, error) {
38 if r.feed == nil {
39 if err := r.getFeed(ctx); err != nil {
40 return -1, err
41 }
42 }
43 return r.feed.Len(), nil
44}
45
46func (r *RssFeed) Get(ctx context.Context, i int) (*Article, error) {
47 if r.feed == nil {
48 if err := r.getFeed(ctx); err != nil {
49 return nil, err
50 }
51 }
52 item := r.feed.Items[i]
53 textOnly := extractText(item.Content)
54 return &Article{raw: textOnly}, nil
55}