scraper webhooks
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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,6 +98,47 @@ 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"})
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user