summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/app.go72
-rw-r--r--app/config.go4
2 files changed, 47 insertions, 29 deletions
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, "<del>", "<strike>")
+ s = strings.ReplaceAll(s, "</del>", "</strike>")
+ s = strings.ReplaceAll(s, "<h1", "<b")
+ s = strings.ReplaceAll(s, "</h1>", "</b>")
+ s = strings.ReplaceAll(s, "<h2", "<b")
+ s = strings.ReplaceAll(s, "</h2>", "</b>")
+ s = strings.ReplaceAll(s, "<br />", "\n")
+ s = strings.ReplaceAll(s, "&lt;p&gt;", "")
+ s = strings.ReplaceAll(s, "&lt;/p&gt;", "")
+ 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("\\<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)
-}
diff --git a/app/config.go b/app/config.go
index 66531aa..093400f 100644
--- a/app/config.go
+++ b/app/config.go
@@ -1,8 +1,8 @@
package app
import (
- "gitrepo.ru/neonxp/rss2world/internal/rss"
- "gitrepo.ru/neonxp/rss2world/internal/telegram"
+ "git.neonxp.ru/posse/internal/rss"
+ "git.neonxp.ru/posse/internal/telegram"
"github.com/caarlos0/env/v11"
)