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,12 +3,16 @@ package controllers
import (
"fmt"
"strconv"
"time"
"gitea.tbdevent.eu/TBD/reforger_crawler_main/initializers"
"gitea.tbdevent.eu/TBD/reforger_crawler_main/models"
"github.com/gin-gonic/gin"
)
var lastWebhookTime time.Time
var nbrOfUnsentWebhooks int
func CreateAddon(c *gin.Context) {
var addonR models.Addon
body := make([]byte, 0)
@@ -21,13 +25,62 @@ func CreateAddon(c *gin.Context) {
}
addon := models.Addon{
ID: addonR.ID,
ID: addonR.ID,
}
result := initializers.DB.Where(&addon).First(&addon)
if result.Error != nil {
// addon does not exist, create it
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 {
// addon exists, check version, update it and queue for reindexing if needed
if addon.CurrentVersionNumber != addonR.CurrentVersionNumber {
@@ -45,7 +98,48 @@ func CreateAddon(c *gin.Context) {
addon.Blocked = addonR.Blocked
addon.ToBeIndexed = true
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"})
}