From 4d1d1360dc3263151865d1ae2a31f63fbd20fc40 Mon Sep 17 00:00:00 2001 From: B4D_US3R Date: Fri, 11 Apr 2025 17:13:54 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C=D1=88?= =?UTF-8?q?=D0=BE=D0=B9=20=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D0=BD=D0=B3,=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B8=D0=BD=D1=86=D0=B8=D0=BF?= =?UTF-8?q?=D0=B0=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=81=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 106 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 67 insertions(+), 39 deletions(-) diff --git a/main.go b/main.go index 41a3c2d..6b00be9 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,6 @@ import ( "net/url" "os" "path/filepath" - "regexp" "strings" "time" @@ -18,7 +17,6 @@ import ( "github.com/mmcdole/gofeed" "github.com/urfave/cli/v3" "golang.org/x/net/html" - "golang.org/x/net/html/atom" ) type MastodonClientData struct { @@ -35,14 +33,17 @@ type KikiSettings struct { func getSecrets(path string) *mastodon.Config { var clientData MastodonClientData + secretConfig, err := os.ReadFile(path) if err != nil { log.Println(err) } + err = yaml.Unmarshal(secretConfig, &clientData) if err != nil { log.Println(err) } + config := &mastodon.Config{ Server: clientData.Instance, ClientID: clientData.ClientID, @@ -53,40 +54,19 @@ func getSecrets(path string) *mastodon.Config { return config } -func returnImgArray(htmlText string) []string { - var imgArray []string - parser, err := html.Parse(strings.NewReader(htmlText)) - if err != nil { - log.Println(err) - } - for n := range parser.Descendants() { - if n.Type == html.ElementNode && n.DataAtom == atom.Img { - for _, img := range n.Attr { - if img.Key == "src" { - imgArray = append(imgArray, img.Val) - } - } - } - } - - return imgArray -} - -func hasHTMLTags(s string) bool { - re := regexp.MustCompile(`<(?i)[a-z][a-z0-9]*[^>]*>`) - return re.MatchString(s) -} - func getKikiConfig(path string) KikiSettings { var kikiSettings KikiSettings + kikiConfigFile, err := os.ReadFile(path) if err != nil { log.Println(err) } + err = yaml.Unmarshal(kikiConfigFile, &kikiSettings) if err != nil { log.Println(err) } + return kikiSettings } @@ -98,6 +78,7 @@ func clientConfiguration(Instance string) { Website: "catgirls.asia", RedirectURIs: "urn:ietf:wg:oauth:2.0:oob", } + app, err := mastodon.RegisterApp(context.Background(), appConfig) if err != nil { log.Println(err) @@ -135,6 +116,7 @@ func clientConfiguration(Instance string) { if err != nil { log.Println(err) } + log.Println(string(marshaledYaml)) secretConfig, err := os.OpenFile("secret.conf", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644) if err != nil { @@ -182,17 +164,62 @@ func picBytesArray(picturesArray []string) [][]byte { func uploadPictures(mastoClient mastodon.Client, filesBytes [][]byte) []*mastodon.Attachment { var attachments []*mastodon.Attachment + for _, file := range filesBytes { att, err := mastoClient.UploadMediaFromBytes(context.Background(), file) + if err != nil { log.Println(err) return attachments } + attachments = append(attachments, att) } + return attachments } +func createToot(mastoClient mastodon.Client, newsDesc string) (mastodon.Toot, error) { + var tootText string + var imgArray []string + var attachments []*mastodon.Attachment + + toot := mastodon.Toot{ + Visibility: "unlisted", + Sensitive: true, + } + + uString := html.UnescapeString(newsDesc) + pHtml, err := html.Parse(strings.NewReader(uString)) + if err != nil { + return mastodon.Toot{}, err + } + + for n := range pHtml.Descendants() { + if n.Type != html.ElementNode { + tootText += (n.Data + "\n") + } + if n.Type == html.ElementNode && n.Data == "img" { + for _, attr := range n.Attr { + if attr.Key == "src" { + imgArray = append(imgArray, attr.Val) + } + } + } + } + + if len(imgArray) != 0 { + attachments = uploadPictures(mastoClient, picBytesArray(imgArray)) + } + + toot.Status = tootText + for _, attach := range attachments { + toot.MediaIDs = append(toot.MediaIDs, attach.ID) + } + + return toot, nil +} + func main() { cmd := &cli.Command{ Name: "kiki", @@ -203,16 +230,20 @@ func main() { Usage: "Инициализировать клиента", Action: func(ctx context.Context, cmd *cli.Command) error { confFile, err := filepath.Abs("config.yaml") + kikiConfig := getKikiConfig(confFile) + if err != nil { log.Println(err) } - kikiConfig := getKikiConfig(confFile) + instanceUrlParser, err := url.Parse(kikiConfig.Instance) if err != nil { log.Println(err) } instanceUrlParser.Scheme = "https" + clientConfiguration(instanceUrlParser.String()) + return nil }, }, @@ -221,31 +252,28 @@ func main() { Usage: "Запуск транслятора", Action: func(ctx context.Context, cmd *cli.Command) error { var lastGUID string + mastoClient := mastodon.NewClient(getSecrets("secret.conf")) + confFile, err := filepath.Abs("config.yaml") if err != nil { log.Println(err) } kikiConfig := getKikiConfig(confFile) - mastoClient := mastodon.NewClient(getSecrets("secret.conf")) + ticker := time.NewTicker(1 * time.Minute) defer ticker.Stop() for range ticker.C { news := newsText(kikiConfig.RSSUri) if news[0].GUID != lastGUID { - toot := mastodon.Toot{ - Visibility: "unlisted", - Sensitive: true, - } log.Println(news[0].Description) - toot.Status = news[0].Description - lastGUID = news[0].GUID - if hasHTMLTags(news[0].Description) { - attachments := uploadPictures(*mastoClient, picBytesArray(returnImgArray(news[0].Description))) - for _, attach := range attachments { - toot.MediaIDs = append(toot.MediaIDs, attach.ID) - } + + toot, err := createToot(*mastoClient, news[0].Description) + if err != nil { + log.Println(err) } + createPost(*mastoClient, toot) + lastGUID = news[0].GUID } } return nil