Изменил логику обработки тутов, теперь автоматом отправяется и текст в посте, и картинки
This commit is contained in:
parent
c5caeecde8
commit
d99d47e4e1
3 changed files with 43 additions and 45 deletions
27
.gitignore
vendored
27
.gitignore
vendored
|
@ -1,3 +1,28 @@
|
||||||
secret.conf
|
secret.conf
|
||||||
go.sum
|
go.sum
|
||||||
config.yaml
|
config.yaml
|
||||||
|
|
||||||
|
### Go ###
|
||||||
|
# If you prefer the allow list template instead of the deny list, see community template:
|
||||||
|
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||||
|
#
|
||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, built with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
||||||
|
|
||||||
|
# Dependency directories (remove the comment below to include it)
|
||||||
|
# vendor/
|
||||||
|
|
||||||
|
# Go workspace file
|
||||||
|
go.work
|
||||||
|
|
||||||
|
# End of https://www.toptal.com/developers/gitignore/api/go
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
instance: pleroma.catgirls.asia
|
|
||||||
rss_url: https://pleroma.catgirls.asia/users/SiberiaBread/feed.atom
|
|
59
main.go
59
main.go
|
@ -33,7 +33,7 @@ type KikiSettings struct {
|
||||||
RSSUri string `yaml:"rss_url,omitempty"`
|
RSSUri string `yaml:"rss_url,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDataFromConfig(path string) *mastodon.Config {
|
func getSecrets(path string) *mastodon.Config {
|
||||||
var clientData MastodonClientData
|
var clientData MastodonClientData
|
||||||
secretConfig, err := os.ReadFile(path)
|
secretConfig, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -154,16 +154,7 @@ func newsText(url string) []*gofeed.Item {
|
||||||
return feed.Items
|
return feed.Items
|
||||||
}
|
}
|
||||||
|
|
||||||
func createPost(statusText string) {
|
func createPost(mastoClient mastodon.Client, toot mastodon.Toot) {
|
||||||
config := getDataFromConfig("secret.conf")
|
|
||||||
|
|
||||||
mastoClient := mastodon.NewClient(config)
|
|
||||||
|
|
||||||
toot := mastodon.Toot{
|
|
||||||
Status: statusText,
|
|
||||||
Visibility: "unlisted",
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := mastoClient.PostStatus(context.Background(), &toot)
|
_, err := mastoClient.PostStatus(context.Background(), &toot)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
@ -189,10 +180,8 @@ func picBytesArray(picturesArray []string) [][]byte {
|
||||||
return picturesBytes
|
return picturesBytes
|
||||||
}
|
}
|
||||||
|
|
||||||
func uploadPictures(filesBytes [][]byte) []*mastodon.Attachment {
|
func uploadPictures(mastoClient mastodon.Client, filesBytes [][]byte) []*mastodon.Attachment {
|
||||||
config := getDataFromConfig("secret.conf")
|
|
||||||
var attachments []*mastodon.Attachment
|
var attachments []*mastodon.Attachment
|
||||||
mastoClient := mastodon.NewClient(config)
|
|
||||||
for _, file := range filesBytes {
|
for _, file := range filesBytes {
|
||||||
att, err := mastoClient.UploadMediaFromBytes(context.Background(), file)
|
att, err := mastoClient.UploadMediaFromBytes(context.Background(), file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -204,25 +193,6 @@ func uploadPictures(filesBytes [][]byte) []*mastodon.Attachment {
|
||||||
return attachments
|
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() {
|
func main() {
|
||||||
cmd := &cli.Command{
|
cmd := &cli.Command{
|
||||||
Name: "kiki",
|
Name: "kiki",
|
||||||
|
@ -256,21 +226,26 @@ func main() {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
kikiConfig := getKikiConfig(confFile)
|
kikiConfig := getKikiConfig(confFile)
|
||||||
|
mastoClient := mastodon.NewClient(getSecrets("secret.conf"))
|
||||||
ticker := time.NewTicker(1 * time.Minute)
|
ticker := time.NewTicker(1 * time.Minute)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
news := newsText(kikiConfig.RSSUri)
|
news := newsText(kikiConfig.RSSUri)
|
||||||
if news[0].GUID != lastGUID {
|
if news[0].GUID != lastGUID {
|
||||||
if !hasHTMLTags(news[0].Description) {
|
toot := mastodon.Toot{
|
||||||
log.Println(news[0].Description)
|
Visibility: "unlisted",
|
||||||
createPost(news[0].Description)
|
Sensitive: true,
|
||||||
lastGUID = news[0].GUID
|
|
||||||
log.Println("Пост отправлен")
|
|
||||||
} else {
|
|
||||||
attachments := uploadPictures(picBytesArray(returnImgArray(news[0].Description)))
|
|
||||||
postWithPictures(attachments)
|
|
||||||
lastGUID = news[0].GUID
|
|
||||||
}
|
}
|
||||||
|
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
|
return nil
|
||||||
|
|
Loading…
Add table
Reference in a new issue