Полный контент в записи

This commit is contained in:
Александр Кирюхин 2024-11-17 02:13:37 +03:00
parent 08f01de98e
commit 533c0aeaeb
Signed by: neonxp
SSH key fingerprint: SHA256:SVt7TjxbVc87m1QYaQziOJ0N3OCFURv2g76gD/UTTXI
2 changed files with 26 additions and 4 deletions

View file

@ -5,6 +5,8 @@ import (
"context" "context"
"log/slog" "log/slog"
"os" "os"
"regexp"
"strings"
"text/template" "text/template"
"time" "time"
@ -52,7 +54,6 @@ func (a *App) Run(ctx context.Context) error {
} }
func (a *App) iteration() error { func (a *App) iteration() error {
seq, err := a.readSeqFile() seq, err := a.readSeqFile()
if os.IsNotExist(err) { if os.IsNotExist(err) {
seq = "" seq = ""
@ -81,11 +82,13 @@ func (a *App) iteration() error {
func (a *App) processItem(item *gofeed.Item) error { func (a *App) processItem(item *gofeed.Item) error {
buf := bytes.NewBufferString("") buf := bytes.NewBufferString("")
item.Content = trimHtml(item.Description)
if err := a.templates.ExecuteTemplate(buf, "telegram", item); err != nil { if err := a.templates.ExecuteTemplate(buf, "telegram", item); err != nil {
return err return err
} }
str := buf.String()
for _, group := range a.config.Telegram.TargetGroups { for _, group := range a.config.Telegram.TargetGroups {
msg := tgbotapi.NewMessage(group, buf.String()) msg := tgbotapi.NewMessage(group, str)
msg.ParseMode = tgbotapi.ModeHTML msg.ParseMode = tgbotapi.ModeHTML
if _, err := a.telegram.Send(msg); err != nil { if _, err := a.telegram.Send(msg); err != nil {
return err return err
@ -105,7 +108,7 @@ func (a *App) readSeqFile() (string, error) {
} }
func (a *App) writeSeqFile(seqVal 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) { 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 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)
}

View file

@ -1,5 +1,5 @@
{{define "telegram"}} {{define "telegram"}}
<a href="{{.Link}}">{{.Title}}</a> <b>{{.Title}}</b>
{{.Content}} {{.Content}}