subscribe-bot/scrape/scrape.go

164 lines
3.8 KiB
Go
Raw Normal View History

2020-10-12 13:47:28 +00:00
package scrape
2020-10-11 19:32:58 +00:00
import (
"fmt"
"log"
"time"
2020-10-12 13:47:28 +00:00
2020-10-12 14:25:50 +00:00
"subscribe-bot/config"
2020-10-12 13:47:28 +00:00
"subscribe-bot/db"
"subscribe-bot/discord"
"subscribe-bot/osuapi"
2020-10-11 19:32:58 +00:00
)
var (
refreshInterval = 30 * time.Second
2020-10-12 13:47:28 +00:00
Ticker = time.NewTicker(refreshInterval)
2020-10-11 19:32:58 +00:00
)
2020-10-12 14:25:50 +00:00
func RunScraper(config *config.Config, bot *discord.Bot, db *db.Db, api *osuapi.Osuapi) {
lastUpdateTime := time.Now()
go func() {
2020-10-12 13:47:28 +00:00
for ; true; <-Ticker.C {
// build a list of currently tracked mappers
trackedMappers := make(map[int]int)
2020-10-14 19:33:23 +00:00
db.IterAllTrackedMappers(func(userId int) error {
trackedMappers[userId] = 1
2020-10-12 08:50:18 +00:00
return nil
})
// TODO: is this sorted for sure??
2020-10-12 13:47:28 +00:00
pendingSets, err := api.SearchBeatmaps("pending")
2020-10-12 08:50:18 +00:00
if err != nil {
log.Println("error fetching pending sets", err)
2020-10-12 08:50:18 +00:00
}
2020-10-11 19:32:58 +00:00
2020-10-12 13:47:28 +00:00
allNewMaps := make(map[int][]osuapi.Beatmapset, 0)
var newLastUpdateTime = time.Unix(0, 0)
2020-10-12 13:14:56 +00:00
for _, beatmapSet := range pendingSets.Beatmapsets {
updatedTime, err := time.Parse(time.RFC3339, beatmapSet.LastUpdated)
if err != nil {
log.Println("error parsing last updated time", updatedTime)
}
if updatedTime.After(newLastUpdateTime) {
// update lastUpdateTime to latest updated map
newLastUpdateTime = updatedTime
}
if !updatedTime.After(lastUpdateTime) {
break
}
mapperId := beatmapSet.UserID
2020-10-12 13:14:56 +00:00
if _, ok := trackedMappers[mapperId]; ok {
if _, ok2 := allNewMaps[mapperId]; !ok2 {
2020-10-12 13:47:28 +00:00
allNewMaps[mapperId] = make([]osuapi.Beatmapset, 0)
}
2020-10-12 13:14:56 +00:00
allNewMaps[mapperId] = append(allNewMaps[mapperId], beatmapSet)
}
}
if len(allNewMaps) > 0 {
for mapperId, newMaps := range allNewMaps {
channels := make([]string, 0)
db.IterTrackingChannels(mapperId, func(channelId string) error {
channels = append(channels, channelId)
return nil
})
err := bot.NotifyNewBeatmap(channels, newMaps)
if err != nil {
2020-10-12 13:14:56 +00:00
log.Println("error notifying new maps:", err)
}
}
}
lastUpdateTime = newLastUpdateTime
2020-10-12 14:25:50 +00:00
// this rings the terminal bell when it's updated so i don't have to stare
// at a blank screen for 30 seconds waiting for the feed to update
if config.Debug {
fmt.Print("\a")
}
log.Println("last updated time", lastUpdateTime)
}
}()
2020-10-11 19:32:58 +00:00
}
2020-10-12 13:47:28 +00:00
func getNewMaps(db *db.Db, api *osuapi.Osuapi, userId int) (newMaps []osuapi.Event, err error) {
2020-10-11 19:32:58 +00:00
// see if there's a last event
hasLastEvent, lastEventId := db.MapperLastEvent(userId)
2020-10-12 13:47:28 +00:00
newMaps = make([]osuapi.Event, 0)
2020-10-11 19:32:58 +00:00
var (
2020-10-12 13:47:28 +00:00
events []osuapi.Event
2020-10-11 19:32:58 +00:00
newLatestEvent = 0
updateLatestEvent = false
)
if hasLastEvent {
offset := 0
loop:
for {
events, err = api.GetUserEvents(userId, 50, offset)
if err != nil {
err = fmt.Errorf("couldn't load events for user %d, offset %d: %w", userId, offset, err)
return
}
if len(events) == 0 {
break
}
for _, event := range events {
if event.ID == lastEventId {
break loop
}
if event.ID > newLatestEvent {
updateLatestEvent = true
newLatestEvent = event.ID
}
if event.Type == "beatmapsetUpload" ||
event.Type == "beatmapsetRevive" ||
event.Type == "beatmapsetUpdate" {
2020-10-12 04:22:47 +00:00
newMaps = append(newMaps, event)
2020-10-11 19:32:58 +00:00
}
}
offset += len(events)
}
} else {
log.Printf("no last event id found for %d\n", userId)
events, err = api.GetUserEvents(userId, 50, 0)
if err != nil {
return
}
for _, event := range events {
if event.ID > newLatestEvent {
updateLatestEvent = true
newLatestEvent = event.ID
}
if event.Type == "beatmapsetUpload" ||
event.Type == "beatmapsetRevive" ||
event.Type == "beatmapsetUpdate" {
2020-10-12 04:22:47 +00:00
newMaps = append(newMaps, event)
2020-10-11 19:32:58 +00:00
}
}
}
2020-10-12 04:22:47 +00:00
// TODO: debug
// updateLatestEvent = false
2020-10-11 19:32:58 +00:00
if updateLatestEvent {
err = db.UpdateMapperLatestEvent(userId, newLatestEvent)
if err != nil {
return
}
}
return
}