Files
reforger_crawler_main/controllers/scraperController.go
ilbinek e39a92c6d3
All checks were successful
Cross Compile Go / build (push) Successful in 12m11s
updates, dokploy
2026-04-09 01:39:37 +02:00

175 lines
4.1 KiB
Go

package controllers
import (
"bytes"
"fmt"
"io"
"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, err := io.ReadAll(c.Request.Body)
if err != nil {
c.JSON(400, gin.H{"error": "Failed to read request body"})
return
}
fmt.Println(string(body))
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
err = c.ShouldBindJSON(&addonR)
if err != nil {
c.JSON(400, gin.H{"error": "Invalid JSON"})
return
}
addon := models.Addon{
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()
oldNbr := nbrOfUnsentWebhooks
nbrOfUnsentWebhooks = 0
go func() {
webhookURL := initializers.ScraperWebhookURL
if webhookURL != "" {
text := fmt.Sprintf("New %d addons found\nLast: %s (%s).", oldNbr, 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(webhookURL, 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 {
addon.Name = addonR.Name
addon.Type = addonR.Type
addon.Summary = addonR.Summary
addon.Unlisted = addonR.Unlisted
addon.Private = addonR.Private
addon.SubscriberCount = addonR.SubscriberCount
addon.Author = addonR.Author
addon.Preview = addonR.Preview
addon.CurrentVersionNumber = addonR.CurrentVersionNumber
addon.CurrentVersionID = addonR.CurrentVersionID
addon.CurrentVersionSize = addonR.CurrentVersionSize
addon.Blocked = addonR.Blocked
addon.ToBeIndexed = true
initializers.DB.Save(&addon)
// send webhook about update
go func() {
webhookURL := initializers.ScraperWebhookURL
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(webhookURL, myHook)
if err != nil {
fmt.Println("Error sending webhook:", err)
}
}
}()
}
}
c.JSON(200, gin.H{"status": "success"})
}
func GetCurrentState(c *gin.Context) {
offsetStr := c.Query("offset")
limitStr := c.Query("limit")
offset := 0
limit := 0
if offsetStr != "" {
if val, err := strconv.Atoi(offsetStr); err == nil {
offset = val
}
}
if limitStr != "" {
if val, err := strconv.Atoi(limitStr); err == nil {
limit = val
}
}
var addons []models.Addon
initializers.DB.Limit(limit).Offset(offset).Find(&addons)
c.JSON(200, addons)
}