99 lines
2.1 KiB
Go
Raw Normal View History

2017-11-27 20:19:17 +00:00
package main
import (
"fmt"
"time"
2019-04-23 05:52:55 +00:00
"github.com/akyoto/color"
2017-11-27 20:19:17 +00:00
"github.com/animenotifier/arn"
2017-11-29 05:40:34 +00:00
"github.com/animenotifier/mal"
2017-11-27 20:19:17 +00:00
)
2017-11-29 05:40:34 +00:00
var malDB = arn.Node.Namespace("mal").RegisterTypes((*mal.Anime)(nil))
2017-11-27 20:19:17 +00:00
var companies = map[string]*arn.Company{}
var now = time.Now()
func main() {
defer arn.Node.Close()
color.Yellow("Importing companies")
2017-11-29 13:30:38 +00:00
for company := range arn.StreamCompanies() {
2018-04-03 08:19:52 +00:00
malID := company.GetMapping("myanimelist/producer")
if malID == "" {
continue
}
companies[malID] = company
2017-11-29 13:30:38 +00:00
}
2017-11-27 20:19:17 +00:00
for anime := range arn.StreamAnime() {
malID := anime.GetMapping("myanimelist/anime")
if malID == "" {
continue
}
importCompanies(anime, malID)
}
color.Green("Finished importing %d companies", len(companies))
}
func importCompanies(anime *arn.Anime, malID string) {
2017-11-29 05:40:34 +00:00
obj, err := malDB.Get("Anime", malID)
2017-11-27 20:19:17 +00:00
if err != nil {
2018-04-03 08:19:52 +00:00
fmt.Printf("%s: %s (invalid MAL ID)\n", color.YellowString(anime.ID), color.RedString(malID))
2017-11-27 20:19:17 +00:00
return
}
2017-11-29 05:40:34 +00:00
malAnime := obj.(*mal.Anime)
2017-11-27 20:19:17 +00:00
2017-11-29 05:40:34 +00:00
for _, producer := range malAnime.Studios {
importByName(anime, "studio", producer)
2017-11-27 20:36:59 +00:00
}
2017-11-27 20:19:17 +00:00
2017-11-29 05:40:34 +00:00
for _, producer := range malAnime.Producers {
importByName(anime, "producer", producer)
2017-11-27 20:36:59 +00:00
}
2017-11-27 20:19:17 +00:00
2017-11-29 05:40:34 +00:00
for _, producer := range malAnime.Licensors {
importByName(anime, "licensor", producer)
2017-11-27 20:36:59 +00:00
}
}
2017-11-29 05:40:34 +00:00
func importByName(anime *arn.Anime, companyType string, producer *mal.Producer) {
2018-04-03 08:19:52 +00:00
company, exists := companies[producer.ID]
2017-11-27 20:36:59 +00:00
2018-04-03 08:19:52 +00:00
// If we encounter a company that has not been added yet, save the new company
2017-11-27 20:36:59 +00:00
if !exists {
2018-04-03 08:19:52 +00:00
fmt.Println("Adding new company:", producer.Name)
// Subtract one second every time we create a new company
// so that they don't all end up with the same creation date.
2017-11-27 20:36:59 +00:00
now = now.Add(-time.Second)
2018-04-03 08:19:52 +00:00
// Create new company
company = arn.NewCompany()
company.Name.English = producer.Name
company.Created = now.UTC().Format(time.RFC3339)
company.SetMapping("myanimelist/producer", producer.ID)
company.Save()
2017-11-27 20:19:17 +00:00
2018-04-03 08:19:52 +00:00
// Add company to the global ID map
companies[producer.ID] = company
2017-11-27 20:36:59 +00:00
}
switch companyType {
case "studio":
2018-04-03 08:19:52 +00:00
anime.AddStudio(company.ID)
2017-11-27 20:36:59 +00:00
case "producer":
2018-04-03 08:19:52 +00:00
anime.AddProducer(company.ID)
2017-11-27 20:36:59 +00:00
case "licensor":
2018-04-03 08:19:52 +00:00
anime.AddLicensor(company.ID)
2017-11-27 20:19:17 +00:00
}
2017-11-27 20:36:59 +00:00
anime.Save()
2017-11-27 20:19:17 +00:00
}