From 70565ecac31f7bb5c1d6c559ed18ff9f9c56ac46 Mon Sep 17 00:00:00 2001 From: Alexander Neonxp Kiryukhin Date: Sun, 15 Dec 2024 17:37:47 +0300 Subject: Изображение в фиде MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/app.go | 72 +++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 27 deletions(-) (limited to 'app/app.go') diff --git a/app/app.go b/app/app.go index 28fefe0..756bad2 100644 --- a/app/app.go +++ b/app/app.go @@ -5,14 +5,15 @@ import ( "context" "log/slog" "os" - "regexp" "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" - "gitrepo.ru/neonxp/rss2world/templates" ) type App struct { @@ -41,6 +42,9 @@ func New(cfg *Config) (*App, error) { 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(): @@ -82,16 +86,49 @@ func (a *App) iteration() error { func (a *App) processItem(item *gofeed.Item) error { buf := bytes.NewBufferString("") - item.Content = trimHtml(item.Description) + + 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, "", "") + s = strings.ReplaceAll(s, "", "") + s = strings.ReplaceAll(s, "", "") + s = strings.ReplaceAll(s, "", "") + s = strings.ReplaceAll(s, "
", "\n") + s = strings.ReplaceAll(s, "<p>", "") + s = strings.ReplaceAll(s, "</p>", "") + item.Content = p.Sanitize(s) + if err := a.templates.ExecuteTemplate(buf, "telegram", item); err != nil { return err } - str := buf.String() + 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") + } for _, group := range a.config.Telegram.TargetGroups { - msg := tgbotapi.NewMessage(group, str) - msg.ParseMode = tgbotapi.ModeHTML - if _, err := a.telegram.Send(msg); err != nil { - return err + 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 + } } } @@ -127,22 +164,3 @@ func (a *App) findNewItems(from string) ([]*gofeed.Item, error) { return out, nil } - -func trimHtml(src string) string { - // Convert all HTML tags to lowercase - re, _ := regexp.Compile("\\<[\\S\\s]+?\\>") - src = re.ReplaceAllStringFunc(src, strings.ToLower) - // Remove STYLE - re, _ = regexp.Compile("\\") - src = re.ReplaceAllString(src, "") - // Remove SCRIPT - re, _ = regexp.Compile("\\") - src = re.ReplaceAllString(src, "") - // Remove all HTML code in angle brackets and replace them with newline characters - re, _ = regexp.Compile("\\<[\\S\\s]+?\\>") - src = re.ReplaceAllString(src, "\n") - // Remove consecutive newlines - re, _ = regexp.Compile("\\s{2,}") - src = re.ReplaceAllString(src, "\n") - return strings.TrimSpace(src) -} -- cgit v1.2.3