diff --git a/helpers/dbHelpers.go b/helpers/dbHelpers.go index 12668d6..5809ffa 100644 --- a/helpers/dbHelpers.go +++ b/helpers/dbHelpers.go @@ -1 +1,59 @@ -package helpers \ No newline at end of file +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 +} + + + + diff --git a/main.go b/main.go index ac98e0f..1f60865 100644 --- a/main.go +++ b/main.go @@ -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") } diff --git a/test.db b/test.db deleted file mode 100644 index 947c1d1..0000000 Binary files a/test.db and /dev/null differ