mirror of
https://github.com/watn3y/steamsalty.git
synced 2025-06-07 15:21:00 +02:00
v0.2
- Include comment text in notification - Include commenter in notification - Use Timestamp to check if a comment is new - Set sleep interval between requests in env variable
This commit is contained in:
parent
f80a6e50b1
commit
9c64a3e4b5
8 changed files with 172 additions and 28 deletions
|
@ -12,6 +12,6 @@ func getPlayerDetails(steamID uint64) (summary steamapi.PlayerSummary) {
|
|||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to get Player Summary")
|
||||
}
|
||||
|
||||
log.Debug().Interface("Player", response[0]).Msg("Successfully got PlayerSummary from Steam API")
|
||||
return response[0]
|
||||
}
|
||||
|
|
|
@ -2,14 +2,18 @@ package steam
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/rs/zerolog/log"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func getComments(steamID uint64, start int, count int) (comments CommentResponse) {
|
||||
func getComments(steamID uint64, start int, count int) (page CommentsPage) {
|
||||
|
||||
baseURL := "https://steamcommunity.com/comment/Profile/render/"
|
||||
|
||||
|
@ -38,11 +42,49 @@ func getComments(steamID uint64, start int, count int) (comments CommentResponse
|
|||
log.Trace().Interface("Body", resp.Body)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, &comments)
|
||||
err = json.Unmarshal(body, &page)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to parse Comments as JSON")
|
||||
}
|
||||
|
||||
log.Debug().Interface("CommentPage", comments).Msg("Successfully got Comment Page")
|
||||
log.Debug().Interface("CommentPage", page).Uint64("ProfileID", steamID).Msg("Successfully got Comment Page")
|
||||
|
||||
return page
|
||||
}
|
||||
|
||||
func parseComments(rawComments CommentsPage) (comments []Comment) {
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(strings.NewReader(rawComments.CommentsHTML))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Error while parsing CommentsHTML")
|
||||
return
|
||||
}
|
||||
doc.Find(".commentthread_comment.responsive_body_text").Each(func(i int, s *goquery.Selection) {
|
||||
var c Comment
|
||||
|
||||
parsedID, err := strconv.ParseUint(strings.TrimPrefix(s.AttrOr("id", ""), "comment_"), 10, 64)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Error while parsing Comment ID")
|
||||
return
|
||||
}
|
||||
c.ID = parsedID
|
||||
|
||||
c.Timestamp, err = strconv.ParseInt(s.Find(".commentthread_comment_timestamp").AttrOr("data-timestamp", ""), 10, 64)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Error while parsing Comment Timestamp")
|
||||
return
|
||||
}
|
||||
|
||||
c.Author = s.Find(".commentthread_comment_author .hoverunderline bdi").Text()
|
||||
|
||||
c.AuthorProfileURL, _ = s.Find(".commentthread_comment_author .hoverunderline").Attr("href")
|
||||
|
||||
c.Text = strings.TrimSpace(s.Find(".commentthread_comment_text").Text())
|
||||
|
||||
comments = append(comments, c)
|
||||
})
|
||||
|
||||
slices.Reverse(comments)
|
||||
log.Debug().Interface("Comments", comments).Msg("Successfully parsed Comment Page")
|
||||
return comments
|
||||
}
|
||||
|
|
|
@ -2,16 +2,21 @@ package steam
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
"math"
|
||||
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"watn3y/steamsalty/botIO"
|
||||
"watn3y/steamsalty/config"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
var sleeptime time.Duration = 1 * time.Minute
|
||||
var sleeptime time.Duration = time.Duration(config.BotConfig.SleepInterval) * time.Second
|
||||
var steamContentCheckText string = "This comment is awaiting analysis by our automated content check system. It will be temporarily hidden until we verify that it does not contain harmful content (e.g. links to websites that attempt to steal information)."
|
||||
|
||||
func StartWatchers(bot *tgbotapi.BotAPI) {
|
||||
var wg sync.WaitGroup
|
||||
|
@ -29,29 +34,47 @@ func StartWatchers(bot *tgbotapi.BotAPI) {
|
|||
|
||||
func watcher(bot *tgbotapi.BotAPI, steamID uint64) {
|
||||
log.Info().Uint64("SteamID", steamID).Msg("Started Watcher")
|
||||
var previousCount int
|
||||
|
||||
var newestProcessedComment int64 = 0
|
||||
|
||||
for {
|
||||
currentCount := getComments(steamID, math.MaxInt32, 0).TotalCount
|
||||
if previousCount == 0 || currentCount <= previousCount {
|
||||
previousCount = currentCount
|
||||
currentCommentsPage := getComments(steamID, 0, math.MaxInt32)
|
||||
if newestProcessedComment == 0 || newestProcessedComment == currentCommentsPage.TimeLastPost {
|
||||
newestProcessedComment = currentCommentsPage.TimeLastPost
|
||||
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),
|
||||
if strings.Contains(currentCommentsPage.CommentsHTML, steamContentCheckText) {
|
||||
log.Info().Uint64("ProfileID", steamID).Msg("Found new comment(s) still being checked by Steam, retrying in "+ sleeptime.String())
|
||||
time.Sleep(sleeptime)
|
||||
continue
|
||||
}
|
||||
|
||||
botIO.SendMessage(msg, bot)
|
||||
log.Info().Uint64("ProfileID", steamID).Msg("Found new comment(s)")
|
||||
|
||||
previousCount = currentCount
|
||||
profileOwner := getPlayerDetails(steamID)
|
||||
|
||||
for _, comment := range parseComments(currentCommentsPage) {
|
||||
log.Debug().Interface("Comment", comment).Msg("Processing Comment")
|
||||
if comment.Timestamp <= newestProcessedComment {
|
||||
log.Debug().Uint64("CommentID", comment.ID).Msg("Skipping Comment")
|
||||
continue
|
||||
}
|
||||
|
||||
msg := tgbotapi.MessageConfig{
|
||||
BaseChat: tgbotapi.BaseChat{ChatID: config.BotConfig.ChatID},
|
||||
ParseMode: "HTML",
|
||||
DisableWebPagePreview: true,
|
||||
Text: fmt.Sprintf(`<b><a href="%s">%s</a> just commented on <a href="%s">%s</a>'s profile:</b>`, comment.AuthorProfileURL, comment.Author, profileOwner.ProfileURL, profileOwner.PersonaName) + "\n" +
|
||||
"<blockquote>" + comment.Text + "</blockquote>",
|
||||
}
|
||||
log.Info().Interface("Comment", comment).Msg("Notifying about new Comment")
|
||||
botIO.SendMessage(msg, bot)
|
||||
time.Sleep(time.Minute / 20)
|
||||
}
|
||||
|
||||
newestProcessedComment = currentCommentsPage.TimeLastPost
|
||||
time.Sleep(sleeptime)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,17 @@
|
|||
package steam
|
||||
|
||||
type CommentResponse struct {
|
||||
type CommentsPage struct {
|
||||
Success bool `json:"success"`
|
||||
Start int `json:"start"`
|
||||
TotalCount int `json:"total_count"`
|
||||
CommentsHTML string `json:"comments_html"`
|
||||
Timelastpost int `json:"timelastpost"`
|
||||
TimeLastPost int64 `json:"timelastpost"`
|
||||
}
|
||||
|
||||
type Comment struct {
|
||||
ID uint64
|
||||
Timestamp int64
|
||||
Author string
|
||||
AuthorProfileURL string
|
||||
Text string
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue