Исправлена ошибка, при которой после таймаута функция NewsText ничего не возвращала и программа падала. Так же теперь лента читается начиная от самого старого поста, заканчивая самым новым.

This commit is contained in:
B4D_US3R 2025-05-04 16:14:34 +05:00
parent 47242e5431
commit a79933b724
8 changed files with 70 additions and 29 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
@ -18,6 +19,7 @@ import (
"gopkg.in/yaml.v2"
)
// Функция создает файл secret.conf, в котором хранятся данные для доступа к аккаунту
func ClientConfiguration(Instance string) {
appConfig := &mastodon.AppConfig{
Server: Instance,
@ -37,7 +39,7 @@ func ClientConfiguration(Instance string) {
log.Println(err)
}
var userToken string
//fmt.Println(u)
fmt.Printf("Перейдите по ссылке\n%s\nИ введите user token ниже:\n", u)
fmt.Scanln(&userToken)
@ -67,24 +69,38 @@ func ClientConfiguration(Instance string) {
}
log.Println(string(marshaledYaml))
secretConfig, err := os.OpenFile("secret.conf", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
if err != nil {
log.Println(err)
}
secretConfig.Write(marshaledYaml)
defer secretConfig.Close()
}
// Возвращает срез новостей, полученных из RSS ленты
func NewsText(url string) []*gofeed.Item {
fp := gofeed.NewParser()
feed, err := fp.ParseURL(url)
if err != nil {
log.Println(err)
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
log.Println("Timeout Error:", err)
return []*gofeed.Item{}
} else {
log.Println(err)
return []*gofeed.Item{}
}
}
log.Println("RSS лента получена")
return feed.Items
}
// Отправляет созданный пост
func CreatePost(mastoClient mastodon.Client, toot mastodon.Toot) {
_, err := mastoClient.PostStatus(context.Background(), &toot)
if err != nil {
@ -92,25 +108,34 @@ func CreatePost(mastoClient mastodon.Client, toot mastodon.Toot) {
}
}
// Обходит срез ссылок на изображения и возвращает срез, состоящий из представления картинок в виде срезов байтов
func PicBytesArray(picturesArray []string) [][]byte {
var picturesBytes [][]byte
for _, picture := range picturesArray {
resp, err := http.Get(picture)
if err != nil {
log.Println(err)
return picturesBytes
}
defer resp.Body.Close()
picBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return picturesBytes
}
picturesBytes = append(picturesBytes, picBytes)
}
return picturesBytes
}
// Загружает на инстанс изображения, после чего возвращает массив прикрепленных медиа в Mastodon. Необходимо для получения MediaID для каждого изображения
func UploadPictures(mastoClient mastodon.Client, filesBytes [][]byte) []*mastodon.Attachment {
var attachments []*mastodon.Attachment
@ -128,6 +153,7 @@ func UploadPictures(mastoClient mastodon.Client, filesBytes [][]byte) []*mastodo
return attachments
}
// Формирование тела статуса
func CreateToot(mastoClient mastodon.Client, newsDesc *gofeed.Item, sensitive bool) (mastodon.Toot, error) {
var imgArray []string
var attachments []*mastodon.Attachment