From 20e1b992484ae8bd8f7e7395ffe4b852f006840b Mon Sep 17 00:00:00 2001 From: Sotirios Pupakis Date: Thu, 25 Sep 2025 00:46:13 +0200 Subject: [PATCH] scraper webhooks --- config.yaml | 1 + controllers/scraperController.go | 98 +++++++++++++++++++++++++++++++- initializers/initialize.go | 5 +- 3 files changed, 101 insertions(+), 3 deletions(-) diff --git a/config.yaml b/config.yaml index e8c8121..1223c88 100644 --- a/config.yaml +++ b/config.yaml @@ -3,4 +3,5 @@ ip: "0.0.0.0" secret: "secret" db: "crawler.db" discordWebhook: "https://discord.com/api/webhooks/1413673169792925787/bfbCJ8wiFeKsYkLGCp1U8sr5jOOWDcF0NP9DsHuGXy8NjrfKOncYWJbPnROophTXr-kH" +scraperWebhook: "https://discord.com/api/webhooks/1420535203725840407/4lBMKhp0abqgie_9Mx0uDUERBCuitjfeguleMyq1JJts0JiUmw5GR4VO-A5iGEq7NFV0" adminSecret: "123456789" diff --git a/controllers/scraperController.go b/controllers/scraperController.go index 6150bcb..2593702 100644 --- a/controllers/scraperController.go +++ b/controllers/scraperController.go @@ -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"}) } diff --git a/initializers/initialize.go b/initializers/initialize.go index 36840d1..b818b96 100644 --- a/initializers/initialize.go +++ b/initializers/initialize.go @@ -17,6 +17,7 @@ var SECRET string var DB_NAME string var DiscordWebhook string var ADMIN_SECRET string +var ScraperWebhook string func ConnectToDB() { db, err := gorm.Open(sqlite.Open(DB_NAME), &gorm.Config{}) @@ -35,7 +36,8 @@ type Configuration struct { Secret string `yaml:"secret"` DB string `yaml:"db"` DiscordWebhook string `yaml:"discordWebhook"` - ADMIN_SECRET string `yaml:"adminSecret"` + ADMIN_SECRET string `yaml:"adminSecret"` + ScraperWebhook string `yaml:"scraperWebhook"` } func Load() { @@ -58,4 +60,5 @@ file, err := os.ReadFile("config.yaml") DB_NAME = configuration.DB DiscordWebhook = configuration.DiscordWebhook ADMIN_SECRET = configuration.ADMIN_SECRET + ScraperWebhook = configuration.ScraperWebhook }