Compare commits
2 Commits
0533151c1b
...
8be6290635
Author | SHA1 | Date | |
---|---|---|---|
8be6290635 | |||
5a897b5412 |
@@ -2,9 +2,14 @@ package helpers
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func AddMigrations(db *gorm.DB) {
|
||||
db.AutoMigrate(&models.Post{}, &models.MediaAttachment{}, &models.Account{})
|
||||
}
|
||||
|
||||
func GetExistingPostIds(db *gorm.DB) []string {
|
||||
var existingPostIds []string
|
||||
db.Model(&models.Post{}).Pluck("id", &existingPostIds)
|
||||
@@ -16,11 +21,10 @@ func GetExistingAccountIds(db *gorm.DB) []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) {
|
||||
if !arrayContains(existingPostIds, post.ID) && len(post.Attachments) > 0 && !post.Account.IsBot {
|
||||
newPosts = append(newPosts, post)
|
||||
}
|
||||
}
|
||||
@@ -37,11 +41,11 @@ func GetNewAccounts(existingAccountIds []string, posts []models.Post) []models.A
|
||||
return newAccounts
|
||||
}
|
||||
|
||||
func InsertNewPosts(db *gorm.DB, newPosts []models.Post) int{
|
||||
func InsertNewPosts(db *gorm.DB, newPosts []models.Post) int {
|
||||
return int(db.Create(&newPosts).RowsAffected)
|
||||
}
|
||||
|
||||
func InsertNewAccounts(db *gorm.DB, newAccounts []models.Account) int{
|
||||
func InsertNewAccounts(db *gorm.DB, newAccounts []models.Account) int {
|
||||
return int(db.Create(&newAccounts).RowsAffected)
|
||||
}
|
||||
|
||||
@@ -53,7 +57,3 @@ func arrayContains(arr []string, str string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
35
main.go
35
main.go
@@ -2,17 +2,25 @@ package main
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/helpers"
|
||||
"CatsOfMastodonBotGo/models"
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var tag = "catsofmastodon"
|
||||
var instance = "https://haminoa.net"
|
||||
var tag = os.Getenv("COM_TAG")
|
||||
if tag == "" {
|
||||
tag = "catsofmastodon"
|
||||
}
|
||||
var instance = os.Getenv("COM_INSTANCE")
|
||||
if instance == "" {
|
||||
instance = "https://haminoa.net"
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
err, posts := helpers.GetPosts(ctx, tag, instance)
|
||||
@@ -21,18 +29,18 @@ func main() {
|
||||
}
|
||||
log.Println("Number of fetched posts: ", len(posts))
|
||||
|
||||
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
|
||||
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{Logger: logger.Default.LogMode(logger.Warn)})
|
||||
if err != nil {
|
||||
panic("failed to connect database")
|
||||
}
|
||||
|
||||
// Migrate the schema
|
||||
db.AutoMigrate(&models.Post{}, &models.MediaAttachment{}, &models.Account{})
|
||||
helpers.AddMigrations(db)
|
||||
|
||||
var existingPostIds = helpers.GetExistingPostIds(db)
|
||||
var existingAccountIds = helpers.GetExistingAccountIds(db)
|
||||
var newAccounts = helpers.GetNewAccounts(existingAccountIds, posts)
|
||||
var newPosts = helpers.GetNewPosts(existingPostIds, posts)
|
||||
var newAccounts = helpers.GetNewAccounts(existingAccountIds, newPosts)
|
||||
|
||||
|
||||
log.Println("Number of existing posts: ", len(existingPostIds))
|
||||
log.Println("Number of existing accounts: ", len(existingAccountIds))
|
||||
@@ -40,23 +48,14 @@ func main() {
|
||||
log.Println("Number of new accounts: ", len(newAccounts))
|
||||
|
||||
if newAccounts != nil {
|
||||
log.Println("Number of inserted accounts: ", db.Create(&newAccounts).RowsAffected)
|
||||
log.Println("Number of inserted accounts: ", helpers.InsertNewAccounts(db, newAccounts))
|
||||
} else {
|
||||
log.Println("No new accounts inserted")
|
||||
}
|
||||
if newPosts != nil {
|
||||
log.Println("Number of inserted posts: ", db.Create(&newPosts).RowsAffected)
|
||||
log.Println("Number of inserted posts: ", helpers.InsertNewPosts(db, 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
|
||||
}
|
||||
|
Reference in New Issue
Block a user