diff --git a/huso.go b/huso.go index f7ed983..b803bd1 100644 --- a/huso.go +++ b/huso.go @@ -19,7 +19,7 @@ import ( ) const ( - husoVersion = "1.2" + husoVersion = "1.3" registerSecret = "綾波レイ" seasonApiJikan = "seasons/now" seasonStrJikan = "seasons/" diff --git a/ober.go b/ober.go index d79134a..15d3720 100644 --- a/ober.go +++ b/ober.go @@ -624,7 +624,7 @@ func processUpdateReq(ctx *fasthttp.RequestCtx, update bool) { if update { // anime exitsts => save // get watch progress - progress, updated, listState, _ := FetchProgress(anime.Anime, userId, username, 0) + progress, updated, _, listState, _ := FetchProgress(anime.Anime, username) // anime is already completed big baka user if listState == malApiStatusC { ctx.WriteString("Du hast schon fertig geschaut bro") diff --git a/praktikant.go b/praktikant.go index fedcca4..750cff9 100644 --- a/praktikant.go +++ b/praktikant.go @@ -59,7 +59,7 @@ func Arbeit() { for _, a := range animesUsers { // iterate users for _, u := range a.Users { - newProgress, updated, listState, err := FetchProgress(a.Anime, u.MalID, u.Username, u.Progress) + newProgress, updated, score, listState, err := FetchProgress(a.Anime, u.Username) if err != nil { color.Errorln(err.Error()) logOut.WriteError(err) @@ -67,8 +67,8 @@ func Arbeit() { } // check if user set anime as completed if listState == malApiStatusC { - color.Infof("%s finished %d\n", u.Username, a.Anime) - logOut.WriteLine(fmt.Sprintf("📜 %s finished %d !", u.Username, a.Anime)) + color.Infof("%s finished %d scored %d\n", u.Username, a.Anime, score) + logOut.WriteLine(fmt.Sprintf("📜 %s finished %d scored %d !", u.Username, a.Anime, score)) // delete user from anime _, err = DeleteUserFromAnime(u.Username, u.MalID, a.Anime) if err != nil { diff --git a/schaffer.go b/schaffer.go index a451146..cb655af 100644 --- a/schaffer.go +++ b/schaffer.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "sort" "strings" "time" @@ -181,57 +182,56 @@ func SearchAppointments(animeId int64) ([]Appointment, error) { return result, nil } -func FetchProgress(animeId, userId int64, username string, progress int) (int, time.Time, string, error) { +func FetchProgress(animeId int64, username string) (int, time.Time, int, string, error) { // check watching first - newProgress, updated, err := fetchProgressOnState(animeId, userId, progress, username, malApiStatusW) + newProgress, updated, score, err := FetchProgressOnState(animeId, username, malApiStatusW) if err != nil { - return newProgress, updated, "", err + return newProgress, updated, score, "", err } if newProgress != -1 { - return newProgress, updated, malApiStatusW, err + return newProgress, updated, score, malApiStatusW, err } // check on hold - newProgress, updated, err = fetchProgressOnState(animeId, userId, progress, username, malApiStatusH) + newProgress, updated, score, err = FetchProgressOnState(animeId, username, malApiStatusH) if err != nil { - return newProgress, updated, "", err + return newProgress, updated, score, "", err } if newProgress != -1 { - return newProgress, updated, malApiStatusH, err + return newProgress, updated, score, malApiStatusH, err } // check completed - newProgress, updated, err = fetchProgressOnState(animeId, userId, progress, username, malApiStatusC) + newProgress, updated, score, err = FetchProgressOnState(animeId, username, malApiStatusC) if err != nil { - return newProgress, updated, "", err + return newProgress, updated, score, "", err } if newProgress != -1 { - return newProgress, updated, malApiStatusC, err + return newProgress, updated, score, malApiStatusC, err } // check dropped - newProgress, updated, err = fetchProgressOnState(animeId, userId, progress, username, malApiStatusD) + newProgress, updated, score, err = FetchProgressOnState(animeId, username, malApiStatusD) if err != nil { - return newProgress, updated, "", err + return newProgress, updated, score, "", err } if newProgress != -1 { - return newProgress, updated, malApiStatusD, err + return newProgress, updated, score, malApiStatusD, err } // has no progress or PTW - return 0, updated, "", nil + return 0, updated, 0, "", nil } -func fetchProgressOnState(animeId, userId int64, progress int, username, malStatus string) (int, time.Time, error) { +func FetchProgressOnState(animeId int64, username, malStatus string) (int, time.Time, int, error) { list, _, err := GetUserAnimeListData(username, malStatus) if err != nil { - return 0, time.Time{}, err + return 0, time.Time{}, 0, err } for _, a := range list.Data { // check if found if a.Node.ID == animeId { - // check if progress changed - return a.ListStatus.NumEpisodesWatched, a.ListStatus.UpdatedAt, nil + return a.ListStatus.NumEpisodesWatched, a.ListStatus.UpdatedAt, a.ListStatus.Score, nil } } // no progess found - return -1, time.Now(), nil + return -1, time.Now(), 0, nil } func AddToChat(old, new, user string) string { @@ -287,6 +287,11 @@ func BuildMovieCharts() ([]MovieChart, error) { if err != nil { return nil, err } + users, err := ReadRegisteredUsers() + if err != nil { + return nil, err + } + charts = make([]MovieChart, 0) for _, m := range movieList { c := MovieChart{ @@ -295,12 +300,33 @@ func BuildMovieCharts() ([]MovieChart, error) { anime, err := SearchAnime(m.Anime) if err != nil { color.Errorln(err.Error()) - } else { - c.Data = *anime - charts = append(charts, c) + continue } + + c.Data = *anime + scoreSum := 0 + for _, u := range users { + progress, _, score, err := FetchProgressOnState(anime.Anime, u.Username, malApiStatusC) + if err != nil { + color.Errorln(err.Error()) + continue + } + if progress == -1 || score == 0 { + // user has no progress/score + continue + } + scoreSum += score + c.UserCount++ + } + if c.UserCount > 0 { + c.AvgScore = float64(scoreSum) / float64(c.UserCount) + } + + charts = append(charts, c) } + sort.SliceStable(charts, func(i, j int) bool { return charts[i].AvgScore > charts[j].AvgScore }) + bytes, err := json.Marshal(charts) if err == nil { mmCache.Set(key, bytes) diff --git a/season.qtpl b/season.qtpl index ceefbec..fc298e3 100644 --- a/season.qtpl +++ b/season.qtpl @@ -38,7 +38,7 @@ body { background-color: #1a1a1a; color: #fff; } {% for _, anime := range animes %} {%dl anime.Anime %} - {%f anime.Score %} + {%f.2 anime.Score %} {%s anime.Title %} {% endfor %} @@ -57,7 +57,7 @@ body { background-color: #1a1a1a; color: #fff; } {%d chart.MmId %} {%dl chart.Data.Anime %} {%d chart.UserCount %} - {%f chart.AvgScore %} + {%f.2 chart.AvgScore %} {%s chart.Data.Title %} {% endfor %} diff --git a/season.qtpl.go b/season.qtpl.go index c3bd21d..ed676e7 100644 --- a/season.qtpl.go +++ b/season.qtpl.go @@ -45,7 +45,7 @@ func StreamIndex(qw422016 *qt422016.Writer, animes []Anime, oracles []MmOracle, //line season.qtpl:40 qw422016.N().S(` `) //line season.qtpl:41 - qw422016.N().F(anime.Score) + qw422016.N().FPrec(anime.Score, 2) //line season.qtpl:41 qw422016.N().S(` `) //line season.qtpl:42 @@ -81,7 +81,7 @@ func StreamIndex(qw422016 *qt422016.Writer, animes []Anime, oracles []MmOracle, //line season.qtpl:59 qw422016.N().S(` `) //line season.qtpl:60 - qw422016.N().F(chart.AvgScore) + qw422016.N().FPrec(chart.AvgScore, 2) //line season.qtpl:60 qw422016.N().S(` `) //line season.qtpl:61