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 | |
parent | 70565ecac31f7bb5c1d6c559ed18ff9f9c56ac46 (diff) |
-rw-r--r-- | app/app.go | 52 | ||||
-rw-r--r-- | go.mod | 13 | ||||
-rw-r--r-- | go.sum | 4 | ||||
-rw-r--r-- | templates/fs.go | 6 | ||||
-rw-r--r-- | templates/telegram.gotmpl | 7 |
5 files changed, 27 insertions, 55 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 @@ -3,21 +3,18 @@ module git.neonxp.ru/posse go 1.22.4 require ( - github.com/caarlos0/env/v11 v11.1.0 + github.com/caarlos0/env/v11 v11.3.0 + github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 + github.com/microcosm-cc/bluemonday v1.0.27 github.com/mmcdole/gofeed v1.3.0 ) require ( - github.com/aymerick/douceur v0.2.0 // indirect - github.com/gorilla/css v1.0.1 // indirect -) - -require ( github.com/PuerkitoBio/goquery v1.8.0 // indirect github.com/andybalholm/cascadia v1.3.1 // indirect - github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 + github.com/aymerick/douceur v0.2.0 // indirect + github.com/gorilla/css v1.0.1 // indirect github.com/json-iterator/go v1.1.12 // indirect - github.com/microcosm-cc/bluemonday v1.0.27 github.com/mmcdole/goxpp v1.1.1-0.20240225020742-a0c311522b23 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect @@ -4,8 +4,8 @@ github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x0 github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk= github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= -github.com/caarlos0/env/v11 v11.1.0 h1:a5qZqieE9ZfzdvbbdhTalRrHT5vu/4V1/ad1Ka6frhI= -github.com/caarlos0/env/v11 v11.1.0/go.mod h1:LwgkYk1kDvfGpHthrWWLof3Ny7PezzFwS4QrsJdHTMo= +github.com/caarlos0/env/v11 v11.3.0 h1:CVTN6W6+twFC1jHKUwsw9eOTEiFpzyJOSA2AyHa8uvw= +github.com/caarlos0/env/v11 v11.3.0/go.mod h1:Q5lYHeOsgY20CCV/R+b50Jwg2MnjySid7+3FUBz2BJw= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/templates/fs.go b/templates/fs.go deleted file mode 100644 index 197a5e1..0000000 --- a/templates/fs.go +++ /dev/null @@ -1,6 +0,0 @@ -package templates - -import "embed" - -//go:embed *.gotmpl -var Templates embed.FS diff --git a/templates/telegram.gotmpl b/templates/telegram.gotmpl deleted file mode 100644 index 775322c..0000000 --- a/templates/telegram.gotmpl +++ /dev/null @@ -1,7 +0,0 @@ -{{define "telegram"}} -<b>{{.Title}}</b> - -{{.Content}} - -<a href="{{.Link}}">Перейти к оригиналу</a> -{{end}}
\ No newline at end of file |