Improved MAL company importer

This commit is contained in:
Eduard Urbach 2018-04-03 10:19:52 +02:00
parent 0d74291075
commit 0fc613011f

View File

@ -18,7 +18,13 @@ func main() {
color.Yellow("Importing companies") color.Yellow("Importing companies")
for company := range arn.StreamCompanies() { for company := range arn.StreamCompanies() {
companies[company.Name.English] = company malID := company.GetMapping("myanimelist/producer")
if malID == "" {
continue
}
companies[malID] = company
} }
for anime := range arn.StreamAnime() { for anime := range arn.StreamAnime() {
@ -31,11 +37,6 @@ func main() {
importCompanies(anime, malID) importCompanies(anime, malID)
} }
for name, company := range companies {
fmt.Println(name)
company.Save()
}
color.Green("Finished importing %d companies", len(companies)) color.Green("Finished importing %d companies", len(companies))
} }
@ -43,7 +44,7 @@ func importCompanies(anime *arn.Anime, malID string) {
obj, err := malDB.Get("Anime", malID) obj, err := malDB.Get("Anime", malID)
if err != nil { if err != nil {
fmt.Println(err) fmt.Printf("%s: %s (invalid MAL ID)\n", color.YellowString(anime.ID), color.RedString(malID))
return return
} }
@ -63,41 +64,34 @@ func importCompanies(anime *arn.Anime, malID string) {
} }
func importByName(anime *arn.Anime, companyType string, producer *mal.Producer) { func importByName(anime *arn.Anime, companyType string, producer *mal.Producer) {
company, exists := companies[producer.Name] company, exists := companies[producer.ID]
// If we encounter a company that has not been added yet, save the new company
if !exists { if !exists {
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.
now = now.Add(-time.Second) now = now.Add(-time.Second)
company = &arn.Company{ // Create new company
ID: arn.GenerateID("Company"), company = arn.NewCompany()
Name: arn.CompanyName{ company.Name.English = producer.Name
English: producer.Name, company.Created = now.UTC().Format(time.RFC3339)
}, company.SetMapping("myanimelist/producer", producer.ID)
Created: now.UTC().Format(time.RFC3339), company.Save()
CreatedBy: "",
Mappings: []*arn.Mapping{
&arn.Mapping{
Service: "myanimelist/producer",
ServiceID: producer.ID,
},
},
Links: []*arn.Link{},
Tags: []string{},
LikeableImplementation: arn.LikeableImplementation{
Likes: []string{},
},
}
companies[producer.Name] = company // Add company to the global ID map
companies[producer.ID] = company
} }
switch companyType { switch companyType {
case "studio": case "studio":
anime.StudioIDs = append(anime.StudioIDs, company.ID) anime.AddStudio(company.ID)
case "producer": case "producer":
anime.ProducerIDs = append(anime.ProducerIDs, company.ID) anime.AddProducer(company.ID)
case "licensor": case "licensor":
anime.LicensorIDs = append(anime.LicensorIDs, company.ID) anime.AddLicensor(company.ID)
} }
anime.Save() anime.Save()