This commit is contained in:
Noah 2024-12-20 02:09:07 +01:00
parent 6aa64b6287
commit 3ec330e3a2
16 changed files with 408 additions and 1 deletions

17
steam/api.go Normal file
View file

@ -0,0 +1,17 @@
package steam
import (
"github.com/Philipp15b/go-steamapi"
"github.com/rs/zerolog/log"
"watn3y/steamsalty/config"
)
func getPlayerDetails(steamID uint64) (summary steamapi.PlayerSummary) {
response, err := steamapi.GetPlayerSummaries([]uint64{steamID}, config.BotConfig.SteamAPIKey)
if err != nil {
log.Error().Err(err).Msg("Failed to get Player Summary")
}
return response[0]
}

48
steam/http.go Normal file
View file

@ -0,0 +1,48 @@
package steam
import (
"encoding/json"
"github.com/rs/zerolog/log"
"io"
"net/http"
"net/url"
"strconv"
)
func getComments(steamID uint64, start int, count int) (comments CommentResponse) {
baseURL := "https://steamcommunity.com/comment/Profile/render/"
url, err := url.Parse(baseURL + strconv.FormatUint(steamID, 10))
if err != nil {
log.Error().Err(err).Msg("Unable to Parse SteamID into URL")
return
}
query := url.Query()
query.Set("start", strconv.Itoa(start))
query.Set("count", strconv.Itoa(count))
url.RawQuery = query.Encode()
resp, err := http.Get(url.String())
if err != nil || resp.StatusCode != http.StatusOK {
log.Error().Err(err).Int("Response Code", resp.StatusCode).Msg("Failed to get Comments")
}
defer resp.Body.Close()
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Error().Err(err).Msg("Failed to parse Comments")
log.Trace().Interface("Body", resp.Body)
}
err = json.Unmarshal(body, &comments)
if err != nil {
log.Error().Err(err).Msg("Failed to parse Comments as JSON")
}
log.Debug().Interface("CommentPage", comments).Msg("Successfully got Comment Page")
return comments
}

57
steam/profile.go Normal file
View file

@ -0,0 +1,57 @@
package steam
import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/rs/zerolog/log"
"math"
"sync"
"time"
"watn3y/steamsalty/botIO"
"watn3y/steamsalty/config"
)
var sleeptime time.Duration = 10 * time.Second
func StartWatchers(bot *tgbotapi.BotAPI) {
var wg sync.WaitGroup
for _, steamID := range config.BotConfig.Watchers {
wg.Add(1)
go func(steamID uint64) {
defer wg.Done()
watcher(bot, steamID)
}(steamID)
}
wg.Wait()
}
func watcher(bot *tgbotapi.BotAPI, steamID uint64) {
log.Info().Uint64("SteamID", steamID).Msg("Started Watcher")
var previousCount int
for {
currentCount := getComments(steamID, math.MaxInt32, 0).TotalCount
if previousCount == 0 || currentCount <= previousCount {
previousCount = currentCount
time.Sleep(sleeptime)
continue
}
log.Info().Int("NumComment", currentCount).Uint64("SteamID", steamID).Msg("Found new comment")
player := getPlayerDetails(steamID)
msg := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: config.BotConfig.ChatID},
ParseMode: "html",
DisableWebPagePreview: false,
Text: fmt.Sprintf(`New comment on <a href="%s">%s's</a> profile`, player.ProfileURL, player.PersonaName),
}
botIO.SendMessage(msg, bot)
previousCount = currentCount
time.Sleep(sleeptime)
}
}

9
steam/types.go Normal file
View file

@ -0,0 +1,9 @@
package steam
type CommentResponse struct {
Success bool `json:"success"`
Start int `json:"start"`
TotalCount int `json:"total_count"`
CommentsHTML string `json:"comments_html"`
Timelastpost int `json:"timelastpost"`
}