83 lines
1.8 KiB
Go
Raw Normal View History

2017-10-17 10:55:54 +00:00
package main
import (
"fmt"
"strings"
2017-10-17 13:47:07 +00:00
"github.com/animenotifier/arn"
2017-10-17 10:55:54 +00:00
"github.com/animenotifier/kitsu"
"github.com/fatih/color"
)
func main() {
color.Yellow("Syncing media relations with Kitsu DB")
kitsuMediaRelations := kitsu.StreamMediaRelations()
2017-10-21 20:47:04 +00:00
relations := map[string]*arn.AnimeRelations{}
2017-10-17 10:55:54 +00:00
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)
2017-10-17 13:47:07 +00:00
animeID := mediaRelation.Relationships.Source.Data.ID
destinationAnimeID := mediaRelation.Relationships.Destination.Data.ID
2017-10-17 10:55:54 +00:00
2017-10-17 14:58:28 +00:00
// Confirm that the anime IDs are valid
exists, _ := arn.DB.Exists("Anime", animeID)
if !exists {
continue
}
exists, _ = arn.DB.Exists("Anime", destinationAnimeID)
if !exists {
continue
}
2017-10-17 10:55:54 +00:00
fmt.Printf(
2017-10-17 13:47:07 +00:00
"%s %s has %s which is %s %s\n",
2017-10-17 10:55:54 +00:00
mediaRelation.Relationships.Source.Data.Type,
2017-10-17 13:47:07 +00:00
animeID,
2017-10-17 10:55:54 +00:00
color.GreenString(relationType),
mediaRelation.Relationships.Destination.Data.Type,
2017-10-17 13:47:07 +00:00
destinationAnimeID,
2017-10-17 10:55:54 +00:00
)
2017-10-17 13:47:07 +00:00
2017-10-17 14:58:28 +00:00
// Add anime to the global map
2017-10-17 13:47:07 +00:00
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 {
err := animeRelations.Save()
if err != nil {
color.Red(err.Error())
}
2017-10-17 10:55:54 +00:00
}
color.Green("Finished.")
}