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