2017-11-01 11:16:31 +01:00

76 lines
1.7 KiB
Go

package main
import (
"fmt"
"strings"
"github.com/animenotifier/arn"
"github.com/animenotifier/kitsu"
"github.com/fatih/color"
)
func main() {
color.Yellow("Syncing media relations with Kitsu DB")
defer arn.Node.Close()
kitsuMediaRelations := kitsu.StreamMediaRelations()
relations := map[string]*arn.AnimeRelations{}
for mediaRelation := range kitsuMediaRelations {
// We only care about anime for now
if mediaRelation.Relationships.Source.Data.Type != "anime" || mediaRelation.Relationships.Destination.Data.Type != "anime" {
continue
}
relationType := strings.Replace(mediaRelation.Attributes.Role, "_", " ", -1)
animeID := mediaRelation.Relationships.Source.Data.ID
destinationAnimeID := mediaRelation.Relationships.Destination.Data.ID
// Confirm that the anime IDs are valid
if !arn.DB.Exists("Anime", animeID) {
continue
}
if !arn.DB.Exists("Anime", destinationAnimeID) {
continue
}
fmt.Printf(
"%s %s has %s which is %s %s\n",
mediaRelation.Relationships.Source.Data.Type,
animeID,
color.GreenString(relationType),
mediaRelation.Relationships.Destination.Data.Type,
destinationAnimeID,
)
// Add anime to the global map
relationsList, found := relations[animeID]
if !found {
relationsList = &arn.AnimeRelations{
AnimeID: animeID,
Items: []*arn.AnimeRelation{},
}
relations[animeID] = relationsList
}
relationsList.Items = append(relationsList.Items, &arn.AnimeRelation{
AnimeID: destinationAnimeID,
Type: relationType,
})
// for _, item := range relationsList.Items {
// fmt.Println("*", item.Type, item.AnimeID)
// }
}
// Save relations map
for _, animeRelations := range relations {
animeRelations.Save()
}
color.Green("Finished.")
}