From 533c0aeaeb5c674304624ec1024ca0e962a8f3f2 Mon Sep 17 00:00:00 2001 From: Alexander Neonxp Kiryukhin Date: Sun, 17 Nov 2024 02:13:37 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=BD=D1=8B=D0=B9=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BD=D1=82=D0=B5=D0=BD=D1=82=20=D0=B2=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/app.go | 28 +++++++++++++++++++++++++--- templates/telegram.gotmpl | 2 +- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/app/app.go b/app/app.go index 405a4e9..28fefe0 100644 --- a/app/app.go +++ b/app/app.go @@ -5,6 +5,8 @@ import ( "context" "log/slog" "os" + "regexp" + "strings" "text/template" "time" @@ -52,7 +54,6 @@ func (a *App) Run(ctx context.Context) error { } func (a *App) iteration() error { - seq, err := a.readSeqFile() if os.IsNotExist(err) { seq = "" @@ -81,11 +82,13 @@ func (a *App) iteration() error { func (a *App) processItem(item *gofeed.Item) error { buf := bytes.NewBufferString("") + item.Content = trimHtml(item.Description) if err := a.templates.ExecuteTemplate(buf, "telegram", item); err != nil { return err } + str := buf.String() for _, group := range a.config.Telegram.TargetGroups { - msg := tgbotapi.NewMessage(group, buf.String()) + msg := tgbotapi.NewMessage(group, str) msg.ParseMode = tgbotapi.ModeHTML if _, err := a.telegram.Send(msg); err != nil { return err @@ -105,7 +108,7 @@ func (a *App) readSeqFile() (string, error) { } func (a *App) writeSeqFile(seqVal string) error { - return os.WriteFile(a.config.RSS.SeqFile, []byte(seqVal), 0644) + return os.WriteFile(a.config.RSS.SeqFile, []byte(seqVal), 0o644) } func (a *App) findNewItems(from string) ([]*gofeed.Item, error) { @@ -124,3 +127,22 @@ 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) +} diff --git a/templates/telegram.gotmpl b/templates/telegram.gotmpl index cd98d7a..d217731 100644 --- a/templates/telegram.gotmpl +++ b/templates/telegram.gotmpl @@ -1,5 +1,5 @@ {{define "telegram"}} -{{.Title}} +{{.Title}} {{.Content}}