105 lines
2.1 KiB
Go
Raw Normal View History

2017-11-27 20:19:17 +00:00
package main
import (
"fmt"
"time"
"github.com/animenotifier/arn"
2017-11-29 05:40:34 +00:00
"github.com/animenotifier/mal"
2017-11-27 20:19:17 +00:00
"github.com/fatih/color"
)
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() {
companies[company.Name.English] = company
}
2017-11-27 20:19:17 +00:00
for anime := range arn.StreamAnime() {
malID := anime.GetMapping("myanimelist/anime")
if malID == "" {
continue
}
importCompanies(anime, malID)
}
for name, company := range companies {
fmt.Println(name)
company.Save()
}
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 {
fmt.Println(err)
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) {
company, exists := companies[producer.Name]
2017-11-27 20:36:59 +00:00
if !exists {
now = now.Add(-time.Second)
company = &arn.Company{
ID: arn.GenerateID("Company"),
Name: arn.CompanyName{
2017-11-29 05:40:34 +00:00
English: producer.Name,
2017-11-27 20:36:59 +00:00
},
Created: now.UTC().Format(time.RFC3339),
CreatedBy: "",
Mappings: []*arn.Mapping{
&arn.Mapping{
Service: "myanimelist/producer",
2017-11-29 05:40:34 +00:00
ServiceID: producer.ID,
2017-11-27 20:19:17 +00:00
},
2017-11-27 20:36:59 +00:00
},
Links: []*arn.Link{},
Tags: []string{},
2018-03-07 02:48:42 +00:00
LikeableImplementation: arn.LikeableImplementation{
Likes: []string{},
},
2017-11-27 20:19:17 +00:00
}
2017-11-29 05:40:34 +00:00
companies[producer.Name] = company
2017-11-27 20:36:59 +00:00
}
switch companyType {
case "studio":
2017-11-27 20:19:17 +00:00
anime.StudioIDs = append(anime.StudioIDs, company.ID)
2017-11-27 20:36:59 +00:00
case "producer":
anime.ProducerIDs = append(anime.ProducerIDs, company.ID)
case "licensor":
anime.LicensorIDs = append(anime.LicensorIDs, 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
}