diff --git a/jobs/mal-import/mal-import.go b/jobs/mal-parse/mal-parse.go similarity index 97% rename from jobs/mal-import/mal-import.go rename to jobs/mal-parse/mal-parse.go index 719a94cb..eb0cdb63 100644 --- a/jobs/mal-import/mal-import.go +++ b/jobs/mal-parse/mal-parse.go @@ -16,7 +16,7 @@ import ( ) func main() { - color.Yellow("Importing MAL anime") + color.Yellow("Parsing MAL files") defer color.Green("Finished.") defer arn.Node.Close() diff --git a/jobs/mal-sync/mal-sync.go b/jobs/mal-sync/mal-sync.go new file mode 100644 index 00000000..cbf72277 --- /dev/null +++ b/jobs/mal-sync/mal-sync.go @@ -0,0 +1,190 @@ +package main + +import ( + "fmt" + + "github.com/animenotifier/arn" + "github.com/animenotifier/mal" + "github.com/fatih/color" +) + +var ( + malDB = arn.Node.Namespace("mal").RegisterTypes((*mal.Anime)(nil)) + characterFinder = arn.NewCharacterFinder("myanimelist/character") +) + +func main() { + color.Yellow("Syncing with MAL DB") + + defer color.Green("Finished.") + defer arn.Node.Close() + + // Sync the most important ones first + allAnime := arn.AllAnime() + arn.SortAnimeByQuality(allAnime) + + for _, anime := range allAnime { + malID := anime.GetMapping("myanimelist/anime") + + if malID == "" { + continue + } + + sync(anime, malID) + } +} + +func sync(anime *arn.Anime, malID string) { + obj, err := malDB.Get("Anime", malID) + + if err != nil { + fmt.Println(err) + return + } + + malAnime := obj.(*mal.Anime) + + // Log title + fmt.Printf("%s %s\n", color.CyanString(anime.Title.Canonical), malID) + + if len(anime.Genres) == 0 && len(malAnime.Genres) > 0 { + fmt.Println("Genres:", malAnime.Genres) + anime.Genres = malAnime.Genres + } + + if anime.EpisodeCount == 0 && malAnime.EpisodeCount != 0 { + fmt.Println("EpisodeCount:", malAnime.EpisodeCount) + anime.EpisodeCount = malAnime.EpisodeCount + } + + if anime.EpisodeLength == 0 && malAnime.EpisodeLength != 0 { + fmt.Println("EpisodeLength:", malAnime.EpisodeLength) + anime.EpisodeLength = malAnime.EpisodeLength + } + + if anime.StartDate == "" && malAnime.StartDate != "" { + fmt.Println("StartDate:", malAnime.StartDate) + anime.StartDate = malAnime.StartDate + } + + if anime.EndDate == "" && malAnime.EndDate != "" { + fmt.Println("EndDate:", malAnime.EndDate) + anime.EndDate = malAnime.EndDate + } + + if anime.Source == "" && malAnime.Source != "" { + fmt.Println("Source:", malAnime.Source) + anime.Source = malAnime.Source + } + + if anime.Title.Japanese == "" && malAnime.JapaneseTitle != "" { + fmt.Println("JapaneseTitle:", malAnime.JapaneseTitle) + anime.Title.Japanese = malAnime.JapaneseTitle + } + + if anime.Title.English == "" && malAnime.EnglishTitle != "" { + fmt.Println("EnglishTitle:", malAnime.EnglishTitle) + anime.Title.English = malAnime.EnglishTitle + } + + // Check for existence of characters + animeCharacters := anime.Characters() + modifiedCharacters := false + + for _, malAnimeCharacter := range malAnime.Characters { + // Make sure we have no invalid entries + if malAnimeCharacter.ID == "" || malAnimeCharacter.Role == "" { + fmt.Println("Skip:", malAnimeCharacter) + continue + } + + animeCharacter := animeCharacters.FindByMapping("myanimelist/character", malAnimeCharacter.ID) + + if animeCharacter != nil { + continue + } + + obj, err := malDB.Get("Character", malAnimeCharacter.ID) + + // If we don't have the MAL character in the DB, + // we can't import anything here. + if err != nil { + continue + } + + malCharacter := obj.(*mal.Character) + + if malCharacter.ID == "" || malCharacter.Name == "" || malCharacter.ImagePath == "" { + fmt.Println("Skip character:", malAnimeCharacter.ID) + continue + } + + fmt.Println("Importing MAL AnimeCharacter:", malAnimeCharacter.ID, "as", malAnimeCharacter.Role) + + // Import character if needed + character := characterFinder.GetCharacter(malAnimeCharacter.ID) + + if character == nil { + character = importCharacter(malCharacter) + } else { + fmt.Println("Found existing character:", character) + + // Download image if missing + if !character.HasImage() { + fmt.Println("Downloading missing image for character:", character) + character.DownloadImage(malCharacter.ImageLink()) + + // Cancel import if that character has no image + if err != nil { + color.Red(err.Error()) + continue + } + } + } + + // If import failed, continue + if character == nil { + continue + } + + // Add to anime characters + err = animeCharacters.Add(&arn.AnimeCharacter{ + CharacterID: character.ID, + Role: malAnimeCharacter.Role, + }) + + if err != nil { + color.Red(err.Error()) + } + + modifiedCharacters = true + } + + if modifiedCharacters { + animeCharacters.Save() + } + + anime.Save() +} + +func importCharacter(malCharacter *mal.Character) *arn.Character { + fmt.Println("Importing MAL Character:", malCharacter.ID, malCharacter.Name, malCharacter.ImagePath) + + character := arn.NewCharacter() + character.Name.Canonical = malCharacter.Name + + // Cancel the import if image could not be fetched + err := character.DownloadImage(malCharacter.ImageLink()) + + if err != nil { + color.Red(err.Error()) + return nil + } + + // Add mapping + character.SetMapping("myanimelist/character", malCharacter.ID) + + // Save character in DB + character.Save() + return character +} diff --git a/patches/import-mal-anime-data/import-mal-anime-data.go b/patches/import-mal-anime-data/import-mal-anime-data.go deleted file mode 100644 index dcbb66f9..00000000 --- a/patches/import-mal-anime-data/import-mal-anime-data.go +++ /dev/null @@ -1,84 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/animenotifier/arn" - "github.com/animenotifier/mal" - "github.com/fatih/color" -) - -var malDB = arn.Node.Namespace("mal").RegisterTypes((*mal.Anime)(nil)) - -func main() { - defer arn.Node.Close() - color.Yellow("Importing MAL data") - - for anime := range arn.StreamAnime() { - malID := anime.GetMapping("myanimelist/anime") - - if malID == "" { - continue - } - - sync(anime, malID) - } - - color.Green("Finished importing MAL data") -} - -func sync(anime *arn.Anime, malID string) { - obj, err := malDB.Get("Anime", malID) - - if err != nil { - fmt.Println(err) - return - } - - malAnime := obj.(*mal.Anime) - - // Log title - fmt.Printf("%s %s\n", color.CyanString(anime.Title.Canonical), malID) - - if len(anime.Genres) == 0 && len(malAnime.Genres) > 0 { - fmt.Println("Genres:", malAnime.Genres) - anime.Genres = malAnime.Genres - } - - if anime.EpisodeCount == 0 && malAnime.EpisodeCount != 0 { - fmt.Println("EpisodeCount:", malAnime.EpisodeCount) - anime.EpisodeCount = malAnime.EpisodeCount - } - - if anime.EpisodeLength == 0 && malAnime.EpisodeLength != 0 { - fmt.Println("EpisodeLength:", malAnime.EpisodeLength) - anime.EpisodeLength = malAnime.EpisodeLength - } - - if anime.StartDate == "" && malAnime.StartDate != "" { - fmt.Println("StartDate:", malAnime.StartDate) - anime.StartDate = malAnime.StartDate - } - - if anime.EndDate == "" && malAnime.EndDate != "" { - fmt.Println("EndDate:", malAnime.EndDate) - anime.EndDate = malAnime.EndDate - } - - if anime.Source == "" && malAnime.Source != "" { - fmt.Println("Source:", malAnime.Source) - anime.Source = malAnime.Source - } - - if anime.Title.Japanese == "" && malAnime.JapaneseTitle != "" { - fmt.Println("JapaneseTitle:", malAnime.JapaneseTitle) - anime.Title.Japanese = malAnime.JapaneseTitle - } - - if anime.Title.English == "" && malAnime.EnglishTitle != "" { - fmt.Println("EnglishTitle:", malAnime.EnglishTitle) - anime.Title.English = malAnime.EnglishTitle - } - - anime.Save() -}