Files
CatsOfMastodonGo/main.go

63 lines
1.6 KiB
Go

package main
import (
"CatsOfMastodonBotGo/helpers"
"CatsOfMastodonBotGo/models"
"context"
"log"
"time"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func main() {
var tag = "catsofmastodon"
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 {
panic(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 existingPostIds = helpers.GetExistingPostIds(db)
var existingAccountIds = helpers.GetExistingAccountIds(db)
var newAccounts = helpers.GetNewAccounts(existingAccountIds, posts)
var newPosts = helpers.GetNewPosts(existingPostIds, posts)
log.Println("Number of existing posts: ", len(existingPostIds))
log.Println("Number of existing accounts: ", len(existingAccountIds))
log.Println("Number of new posts: ", len(newPosts))
log.Println("Number of new accounts: ", len(newAccounts))
if newAccounts != nil {
log.Println("Number of inserted accounts: ", db.Create(&newAccounts).RowsAffected)
} else {
log.Println("No new accounts inserted")
}
if newPosts != nil {
log.Println("Number of inserted posts: ", db.Create(&newPosts).RowsAffected)
} 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
}