Compare commits

...

2 Commits

Author SHA1 Message Date
0533151c1b Refactor database operations and move helpers to separate package 2025-05-10 21:33:50 +03:30
e3f2f3199d Added .gitignore 2025-05-10 21:33:28 +03:30
4 changed files with 71 additions and 29 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
*.db
*.sqlite

View File

@@ -1 +1,59 @@
package helpers
import (
"CatsOfMastodonBotGo/models"
"gorm.io/gorm"
)
func GetExistingPostIds(db *gorm.DB) []string {
var existingPostIds []string
db.Model(&models.Post{}).Pluck("id", &existingPostIds)
return existingPostIds
}
func GetExistingAccountIds(db *gorm.DB) []string {
var existingAccountIds []string
db.Model(&models.Account{}).Pluck("acc_id", &existingAccountIds)
return existingAccountIds
}
func GetNewPosts(existingPostIds []string, posts []models.Post) []models.Post {
var newPosts []models.Post = nil
for _, post := range posts {
if !arrayContains(existingPostIds, post.ID) {
newPosts = append(newPosts, post)
}
}
return newPosts
}
func GetNewAccounts(existingAccountIds []string, posts []models.Post) []models.Account {
var newAccounts []models.Account = nil
for _, post := range posts {
if !arrayContains(existingAccountIds, post.Account.AccId) {
newAccounts = append(newAccounts, post.Account)
}
}
return newAccounts
}
func InsertNewPosts(db *gorm.DB, newPosts []models.Post) int{
return int(db.Create(&newPosts).RowsAffected)
}
func InsertNewAccounts(db *gorm.DB, newAccounts []models.Account) int{
return int(db.Create(&newAccounts).RowsAffected)
}
func arrayContains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}

38
main.go
View File

@@ -6,19 +6,18 @@ import (
"context"
"log"
"time"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func main() {
var tag = "cat"
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 {
log.Fatal(err)
panic(err)
}
log.Println("Number of fetched posts: ", len(posts))
@@ -30,40 +29,23 @@ func main() {
// 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 existingPostIds = helpers.GetExistingPostIds(db)
var existingAccountIds = helpers.GetExistingAccountIds(db)
var newAccounts = helpers.GetNewAccounts(existingAccountIds, posts)
var newPosts = helpers.GetNewPosts(existingPostIds, posts)
var newPosts []models.Post = nil
for _, post := range posts {
if !arrayContains(existingPosts, post.ID) {
newPosts = append(newPosts, post)
}
}
log.Println("Number of existing posts: ", len(existingPostIds))
log.Println("Number of existing accounts: ", len(existingAccountIds))
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)
log.Println("Number of inserted accounts: ", db.Create(&newAccounts).RowsAffected)
} else {
log.Println("No new accounts inserted")
}
if newPosts != nil {
db.Create(&newPosts)
log.Println("Number of inserted posts: ", db.Create(&newPosts).RowsAffected)
} else {
log.Println("No new posts inserted")
}

BIN
test.db

Binary file not shown.