105 lines
2.3 KiB
Go
105 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"kiki/src/config"
|
|
"kiki/src/stacker"
|
|
"kiki/src/tooter"
|
|
|
|
"github.com/mattn/go-mastodon"
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
func main() {
|
|
cmd := &cli.Command{
|
|
Name: "kiki",
|
|
Usage: "Ретранслятор из RSS в Mastodon. Когда-нибудь...",
|
|
Commands: []*cli.Command{
|
|
{
|
|
Name: "init",
|
|
Usage: "Инициализировать клиента",
|
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
|
confFile, err := filepath.Abs("config.yaml")
|
|
kikiConfig := config.GetKikiConfig(confFile)
|
|
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
instanceUrlParser, err := url.Parse(kikiConfig.Instance)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
instanceUrlParser.Scheme = "https"
|
|
|
|
tooter.ClientConfiguration(instanceUrlParser.String())
|
|
|
|
return nil
|
|
},
|
|
},
|
|
{
|
|
Name: "run",
|
|
Usage: "Запуск транслятора",
|
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
|
mastoClient := mastodon.NewClient(config.GetSecrets("secret.conf"))
|
|
|
|
confFile, err := filepath.Abs("config.yaml")
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
kikiConfig := config.GetKikiConfig(confFile)
|
|
|
|
rdb := stacker.ConnectToRedis(kikiConfig.Redis.Address)
|
|
|
|
ticker := time.NewTicker(1 * time.Minute)
|
|
defer ticker.Stop()
|
|
for range ticker.C {
|
|
|
|
newPosts := tooter.NewsText(kikiConfig.RSSUri)
|
|
|
|
for _, post := range newPosts {
|
|
inStack, err := stacker.CheckInRedis(rdb, post.GUID)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
if !inStack {
|
|
log.Println(post.Description)
|
|
|
|
toot, err := tooter.CreateToot(*mastoClient, post.Description)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
tooter.CreatePost(*mastoClient, toot)
|
|
|
|
stacker.SetToRedis(rdb, post.GUID, post.Description)
|
|
}
|
|
}
|
|
/*if news[0].GUID != lastGUID {
|
|
log.Println(news[0].Description)
|
|
|
|
toot, err := tooter.CreateToot(*mastoClient, news[0].Description)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
tooter.CreatePost(*mastoClient, toot)
|
|
lastGUID = news[0].GUID
|
|
}*/
|
|
}
|
|
return nil
|
|
},
|
|
},
|
|
},
|
|
}
|
|
if err := cmd.Run(context.Background(), os.Args); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|