2023-07-03 15:30:14 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023 Laura Kalb <dev@lauka.net>
|
|
|
|
* The code of this project is available under the MIT license. See the LICENSE file for more info.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2024-11-06 11:57:26 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
|
2023-07-04 16:56:09 +02:00
|
|
|
"codeberg.org/lauralani/humble-bot/constants"
|
|
|
|
"codeberg.org/lauralani/humble-bot/db"
|
2023-07-03 15:30:14 +02:00
|
|
|
"codeberg.org/lauralani/humble-bot/misc"
|
|
|
|
"codeberg.org/lauralani/humble-bot/models"
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
2023-07-04 16:56:09 +02:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-07-03 15:30:14 +02:00
|
|
|
)
|
|
|
|
|
2023-07-04 16:56:09 +02:00
|
|
|
func UpdateBundles() {
|
|
|
|
log.Debug().Str("func", "UpdateBundles").Msg("starting bundle run")
|
2023-07-03 15:30:14 +02:00
|
|
|
|
2023-07-04 16:56:09 +02:00
|
|
|
for _, category := range constants.HumbleCategories {
|
|
|
|
log.Debug().Str("category", category).Msg("starting bundle update")
|
|
|
|
endpoint, _ := url.Parse("https://www.humblebundle.com/" + category)
|
2023-07-03 15:30:14 +02:00
|
|
|
|
2023-07-04 16:56:09 +02:00
|
|
|
client := misc.CustomHttpClient()
|
|
|
|
req := misc.CustomHttpRequest()
|
|
|
|
req.URL = endpoint
|
|
|
|
req.Method = "GET"
|
|
|
|
|
|
|
|
response, err := client.Do(req)
|
2023-07-03 15:30:14 +02:00
|
|
|
if err != nil {
|
2023-07-04 16:56:09 +02:00
|
|
|
log.Error().Str("category", category).Str("status", response.Status).
|
|
|
|
Str("func", "app.UpdateBundles").Msgf("HTTP Error: %q", err)
|
2023-07-03 15:30:14 +02:00
|
|
|
}
|
2023-07-04 16:56:09 +02:00
|
|
|
defer func(Body io.ReadCloser) {
|
|
|
|
err = Body.Close()
|
2023-07-03 15:30:14 +02:00
|
|
|
if err != nil {
|
2023-07-04 16:56:09 +02:00
|
|
|
log.Error().Str("func", "app.UpdateBundles").Msg(err.Error())
|
2023-07-03 15:30:14 +02:00
|
|
|
}
|
2023-07-04 16:56:09 +02:00
|
|
|
}(response.Body)
|
2023-07-03 15:30:14 +02:00
|
|
|
|
2023-07-04 16:56:09 +02:00
|
|
|
document, err := goquery.NewDocumentFromReader(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Str("func", "app.UpdateBundles").Msg(err.Error())
|
|
|
|
}
|
2023-07-03 15:30:14 +02:00
|
|
|
|
2023-07-04 16:56:09 +02:00
|
|
|
document.Find("script#landingPage-json-data").Each(func(idx int, s *goquery.Selection) {
|
|
|
|
node := s.Nodes[0]
|
|
|
|
data := node.FirstChild.Data
|
|
|
|
bundles, err := parseBundles([]byte(data), category)
|
2023-07-03 15:30:14 +02:00
|
|
|
if err != nil {
|
2023-07-04 16:56:09 +02:00
|
|
|
log.Error().Str("func", "UpdateBundles").Msg(err.Error())
|
2023-07-03 15:30:14 +02:00
|
|
|
}
|
|
|
|
|
2023-07-04 16:56:09 +02:00
|
|
|
for _, bundle := range bundles {
|
|
|
|
err := handleHumbleBundle(bundle, category)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Str("bundle", bundle.MachineName).
|
|
|
|
Msgf("Error handling humble bundle: %v", err)
|
|
|
|
}
|
2023-07-03 15:30:14 +02:00
|
|
|
}
|
2024-11-06 11:57:26 +01:00
|
|
|
|
|
|
|
log.Info().Int("bundles", len(bundles)).Msg("finished queueing " + category + " bundles")
|
2023-07-04 16:56:09 +02:00
|
|
|
})
|
|
|
|
log.Debug().Str("category", category).Msg("finished bundle update")
|
|
|
|
}
|
2023-07-03 15:30:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseBundles(data []byte, category string) ([]models.Bundle, error) {
|
|
|
|
switch category {
|
|
|
|
case "books":
|
|
|
|
var books models.Books
|
|
|
|
err := json.Unmarshal(data, &books)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return books.Data.Books.Mosaic[0].Products, nil
|
|
|
|
case "games":
|
|
|
|
var games models.Games
|
|
|
|
err := json.Unmarshal(data, &games)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return games.Data.Games.Mosaic[0].Products, nil
|
|
|
|
case "software":
|
|
|
|
var software models.Software
|
|
|
|
err := json.Unmarshal(data, &software)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return software.Data.Software.Mosaic[0].Products, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown category %s", category)
|
|
|
|
}
|
|
|
|
}
|
2023-07-04 16:56:09 +02:00
|
|
|
|
|
|
|
func handleHumbleBundle(bundle models.Bundle, category string) error {
|
|
|
|
isnew, err := db.IsANewBundle(bundle)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isnew {
|
|
|
|
log.Debug().Str("bundle", bundle.MachineName).Msg("bundle already exists")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Str("bundle", bundle.ProductURL).Msg("found new Bundle")
|
|
|
|
|
|
|
|
err = db.Enqueue(bundle, category)
|
|
|
|
return err
|
|
|
|
}
|