mirror of
https://github.com/ultrasn0w/huso.git
synced 2025-12-14 06:29:53 +01:00
Charts V2
This commit is contained in:
70
schaffer.go
70
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)
|
||||
|
||||
Reference in New Issue
Block a user