1 | package main
|
2 |
|
3 | import (
|
4 | "context"
|
5 | "errors"
|
6 | "os"
|
7 |
|
8 | miniflux "miniflux.app/client"
|
9 | )
|
10 |
|
11 | var _ Source = &MinifluxFeed{}
|
12 |
|
13 | type MinifluxFeed struct {
|
14 | name string
|
15 | endpoint string
|
16 | feedId int64
|
17 | language Language
|
18 | client *miniflux.Client
|
19 | }
|
20 |
|
21 | func (m *MinifluxFeed) getClient() error {
|
22 | token := os.Getenv("MINIFLUX_SECRET_TOKEN")
|
23 | if token == "" {
|
24 | return errors.New("MINIFLUX_SECRET_TOKEN not set")
|
25 | }
|
26 | m.client = miniflux.New(m.endpoint, token)
|
27 | return nil
|
28 | }
|
29 |
|
30 | func (m *MinifluxFeed) Name() string {
|
31 | return m.name
|
32 | }
|
33 |
|
34 | func (m *MinifluxFeed) Language() Language {
|
35 | return m.language
|
36 | }
|
37 |
|
38 | func (m *MinifluxFeed) Len(ctx context.Context) (int, error) {
|
39 | if m.client == nil {
|
40 | if err := m.getClient(); err != nil {
|
41 | return -1, err
|
42 | }
|
43 | }
|
44 | entries, err := m.client.FeedEntries(m.feedId, &miniflux.Filter{Status: miniflux.EntryStatusUnread})
|
45 | if err != nil {
|
46 | return -1, err
|
47 | }
|
48 | return entries.Total, nil
|
49 | }
|
50 |
|
51 | func (m *MinifluxFeed) Get(ctx context.Context, i int) (*Article, error) {
|
52 | if m.client == nil {
|
53 | if err := m.getClient(); err != nil {
|
54 | return nil, err
|
55 | }
|
56 | }
|
57 | entries, err := m.client.FeedEntries(m.feedId, &miniflux.Filter{
|
58 | Status: miniflux.EntryStatusUnread, Direction: "desc", Order: "published_at"})
|
59 | if err != nil {
|
60 | return nil, err
|
61 | }
|
62 | entry := entries.Entries[i]
|
63 | if err := m.client.UpdateEntries([]int64{entry.ID}, "read"); err != nil {
|
64 | return nil, err
|
65 | }
|
66 | return &Article{raw: extractText(entry.Content)}, nil
|
67 | }
|