diff options
author | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-11-17 02:13:37 +0300 |
---|---|---|
committer | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-11-17 02:32:40 +0300 |
commit | 533c0aeaeb5c674304624ec1024ca0e962a8f3f2 (patch) | |
tree | 8380d72f3ec72e4221be04df7a4ef564ff462ea4 /app/app.go | |
parent | 08f01de98e72746c84a584d4ba8afd35edf5bd85 (diff) |
Полный контент в записи
Diffstat (limited to 'app/app.go')
-rw-r--r-- | app/app.go | 28 |
1 files changed, 25 insertions, 3 deletions
@@ -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("\\<style[\\S\\s]+?\\</style\\>") + src = re.ReplaceAllString(src, "") + // Remove SCRIPT + re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>") + 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) +} |