scraper webhooks

This commit is contained in:
Sotirios Pupakis
2025-09-25 00:46:13 +02:00
parent 1da3dbad27
commit 20e1b99248
3 changed files with 101 additions and 3 deletions

View File

@@ -3,4 +3,5 @@ ip: "0.0.0.0"
secret: "secret" secret: "secret"
db: "crawler.db" db: "crawler.db"
discordWebhook: "https://discord.com/api/webhooks/1413673169792925787/bfbCJ8wiFeKsYkLGCp1U8sr5jOOWDcF0NP9DsHuGXy8NjrfKOncYWJbPnROophTXr-kH" discordWebhook: "https://discord.com/api/webhooks/1413673169792925787/bfbCJ8wiFeKsYkLGCp1U8sr5jOOWDcF0NP9DsHuGXy8NjrfKOncYWJbPnROophTXr-kH"
scraperWebhook: "https://discord.com/api/webhooks/1420535203725840407/4lBMKhp0abqgie_9Mx0uDUERBCuitjfeguleMyq1JJts0JiUmw5GR4VO-A5iGEq7NFV0"
adminSecret: "123456789" adminSecret: "123456789"

View File

@@ -3,12 +3,16 @@ package controllers
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"time"
"gitea.tbdevent.eu/TBD/reforger_crawler_main/initializers" "gitea.tbdevent.eu/TBD/reforger_crawler_main/initializers"
"gitea.tbdevent.eu/TBD/reforger_crawler_main/models" "gitea.tbdevent.eu/TBD/reforger_crawler_main/models"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
var lastWebhookTime time.Time
var nbrOfUnsentWebhooks int
func CreateAddon(c *gin.Context) { func CreateAddon(c *gin.Context) {
var addonR models.Addon var addonR models.Addon
body := make([]byte, 0) body := make([]byte, 0)
@@ -21,13 +25,62 @@ func CreateAddon(c *gin.Context) {
} }
addon := models.Addon{ addon := models.Addon{
ID: addonR.ID, ID: addonR.ID,
} }
result := initializers.DB.Where(&addon).First(&addon) result := initializers.DB.Where(&addon).First(&addon)
if result.Error != nil { if result.Error != nil {
// addon does not exist, create it // addon does not exist, create it
initializers.DB.Create(&addonR) initializers.DB.Create(&addonR)
// send webhook about creation
if time.Since(lastWebhookTime) < time.Second*10 {
nbrOfUnsentWebhooks++
return
}
lastWebhookTime = time.Now()
nbrOfUnsentWebhooks = 0
go func() {
webhookURL := initializers.ScraperWebhook
if webhookURL != "" {
text := fmt.Sprintf("New %d addons found\nLast: %s (%s).", nbrOfUnsentWebhooks, addon.ID, addon.Name)
colour := 2228479
size := fmt.Sprintf("%.2f MB", float64(addon.CurrentVersionSize)/1_000_000)
myEmbed := models.CustomEmbed{
Title: text,
Color: colour,
Timestamp: time.Now(),
Image: models.CustomImage{
URL: addon.Preview,
},
Fields: []models.CustomEmbedField{
{
Name: "Size",
Value: size,
Inline: true,
},
{
Name: "Current Version",
Value: addon.CurrentVersionNumber,
Inline: true,
},
},
}
myHook := models.CustomHook{
Username: "Reforger Crawler",
Embeds: []models.CustomEmbed{myEmbed},
}
err := SendCustomWebhook(initializers.DiscordWebhook, myHook)
if err != nil {
fmt.Println("Error sending webhook:", err)
}
}
}()
} else { } else {
// addon exists, check version, update it and queue for reindexing if needed // addon exists, check version, update it and queue for reindexing if needed
if addon.CurrentVersionNumber != addonR.CurrentVersionNumber { if addon.CurrentVersionNumber != addonR.CurrentVersionNumber {
@@ -45,6 +98,47 @@ func CreateAddon(c *gin.Context) {
addon.Blocked = addonR.Blocked addon.Blocked = addonR.Blocked
addon.ToBeIndexed = true addon.ToBeIndexed = true
initializers.DB.Save(&addon) initializers.DB.Save(&addon)
// send webhook about update
go func() {
webhookURL := initializers.ScraperWebhook
if webhookURL != "" {
text := fmt.Sprintf("Addon: %s (%s) updated", addon.ID, addon.Name)
colour := 2228479
size := fmt.Sprintf("%.2f MB", float64(addon.CurrentVersionSize)/1_000_000)
myEmbed := models.CustomEmbed{
Title: text,
Color: colour,
Timestamp: time.Now(),
Image: models.CustomImage{
URL: addon.Preview,
},
Fields: []models.CustomEmbedField{
{
Name: "Size",
Value: size,
Inline: true,
},
{
Name: "Current Version",
Value: addon.CurrentVersionNumber,
Inline: true,
},
},
}
myHook := models.CustomHook{
Username: "Reforger Crawler",
Embeds: []models.CustomEmbed{myEmbed},
}
err := SendCustomWebhook(initializers.DiscordWebhook, myHook)
if err != nil {
fmt.Println("Error sending webhook:", err)
}
}
}()
} }
} }
c.JSON(200, gin.H{"status": "success"}) c.JSON(200, gin.H{"status": "success"})

View File

@@ -17,6 +17,7 @@ var SECRET string
var DB_NAME string var DB_NAME string
var DiscordWebhook string var DiscordWebhook string
var ADMIN_SECRET string var ADMIN_SECRET string
var ScraperWebhook string
func ConnectToDB() { func ConnectToDB() {
db, err := gorm.Open(sqlite.Open(DB_NAME), &gorm.Config{}) db, err := gorm.Open(sqlite.Open(DB_NAME), &gorm.Config{})
@@ -35,7 +36,8 @@ type Configuration struct {
Secret string `yaml:"secret"` Secret string `yaml:"secret"`
DB string `yaml:"db"` DB string `yaml:"db"`
DiscordWebhook string `yaml:"discordWebhook"` DiscordWebhook string `yaml:"discordWebhook"`
ADMIN_SECRET string `yaml:"adminSecret"` ADMIN_SECRET string `yaml:"adminSecret"`
ScraperWebhook string `yaml:"scraperWebhook"`
} }
func Load() { func Load() {
@@ -58,4 +60,5 @@ file, err := os.ReadFile("config.yaml")
DB_NAME = configuration.DB DB_NAME = configuration.DB
DiscordWebhook = configuration.DiscordWebhook DiscordWebhook = configuration.DiscordWebhook
ADMIN_SECRET = configuration.ADMIN_SECRET ADMIN_SECRET = configuration.ADMIN_SECRET
ScraperWebhook = configuration.ScraperWebhook
} }