diff options
author | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-12-17 20:51:07 +0300 |
---|---|---|
committer | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-12-17 20:51:07 +0300 |
commit | 12cd485899f55058339faeca0f8523fbb0d2b334 (patch) | |
tree | d059341d5fb86d5f8ef80c366f167fd448651425 /app/app.go | |
parent | 70565ecac31f7bb5c1d6c559ed18ff9f9c56ac46 (diff) |
Diffstat (limited to 'app/app.go')
-rw-r--r-- | app/app.go | 52 |
1 files changed, 20 insertions, 32 deletions
@@ -1,25 +1,21 @@ package app import ( - "bytes" "context" + "fmt" "log/slog" "os" "strings" - "text/template" "time" - "git.neonxp.ru/posse/templates" - 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 - templates *template.Template + config *Config + telegram *tgbotapi.BotAPI } func New(cfg *Config) (*App, error) { @@ -28,15 +24,9 @@ func New(cfg *Config) (*App, error) { return nil, err } - tpl, err := template.ParseFS(templates.Templates, "*.gotmpl") - if err != nil { - return nil, err - } - return &App{ - config: cfg, - telegram: bot, - templates: tpl, + config: cfg, + telegram: bot, }, nil } @@ -85,8 +75,6 @@ func (a *App) iteration() error { } func (a *App) processItem(item *gofeed.Item) error { - buf := bytes.NewBufferString("") - p := bluemonday.Policy{} p.AllowStandardURLs() p.AllowElements("b", "i", "code", "u", "strike", "pre", "br", "a") @@ -98,26 +86,19 @@ func (a *App) processItem(item *gofeed.Item) error { 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 = strings.ReplaceAll(s, "<p>", "") - s = strings.ReplaceAll(s, "</p>", "") - item.Content = p.Sanitize(s) + s = p.Sanitize(s) + s = strings.Trim(s, "\n\t ") - if err := a.templates.ExecuteTemplate(buf, "telegram", item); err != nil { - return err - } - str := "" - str2 := buf.String() - for str != str2 { - str = str2 - str2 = strings.ReplaceAll(str, "\n\n", "\n") - str2 = strings.ReplaceAll(str2, " ", " ") - str2 = strings.Trim(str2, " \t\n") - } + 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 := tgbotapi.NewPhoto( + group, + tgbotapi.FileURL(item.Image.URL), + ) msg.ParseMode = tgbotapi.ModeHTML msg.Caption = str if _, err := a.telegram.Send(msg); err != nil { @@ -130,6 +111,13 @@ func (a *App) processItem(item *gofeed.Item) error { return err } } + msg := tgbotapi.NewMessage( + group, + fmt.Sprintf("Оригинал: %s", item.Link), + ) + if _, err := a.telegram.Send(msg); err != nil { + return err + } } return nil |