76 lines
1.7 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/blitzprog/color"
2017-10-17 10:55:54 +00:00
)
func main() {
color.Yellow("Syncing media relations with Kitsu DB")
2018-03-28 22:26:19 +00:00
defer color.Green("Finished.")
2017-11-01 10:16:31 +00:00
defer arn.Node.Close()
2017-10-17 10:55:54 +00:00
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
2017-11-01 10:16:31 +00:00
if !arn.DB.Exists("Anime", animeID) {
2017-10-17 14:58:28 +00:00
continue
}
2017-11-01 10:16:31 +00:00
if !arn.DB.Exists("Anime", destinationAnimeID) {
2017-10-17 14:58:28 +00:00
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 {
2017-11-01 10:16:31 +00:00
animeRelations.Save()
2017-10-17 10:55:54 +00:00
}
}