fix pending list

This commit is contained in:
Michael Zhang 2020-10-12 08:14:56 -05:00
parent e84affb5e2
commit 6ccacec1ec
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
2 changed files with 28 additions and 21 deletions

33
bot.go
View file

@ -75,16 +75,14 @@ func (bot *Bot) errWrap(fn interface{}) interface{} {
} }
func (bot *Bot) NotifyNewBeatmap(channels []string, newMaps []Beatmapset) (err error) { func (bot *Bot) NotifyNewBeatmap(channels []string, newMaps []Beatmapset) (err error) {
for i, beatmapSet := range newMaps { for _, beatmapSet := range newMaps {
var eventTime time.Time var eventTime time.Time
eventTime, err = time.Parse(time.RFC3339, beatmapSet.LastUpdated) eventTime, err = time.Parse(time.RFC3339, beatmapSet.LastUpdated)
if err != nil { if err != nil {
return return
} }
log.Println(i, "event time", eventTime)
var ( var (
beatmapSet Beatmapset
gotDownloadedBeatmap = false gotDownloadedBeatmap = false
downloadedBeatmap BeatmapsetDownloaded downloadedBeatmap BeatmapsetDownloaded
// status git.Status // status git.Status
@ -130,10 +128,6 @@ func (bot *Bot) NotifyNewBeatmap(channels []string, newMaps []Beatmapset) (err e
if err != nil { if err != nil {
return return
} }
// status, err = worktree.Status()
// if err != nil {
// return
// }
files, err = ioutil.ReadDir(repoDir) files, err = ioutil.ReadDir(repoDir)
if err != nil { if err != nil {
return return
@ -155,31 +149,34 @@ func (bot *Bot) NotifyNewBeatmap(channels []string, newMaps []Beatmapset) (err e
}, },
) )
if err != nil { if err != nil {
err = fmt.Errorf("couldn't create commit for %s: %w", beatmapSet.ID, err)
return return
} }
commit, err = repo.CommitObject(hash) commit, err = repo.CommitObject(hash)
if err != nil { if err != nil {
err = fmt.Errorf("couldn't find commit with hash %s: %w", hash, err)
return return
} }
parent, err = commit.Parent(0) parent, err = commit.Parent(0)
if err == object.ErrParentNotFound { if errors.Is(err, object.ErrParentNotFound) {
err = nil
} else if err != nil { } else if err != nil {
err = fmt.Errorf("couldn't retrieve commit parent: %w", err)
return return
} else { } else {
patch, err = commit.Patch(parent) patch, err = commit.Patch(parent)
if err != nil { if err != nil {
err = fmt.Errorf("couldn't retrieve patch: %w", err)
return return
} }
foundPatch = true foundPatch = true
} }
log.Println("BEATMAP SET", beatmapSet)
embed := &discordgo.MessageEmbed{ embed := &discordgo.MessageEmbed{
URL: fmt.Sprintf("https://osu.ppy.sh/s/%d", beatmapSet.ID), URL: fmt.Sprintf("https://osu.ppy.sh/s/%d", beatmapSet.ID),
Title: fmt.Sprintf("Update: %s - %s", beatmapSet.Artist, beatmapSet.Title), Title: fmt.Sprintf("Update: %s - %s", beatmapSet.Artist, beatmapSet.Title),
Timestamp: eventTime.String(), Timestamp: eventTime.Format(time.RFC3339),
Author: &discordgo.MessageEmbedAuthor{ Author: &discordgo.MessageEmbedAuthor{
URL: "https://osu.ppy.sh/u/" + strconv.Itoa(beatmapSet.UserId), URL: "https://osu.ppy.sh/u/" + strconv.Itoa(beatmapSet.UserId),
Name: beatmapSet.Creator, Name: beatmapSet.Creator,
@ -197,12 +194,22 @@ func (bot *Bot) NotifyNewBeatmap(channels []string, newMaps []Beatmapset) (err e
if gotDownloadedBeatmap { if gotDownloadedBeatmap {
log.Println(downloadedBeatmap) log.Println(downloadedBeatmap)
if foundPatch { if foundPatch {
embed.Description = patch.Stats().String() embed.Description = fmt.Sprintf(
"Latest revision: %s\n%s",
hash,
patch.Stats().String(),
)
} else {
embed.Description = "Newly tracked map; diff information will be reported upon next update!"
} }
} }
for _, channelId := range channels { for _, channelId := range channels {
bot.ChannelMessageSendEmbed(channelId, embed) _, err = bot.ChannelMessageSendEmbed(channelId, embed)
if err != nil {
err = fmt.Errorf("failed to send to %s: %w", channelId, err)
return
}
} }
} }

View file

@ -14,7 +14,7 @@ var (
func RunScraper(bot *Bot, db *Db, api *Osuapi, requests chan int) { func RunScraper(bot *Bot, db *Db, api *Osuapi, requests chan int) {
lastUpdateTime := time.Now() lastUpdateTime := time.Now()
go func() { go func() {
for range ticker.C { for ; true; <-ticker.C {
// build a list of currently tracked mappers // build a list of currently tracked mappers
trackedMappers := make(map[int]int) trackedMappers := make(map[int]int)
db.IterTrackedMappers(func(userId int) error { db.IterTrackedMappers(func(userId int) error {
@ -30,8 +30,8 @@ func RunScraper(bot *Bot, db *Db, api *Osuapi, requests chan int) {
allNewMaps := make(map[int][]Beatmapset, 0) allNewMaps := make(map[int][]Beatmapset, 0)
var newLastUpdateTime = time.Unix(0, 0) var newLastUpdateTime = time.Unix(0, 0)
for _, beatmap := range pendingSets.Beatmapsets { for _, beatmapSet := range pendingSets.Beatmapsets {
updatedTime, err := time.Parse(time.RFC3339, beatmap.LastUpdated) updatedTime, err := time.Parse(time.RFC3339, beatmapSet.LastUpdated)
if err != nil { if err != nil {
log.Println("error parsing last updated time", updatedTime) log.Println("error parsing last updated time", updatedTime)
} }
@ -45,33 +45,33 @@ func RunScraper(bot *Bot, db *Db, api *Osuapi, requests chan int) {
break break
} }
if mapperId, ok := trackedMappers[beatmap.UserId]; ok { mapperId := beatmapSet.UserId
if _, ok := trackedMappers[mapperId]; ok {
if _, ok2 := allNewMaps[mapperId]; !ok2 { if _, ok2 := allNewMaps[mapperId]; !ok2 {
allNewMaps[mapperId] = make([]Beatmapset, 0) allNewMaps[mapperId] = make([]Beatmapset, 0)
} }
allNewMaps[mapperId] = append(allNewMaps[mapperId], beatmap) allNewMaps[mapperId] = append(allNewMaps[mapperId], beatmapSet)
} }
} }
if len(allNewMaps) > 0 { if len(allNewMaps) > 0 {
log.Println("all new maps", allNewMaps)
for mapperId, newMaps := range allNewMaps { for mapperId, newMaps := range allNewMaps {
channels := make([]string, 0) channels := make([]string, 0)
db.IterTrackingChannels(mapperId, func(channelId string) error { db.IterTrackingChannels(mapperId, func(channelId string) error {
channels = append(channels, channelId) channels = append(channels, channelId)
return nil return nil
}) })
log.Println(newMaps)
err := bot.NotifyNewBeatmap(channels, newMaps) err := bot.NotifyNewBeatmap(channels, newMaps)
if err != nil { if err != nil {
log.Println("error notifying new maps", err) log.Println("error notifying new maps:", err)
} }
} }
} }
lastUpdateTime = newLastUpdateTime lastUpdateTime = newLastUpdateTime
fmt.Print("\a")
log.Println("last updated time", lastUpdateTime) log.Println("last updated time", lastUpdateTime)
} }
}() }()