116 lines
2.2 KiB
Go
Raw Normal View History

2017-11-27 20:19:17 +00:00
package main
import (
"fmt"
"strings"
"time"
"github.com/animenotifier/arn"
"github.com/animenotifier/jikan"
"github.com/fatih/color"
)
var jikanDB = arn.Node.Namespace("jikan")
var companies = map[string]*arn.Company{}
var now = time.Now()
func main() {
defer arn.Node.Close()
color.Yellow("Importing companies")
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) {
obj, err := jikanDB.Get("Anime", malID)
if err != nil {
fmt.Println(err)
return
}
jikanAnime := obj.(*jikan.Anime)
2017-11-27 20:36:59 +00:00
for _, info := range jikanAnime.Studio {
importByName(anime, "studio", info)
}
2017-11-27 20:19:17 +00:00
2017-11-27 20:36:59 +00:00
for _, info := range jikanAnime.Producer {
importByName(anime, "producer", info)
}
2017-11-27 20:19:17 +00:00
2017-11-27 20:36:59 +00:00
for _, info := range jikanAnime.Licensor {
importByName(anime, "licensor", info)
}
}
func importByName(anime *arn.Anime, companyType string, info []string) {
studioMALID := info[0]
slashPos := strings.Index(studioMALID, "/")
2017-11-27 20:19:17 +00:00
2017-11-27 20:36:59 +00:00
if slashPos != -1 {
studioMALID = studioMALID[:slashPos]
}
2017-11-27 20:19:17 +00:00
2017-11-27 20:36:59 +00:00
studioName := info[1]
htmlPos := strings.Index(studioName, "<")
if htmlPos != -1 {
studioName = studioName[:htmlPos]
}
company, exists := companies[studioName]
if !exists {
now = now.Add(-time.Second)
company = &arn.Company{
ID: arn.GenerateID("Company"),
Name: arn.CompanyName{
English: studioName,
},
Created: now.UTC().Format(time.RFC3339),
CreatedBy: "",
Mappings: []*arn.Mapping{
&arn.Mapping{
Service: "myanimelist/producer",
ServiceID: studioMALID,
Created: arn.DateTimeUTC(),
CreatedBy: "",
2017-11-27 20:19:17 +00:00
},
2017-11-27 20:36:59 +00:00
},
Links: []*arn.Link{},
Tags: []string{},
Likes: []string{},
2017-11-27 20:19:17 +00:00
}
2017-11-27 20:36:59 +00:00
companies[studioName] = company
}
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
}