summaryrefslogtreecommitdiff
path: root/app/app.go
blob: 7baa73af112afd88a3394751ed8fe51a5c9b6c6e (plain) (blame)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package app

import (
	"context"
	"fmt"
	"log/slog"
	"os"
	"strings"
	"time"

	tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
	"github.com/microcosm-cc/bluemonday"
	"github.com/mmcdole/gofeed"
)

type App struct {
	config   *Config
	telegram *tgbotapi.BotAPI
}

func New(cfg *Config) (*App, error) {
	bot, err := tgbotapi.NewBotAPI(cfg.Telegram.BotToken)
	if err != nil {
		return nil, err
	}

	return &App{
		config:   cfg,
		telegram: bot,
	}, nil
}

func (a *App) Run(ctx context.Context) error {
	ticker := time.NewTicker(a.config.RSS.CheckInterval)
	if err := a.iteration(); err != nil {
		slog.ErrorContext(ctx, "failed iteration", slog.Any("error", err))
	}
	for {
		select {
		case <-ctx.Done():
			return nil
		case <-ticker.C:
			if err := a.iteration(); err != nil {
				slog.ErrorContext(ctx, "failed iteration", slog.Any("error", err))
			}
		}
	}
}

func (a *App) iteration() error {
	seq, err := a.readSeqFile()
	if os.IsNotExist(err) {
		seq = ""
		err = nil
	}
	if err != nil {
		return err
	}

	items, err := a.findNewItems(seq)
	if err != nil || len(items) == 0 {
		return err
	}

	for i := len(items) - 1; i >= 0; i-- {
		if err := a.processItem(items[i]); err != nil {
			return err
		}
		if err := a.writeSeqFile(items[i].GUID); err != nil {
			return err
		}
	}

	return nil
}

func (a *App) processItem(item *gofeed.Item) error {
	p := bluemonday.Policy{}
	p.AllowStandardURLs()
	p.AllowElements("b", "i", "code", "u", "strike", "pre", "br", "a")
	p.AllowNoAttrs().Globally()
	p.AllowAttrs("href").OnElements("a")
	s := strings.ReplaceAll(item.Description, "<del>", "<strike>")
	s = strings.ReplaceAll(s, "</del>", "</strike>")
	s = strings.ReplaceAll(s, "<h1", "<b")
	s = strings.ReplaceAll(s, "</h1>", "</b>")
	s = strings.ReplaceAll(s, "<h2", "<b")
	s = strings.ReplaceAll(s, "</h2>", "</b>")
	s = strings.ReplaceAll(s, "</p>", "\n")
	s = strings.ReplaceAll(s, "<br />", "\n")
	s = p.Sanitize(s)
	s = strings.Trim(s, "\n\t ")

	str := fmt.Sprintf("<b>%s</b>\n%s", item.Title, s)
	for _, group := range a.config.Telegram.TargetGroups {
		switch {
		case item.Image != nil:
			msg := tgbotapi.NewPhoto(
				group,
				tgbotapi.FileURL(item.Image.URL),
			)
			msg.ParseMode = tgbotapi.ModeHTML
			msg.Caption = str
			if _, err := a.telegram.Send(msg); err != nil {
				return err
			}
		default:
			msg := tgbotapi.NewMessage(group, str)
			msg.ParseMode = tgbotapi.ModeHTML
			if _, err := a.telegram.Send(msg); err != nil {
				return err
			}
		}
		msg := tgbotapi.NewMessage(
			group,
			fmt.Sprintf("Оригинал: %s", item.Link),
		)
		if _, err := a.telegram.Send(msg); err != nil {
			return err
		}
	}

	return nil
}

func (a *App) readSeqFile() (string, error) {
	seqVal, err := os.ReadFile(a.config.RSS.SeqFile)
	if err != nil {
		return "", err
	}

	return string(seqVal), nil
}

func (a *App) writeSeqFile(seqVal string) error {
	return os.WriteFile(a.config.RSS.SeqFile, []byte(seqVal), 0o644)
}

func (a *App) findNewItems(from string) ([]*gofeed.Item, error) {
	fp := gofeed.NewParser()
	feed, err := fp.ParseURL(a.config.RSS.URL)
	if err != nil {
		return nil, err
	}
	out := make([]*gofeed.Item, 0, len(feed.Items))
	for _, item := range feed.Items {
		if item.GUID == from {
			break
		}
		out = append(out, item)
	}

	return out, nil
}