Compare commits
2 Commits
7f567a7adb
...
0533151c1b
Author | SHA1 | Date | |
---|---|---|---|
0533151c1b | |||
e3f2f3199d |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
*.db
|
||||||
|
*.sqlite
|
@@ -1 +1,59 @@
|
|||||||
package helpers
|
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
38
main.go
@@ -6,19 +6,18 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/driver/sqlite"
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var tag = "cat"
|
var tag = "catsofmastodon"
|
||||||
var instance = "https://haminoa.net"
|
var instance = "https://haminoa.net"
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err, posts := helpers.GetPosts(ctx, tag, instance)
|
err, posts := helpers.GetPosts(ctx, tag, instance)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
log.Println("Number of fetched posts: ", len(posts))
|
log.Println("Number of fetched posts: ", len(posts))
|
||||||
|
|
||||||
@@ -30,40 +29,23 @@ func main() {
|
|||||||
// Migrate the schema
|
// Migrate the schema
|
||||||
db.AutoMigrate(&models.Post{}, &models.MediaAttachment{}, &models.Account{})
|
db.AutoMigrate(&models.Post{}, &models.MediaAttachment{}, &models.Account{})
|
||||||
|
|
||||||
var existingAccounts []string
|
var existingPostIds = helpers.GetExistingPostIds(db)
|
||||||
var existingPosts []string
|
var existingAccountIds = helpers.GetExistingAccountIds(db)
|
||||||
db.Model(&models.Account{}).Pluck("acc_id", &existingAccounts)
|
var newAccounts = helpers.GetNewAccounts(existingAccountIds, posts)
|
||||||
db.Model(&models.Post{}).Pluck("id", &existingPosts)
|
var newPosts = helpers.GetNewPosts(existingPostIds, posts)
|
||||||
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
|
log.Println("Number of existing posts: ", len(existingPostIds))
|
||||||
for _, post := range posts {
|
log.Println("Number of existing accounts: ", len(existingAccountIds))
|
||||||
|
|
||||||
if !arrayContains(existingPosts, post.ID) {
|
|
||||||
newPosts = append(newPosts, post)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
log.Println("Number of new posts: ", len(newPosts))
|
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))
|
log.Println("Number of new accounts: ", len(newAccounts))
|
||||||
|
|
||||||
if newAccounts != nil {
|
if newAccounts != nil {
|
||||||
db.Create(&newAccounts)
|
log.Println("Number of inserted accounts: ", db.Create(&newAccounts).RowsAffected)
|
||||||
} else {
|
} else {
|
||||||
log.Println("No new accounts inserted")
|
log.Println("No new accounts inserted")
|
||||||
}
|
}
|
||||||
if newPosts != nil {
|
if newPosts != nil {
|
||||||
db.Create(&newPosts)
|
log.Println("Number of inserted posts: ", db.Create(&newPosts).RowsAffected)
|
||||||
} else {
|
} else {
|
||||||
log.Println("No new posts inserted")
|
log.Println("No new posts inserted")
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user