summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-12-15 17:37:47 +0300
committerAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-12-15 17:37:47 +0300
commit70565ecac31f7bb5c1d6c559ed18ff9f9c56ac46 (patch)
tree2a45103ab148653d72fd6c33cd39201984509442
parent533c0aeaeb5c674304624ec1024ca0e962a8f3f2 (diff)
Изображение в фиде
-rw-r--r--Dockerfile8
-rw-r--r--app/app.go72
-rw-r--r--app/config.go4
-rw-r--r--go.mod12
-rw-r--r--go.sum20
-rw-r--r--main.go2
-rw-r--r--store/seq.txt1
-rw-r--r--templates/telegram.gotmpl4
8 files changed, 78 insertions, 45 deletions
diff --git a/Dockerfile b/Dockerfile
index 9a87a74..d8878c5 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1
-FROM golang:1.22.4-alpine
+FROM golang:1.23.4-alpine
WORKDIR /app
@@ -9,12 +9,12 @@ RUN go mod download
COPY . .
-RUN CGO_ENABLED=0 GOOS=linux go build -o /rss2world
+RUN CGO_ENABLED=0 GOOS=linux go build -o /posse
-ENV RSS_URL="https://neonxp.ru/feed.atom"
+ENV RSS_URL="https://neonxp.ru/feed/"
ENV CHECK_INTERVAL="5m"
ENV SEQ_FILE="/store/seq.txt"
ENV TELEGRAM_TOKEN="279146841:AAE9Yd2W........_gAhbO3usM"
ENV TELEGRAM_GROUPS="-1001003209449"
-CMD ["/rss2world"] \ No newline at end of file
+CMD ["/posse"] \ No newline at end of file
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"
)
diff --git a/go.mod b/go.mod
index ea89cc8..486eeaf 100644
--- a/go.mod
+++ b/go.mod
@@ -1,4 +1,4 @@
-module gitrepo.ru/neonxp/rss2world
+module git.neonxp.ru/posse
go 1.22.4
@@ -8,13 +8,19 @@ require (
)
require (
+ github.com/aymerick/douceur v0.2.0 // indirect
+ github.com/gorilla/css v1.0.1 // indirect
+)
+
+require (
github.com/PuerkitoBio/goquery v1.8.0 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/json-iterator/go v1.1.12 // indirect
+ github.com/microcosm-cc/bluemonday v1.0.27
github.com/mmcdole/goxpp v1.1.1-0.20240225020742-a0c311522b23 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
- golang.org/x/net v0.4.0 // indirect
- golang.org/x/text v0.5.0 // indirect
+ golang.org/x/net v0.26.0 // indirect
+ golang.org/x/text v0.16.0 // indirect
)
diff --git a/go.sum b/go.sum
index 1e4dd4c..fd94230 100644
--- a/go.sum
+++ b/go.sum
@@ -2,15 +2,22 @@ github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0g
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
+github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
+github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/caarlos0/env/v11 v11.1.0 h1:a5qZqieE9ZfzdvbbdhTalRrHT5vu/4V1/ad1Ka6frhI=
github.com/caarlos0/env/v11 v11.1.0/go.mod h1:LwgkYk1kDvfGpHthrWWLof3Ny7PezzFwS4QrsJdHTMo=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
+github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8=
+github.com/gorilla/css v1.0.1/go.mod h1:BvnYkspnSzMmwRK+b8/xgNPLiIuNZr6vbZBTPQ2A3b0=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
+github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk=
+github.com/microcosm-cc/bluemonday v1.0.27/go.mod h1:jFi9vgW+H7c3V0lb6nR74Ib/DIB5OBs92Dimizgw2cA=
github.com/mmcdole/gofeed v1.3.0 h1:5yn+HeqlcvjMeAI4gu6T+crm7d0anY85+M+v6fIFNG4=
github.com/mmcdole/gofeed v1.3.0/go.mod h1:9TGv2LcJhdXePDzxiuMnukhV2/zb6VtnZt1mS+SjkLE=
github.com/mmcdole/goxpp v1.1.1-0.20240225020742-a0c311522b23 h1:Zr92CAlFhy2gL+V1F+EyIuzbQNbSgP4xhTODZtrXUtk=
@@ -20,16 +27,21 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
+github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
+github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
-golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
-golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
+golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
+golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
-golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
-golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
+golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
+golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/main.go b/main.go
index 017772e..b1f39d5 100644
--- a/main.go
+++ b/main.go
@@ -5,7 +5,7 @@ import (
"os"
"os/signal"
- "gitrepo.ru/neonxp/rss2world/app"
+ "git.neonxp.ru/posse/app"
)
func main() {
diff --git a/store/seq.txt b/store/seq.txt
deleted file mode 100644
index aeb20ea..0000000
--- a/store/seq.txt
+++ /dev/null
@@ -1 +0,0 @@
-https://neonxp.ru/posts/2024/06/02/%D0%BA%D0%BD%D0%B8%D0%B6%D0%BD%D1%8B%D0%B5_%D1%80%D0%B5%D0%BA%D0%BE%D0%BC%D0%B5%D0%BD%D0%B4%D0%B0%D1%86%D0%B8%D0%B8_1/ \ No newline at end of file
diff --git a/templates/telegram.gotmpl b/templates/telegram.gotmpl
index d217731..775322c 100644
--- a/templates/telegram.gotmpl
+++ b/templates/telegram.gotmpl
@@ -3,7 +3,5 @@
{{.Content}}
-<a href="{{.Link}}">Запись полностью и комментарии</a>
-
-Отправлено через <a href="https://git.neonxp.ru/neonxp/rss2world">rss2world</a>
+<a href="{{.Link}}">Перейти к оригиналу</a>
{{end}} \ No newline at end of file