Изменил логику обработки тутов, теперь автоматом отправяется и текст в посте, и картинки

This commit is contained in:
B4D_US3R 2025-04-10 19:29:33 +05:00
parent c5caeecde8
commit d99d47e4e1
3 changed files with 43 additions and 45 deletions

59
main.go
View file

@ -33,7 +33,7 @@ type KikiSettings struct {
RSSUri string `yaml:"rss_url,omitempty"`
}
func getDataFromConfig(path string) *mastodon.Config {
func getSecrets(path string) *mastodon.Config {
var clientData MastodonClientData
secretConfig, err := os.ReadFile(path)
if err != nil {
@ -154,16 +154,7 @@ func newsText(url string) []*gofeed.Item {
return feed.Items
}
func createPost(statusText string) {
config := getDataFromConfig("secret.conf")
mastoClient := mastodon.NewClient(config)
toot := mastodon.Toot{
Status: statusText,
Visibility: "unlisted",
}
func createPost(mastoClient mastodon.Client, toot mastodon.Toot) {
_, err := mastoClient.PostStatus(context.Background(), &toot)
if err != nil {
log.Println(err)
@ -189,10 +180,8 @@ func picBytesArray(picturesArray []string) [][]byte {
return picturesBytes
}
func uploadPictures(filesBytes [][]byte) []*mastodon.Attachment {
config := getDataFromConfig("secret.conf")
func uploadPictures(mastoClient mastodon.Client, filesBytes [][]byte) []*mastodon.Attachment {
var attachments []*mastodon.Attachment
mastoClient := mastodon.NewClient(config)
for _, file := range filesBytes {
att, err := mastoClient.UploadMediaFromBytes(context.Background(), file)
if err != nil {
@ -204,25 +193,6 @@ func uploadPictures(filesBytes [][]byte) []*mastodon.Attachment {
return attachments
}
func postWithPictures(attachments []*mastodon.Attachment) {
var mediaID []mastodon.ID
config := getDataFromConfig("secret.conf")
mastoClient := mastodon.NewClient(config)
for _, attach := range attachments {
mediaID = append(mediaID, attach.ID)
}
toot := mastodon.Toot{
MediaIDs: mediaID,
Visibility: "unlisted",
Sensitive: true,
}
_, err := mastoClient.PostStatus(context.Background(), &toot)
if err != nil {
log.Println(err)
}
}
func main() {
cmd := &cli.Command{
Name: "kiki",
@ -256,21 +226,26 @@ func main() {
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 {
if !hasHTMLTags(news[0].Description) {
log.Println(news[0].Description)
createPost(news[0].Description)
lastGUID = news[0].GUID
log.Println("Пост отправлен")
} else {
attachments := uploadPictures(picBytesArray(returnImgArray(news[0].Description)))
postWithPictures(attachments)
lastGUID = news[0].GUID
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)
}
}
createPost(*mastoClient, toot)
}
}
return nil