2017-11-01 09:45:14 +01:00

131 lines
2.8 KiB
Go

package main
import (
"github.com/animenotifier/arn"
"github.com/fatih/color"
)
var ratings = map[string][]*arn.AnimeRating{}
var finalRating = map[string]*arn.AnimeRating{}
var popularity = map[string]*arn.AnimePopularity{}
// Note this is using the airing-anime as a template with modfications
// made to it.
func main() {
color.Yellow("Updating anime ratings")
defer arn.Node.Close()
allAnimeLists, err := arn.AllAnimeLists()
arn.PanicOnError(err)
for _, animeList := range allAnimeLists {
extractRatings(animeList)
extractPopularity(animeList)
}
// Calculate rating
for animeID := range finalRating {
overall := []float64{}
story := []float64{}
visuals := []float64{}
soundtrack := []float64{}
for _, rating := range ratings[animeID] {
if rating.Overall != 0 {
overall = append(overall, rating.Overall)
}
if rating.Story != 0 {
story = append(story, rating.Story)
}
if rating.Visuals != 0 {
visuals = append(visuals, rating.Visuals)
}
if rating.Soundtrack != 0 {
soundtrack = append(soundtrack, rating.Soundtrack)
}
}
finalRating[animeID].Overall = average(overall)
finalRating[animeID].Story = average(story)
finalRating[animeID].Visuals = average(visuals)
finalRating[animeID].Soundtrack = average(soundtrack)
}
// Save
for animeID := range finalRating {
anime, err := arn.GetAnime(animeID)
arn.PanicOnError(err)
anime.Rating = finalRating[animeID]
anime.Save()
}
// Save popularity
for animeID := range popularity {
anime, err := arn.GetAnime(animeID)
arn.PanicOnError(err)
anime.Popularity = popularity[animeID]
anime.Save()
}
color.Green("Finished.")
}
func average(floatSlice []float64) float64 {
if len(floatSlice) == 0 {
return arn.DefaultAverageRating
}
var sum float64
for _, value := range floatSlice {
sum += value
}
return sum / float64(len(floatSlice))
}
func extractRatings(animeList *arn.AnimeList) {
for _, item := range animeList.Items {
if item.Rating.IsNotRated() {
continue
}
_, found := ratings[item.AnimeID]
if !found {
ratings[item.AnimeID] = []*arn.AnimeRating{}
finalRating[item.AnimeID] = &arn.AnimeRating{}
}
ratings[item.AnimeID] = append(ratings[item.AnimeID], item.Rating)
}
}
func extractPopularity(animeList *arn.AnimeList) {
for _, item := range animeList.Items {
_, found := popularity[item.AnimeID]
if !found {
popularity[item.AnimeID] = &arn.AnimePopularity{}
}
counter := popularity[item.AnimeID]
switch item.Status {
case arn.AnimeListStatusWatching:
counter.Watching++
case arn.AnimeListStatusCompleted:
counter.Completed++
case arn.AnimeListStatusPlanned:
counter.Planned++
case arn.AnimeListStatusHold:
counter.Hold++
case arn.AnimeListStatusDropped:
counter.Dropped++
}
}
}