Files
CatsOfMastodonGo/main.go
2025-05-10 20:35:16 +03:30

81 lines
1.9 KiB
Go

package main
import (
"CatsOfMastodonBotGo/helpers"
"CatsOfMastodonBotGo/models"
"context"
"log"
"time"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func main() {
var tag = "cat"
var instance = "https://haminoa.net"
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err, posts := helpers.GetPosts(ctx, tag, instance)
if err != nil {
log.Fatal(err)
}
log.Println("Number of fetched posts: ", len(posts))
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
// Migrate the schema
db.AutoMigrate(&models.Post{}, &models.MediaAttachment{}, &models.Account{})
var existingAccounts []string
var existingPosts []string
db.Model(&models.Account{}).Pluck("acc_id", &existingAccounts)
db.Model(&models.Post{}).Pluck("id", &existingPosts)
log.Println("Number of existing accounts in the database: ", len(existingAccounts))
log.Println("Number of existing posts in the database: ", len(existingPosts))
var newPosts []models.Post = nil
for _, post := range posts {
if !arrayContains(existingPosts, post.ID) {
newPosts = append(newPosts, post)
}
}
log.Println("Number of new posts: ", len(newPosts))
var newAccounts []models.Account = nil
accountSet := make(map[string]bool)
for _, post := range newPosts {
if _, value := accountSet[post.Account.AccId]; !value {
accountSet[post.Account.AccId] = true
newAccounts = append(newAccounts, post.Account)
}
}
log.Println("Number of new accounts: ", len(newAccounts))
if newAccounts != nil {
db.Create(&newAccounts)
} else {
log.Println("No new accounts inserted")
}
if newPosts != nil {
db.Create(&newPosts)
} else {
log.Println("No new posts inserted")
}
}
func arrayContains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}