160 lines
3.5 KiB
Go
Raw Normal View History

2017-06-30 18:00:56 +00:00
package main
import (
2018-12-11 05:51:17 +00:00
"fmt"
2017-06-30 18:00:56 +00:00
"github.com/animenotifier/arn"
"github.com/fatih/color"
)
2018-03-27 10:36:32 +00:00
var ratings = map[string][]arn.AnimeListItemRating{}
2017-06-30 18:00:56 +00:00
var finalRating = map[string]*arn.AnimeRating{}
2017-09-30 14:04:13 +00:00
var popularity = map[string]*arn.AnimePopularity{}
2017-06-30 18:00:56 +00:00
// Note this is using the airing-anime as a template with modfications
// made to it.
func main() {
color.Yellow("Updating anime ratings")
2018-03-28 22:26:19 +00:00
defer color.Green("Finished.")
2017-11-01 08:45:14 +00:00
defer arn.Node.Close()
2017-06-30 18:00:56 +00:00
2018-12-11 05:51:17 +00:00
fmt.Println("Processing anime lists")
2017-06-30 18:00:56 +00:00
2018-12-11 05:51:17 +00:00
for animeList := range arn.StreamAnimeLists() {
2017-06-30 18:00:56 +00:00
extractRatings(animeList)
2017-09-30 14:04:13 +00:00
extractPopularity(animeList)
2017-06-30 18:00:56 +00:00
}
2017-09-30 14:04:13 +00:00
// Calculate rating
2017-06-30 18:00:56 +00:00
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)
}
}
2018-03-22 02:44:18 +00:00
// Save number of people who rated on this
finalRating[animeID].Count.Overall = len(overall)
finalRating[animeID].Count.Story = len(story)
finalRating[animeID].Count.Visuals = len(visuals)
finalRating[animeID].Count.Soundtrack = len(soundtrack)
// Dampen the rating if number of users is too low
if len(overall) < arn.RatingCountThreshold {
overall = append(overall, arn.AverageRating)
}
if len(story) < arn.RatingCountThreshold {
story = append(story, arn.AverageRating)
}
if len(visuals) < arn.RatingCountThreshold {
visuals = append(visuals, arn.AverageRating)
}
if len(soundtrack) < arn.RatingCountThreshold {
soundtrack = append(soundtrack, arn.AverageRating)
}
// Average rating
2017-06-30 18:00:56 +00:00
finalRating[animeID].Overall = average(overall)
finalRating[animeID].Story = average(story)
finalRating[animeID].Visuals = average(visuals)
finalRating[animeID].Soundtrack = average(soundtrack)
}
2018-12-11 05:51:17 +00:00
// Save rating
fmt.Println("Saving rating")
2017-06-30 18:00:56 +00:00
for animeID := range finalRating {
anime, err := arn.GetAnime(animeID)
arn.PanicOnError(err)
anime.Rating = finalRating[animeID]
2017-11-01 08:45:14 +00:00
anime.Save()
2017-06-30 18:00:56 +00:00
}
2017-09-30 14:04:13 +00:00
// Save popularity
2018-12-11 05:51:17 +00:00
fmt.Println("Saving popularity")
2017-09-30 14:04:13 +00:00
for animeID := range popularity {
anime, err := arn.GetAnime(animeID)
arn.PanicOnError(err)
anime.Popularity = popularity[animeID]
2017-11-01 08:45:14 +00:00
anime.Save()
2017-09-30 14:04:13 +00:00
}
2017-06-30 18:00:56 +00:00
}
func average(floatSlice []float64) float64 {
if len(floatSlice) == 0 {
2018-03-22 02:44:18 +00:00
return 0
2017-06-30 18:00:56 +00:00
}
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 {
2018-03-27 10:36:32 +00:00
ratings[item.AnimeID] = []arn.AnimeListItemRating{}
2017-06-30 18:00:56 +00:00
finalRating[item.AnimeID] = &arn.AnimeRating{}
}
ratings[item.AnimeID] = append(ratings[item.AnimeID], item.Rating)
}
}
2017-09-30 14:04:13 +00:00
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++
}
}
}