2023-06-15 17:03:57 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.nefrace.ru/nefrace/nechotron"
|
|
|
|
"git.nefrace.ru/nefrace/tongo"
|
|
|
|
"github.com/NicoNex/echotron/v3"
|
|
|
|
"github.com/mmcdole/gofeed"
|
|
|
|
)
|
|
|
|
|
|
|
|
var text_template = `
|
|
|
|
[*%s*](%s)
|
2023-06-16 17:00:50 +03:00
|
|
|
_%s_
|
2023-06-15 17:03:57 +03:00
|
|
|
|
|
|
|
%s
|
|
|
|
`
|
|
|
|
|
|
|
|
func RSSTask(api *echotron.API) {
|
|
|
|
for {
|
|
|
|
ParseRSS(api)
|
|
|
|
time.Sleep(5 * time.Minute)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseRSS(api *echotron.API) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
feedChats := tongo.NewStore[FeedChat](db)
|
|
|
|
feeds := tongo.NewStore[Feeds](db)
|
|
|
|
lastFeed, err := feeds.GetOne(ctx)
|
|
|
|
if err != nil || lastFeed == nil {
|
|
|
|
lastFeed = &Feeds{
|
|
|
|
Item: tongo.NewID(),
|
|
|
|
}
|
|
|
|
feeds.ReplaceItem(ctx, *lastFeed, true)
|
|
|
|
}
|
|
|
|
fp := gofeed.NewParser()
|
|
|
|
feed, err := fp.ParseURL("https://godotengine.org/rss.xml")
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Can't get feed: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
item := feed.Items[0]
|
|
|
|
if lastFeed.Published.Compare(*item.PublishedParsed) == -1 {
|
|
|
|
chats, _ := feedChats.GetMany(ctx)
|
|
|
|
if len(chats) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2023-06-16 17:00:50 +03:00
|
|
|
title := item.Title
|
|
|
|
author := "by " + item.Author.Name
|
|
|
|
desc := item.Description
|
|
|
|
translator := InitTranslator()
|
|
|
|
translates, err := translator.Translate("ru", item.Title, "by "+item.Author.Name, item.Description)
|
|
|
|
if err == nil {
|
|
|
|
title = translates[0]
|
|
|
|
author = translates[1]
|
|
|
|
desc = translates[2]
|
|
|
|
}
|
2023-06-15 17:03:57 +03:00
|
|
|
text := fmt.Sprintf(text_template,
|
2023-06-16 17:00:50 +03:00
|
|
|
nechotron.EscapeMd2(title),
|
2023-06-15 17:03:57 +03:00
|
|
|
item.Link,
|
2023-06-16 17:00:50 +03:00
|
|
|
nechotron.EscapeMd2(author),
|
|
|
|
nechotron.EscapeMd2(desc),
|
2023-06-15 17:03:57 +03:00
|
|
|
)
|
|
|
|
sent := false
|
|
|
|
for _, chat := range chats {
|
|
|
|
_, err := api.SendPhoto(echotron.NewInputFileURL(item.Custom["image"]), chat.ChatID, nechotron.NewOptions().MarkdownV2().Caption(text).PhotoOptions())
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Can't send message to", chat.Title, ": ", err)
|
|
|
|
} else {
|
|
|
|
sent = true
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
if !sent {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lastFeed.Published = *item.PublishedParsed
|
|
|
|
lastFeed.Title = item.Title
|
|
|
|
feeds.ReplaceItem(ctx, *lastFeed, true)
|
|
|
|
}
|
|
|
|
}
|