2020-10-11 19:32:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
refreshInterval = 60 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
func RunScraper(bot *Bot, db *Db, api *Osuapi, requests chan int) {
|
|
|
|
// start timers
|
|
|
|
go startTimers(db, requests)
|
|
|
|
|
|
|
|
for userId := range requests {
|
2020-10-12 06:33:45 +00:00
|
|
|
log.Println("scraping user", userId)
|
2020-10-11 19:32:58 +00:00
|
|
|
newMaps, err := getNewMaps(db, api, userId)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("err getting new maps:", err)
|
|
|
|
}
|
2020-10-12 08:50:18 +00:00
|
|
|
log.Println("new maps for", userId, newMaps)
|
2020-10-11 19:32:58 +00:00
|
|
|
|
2020-10-12 08:50:18 +00:00
|
|
|
if len(newMaps) > 0 {
|
|
|
|
channels := make([]string, 0)
|
|
|
|
db.IterTrackingChannels(userId, func(channelId string) error {
|
|
|
|
channels = append(channels, channelId)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
err := bot.NotifyNewEvent(channels, newMaps)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error notifying new maps", err)
|
|
|
|
}
|
|
|
|
}
|
2020-10-11 19:32:58 +00:00
|
|
|
|
|
|
|
// wait a minute and put them back into the queue
|
2020-10-12 06:22:03 +00:00
|
|
|
go func(id int) {
|
2020-10-11 19:32:58 +00:00
|
|
|
time.Sleep(refreshInterval)
|
2020-10-12 06:22:03 +00:00
|
|
|
requests <- id
|
|
|
|
}(userId)
|
2020-10-11 19:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 04:22:47 +00:00
|
|
|
func getNewMaps(db *Db, api *Osuapi, userId int) (newMaps []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 04:22:47 +00:00
|
|
|
newMaps = make([]Event, 0)
|
2020-10-11 19:32:58 +00:00
|
|
|
var (
|
|
|
|
events []Event
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func startTimers(db *Db, requests chan int) {
|
|
|
|
db.IterTrackedMappers(func(userId int) error {
|
|
|
|
requests <- userId
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|