Finished company import

This commit is contained in:
Eduard Urbach 2017-11-29 06:40:34 +01:00
parent 082a01c4be
commit 82de7b3f1c
2 changed files with 15 additions and 30 deletions

View File

@ -2,15 +2,14 @@ package main
import ( import (
"fmt" "fmt"
"strings"
"time" "time"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/animenotifier/jikan" "github.com/animenotifier/mal"
"github.com/fatih/color" "github.com/fatih/color"
) )
var jikanDB = arn.Node.Namespace("jikan") var malDB = arn.Node.Namespace("mal").RegisterTypes((*mal.Anime)(nil))
var companies = map[string]*arn.Company{} var companies = map[string]*arn.Company{}
var now = time.Now() var now = time.Now()
@ -37,44 +36,30 @@ func main() {
} }
func importCompanies(anime *arn.Anime, malID string) { func importCompanies(anime *arn.Anime, malID string) {
obj, err := jikanDB.Get("Anime", malID) obj, err := malDB.Get("Anime", malID)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
jikanAnime := obj.(*jikan.Anime) malAnime := obj.(*mal.Anime)
for _, info := range jikanAnime.Studio { for _, producer := range malAnime.Studios {
importByName(anime, "studio", info) importByName(anime, "studio", producer)
} }
for _, info := range jikanAnime.Producer { for _, producer := range malAnime.Producers {
importByName(anime, "producer", info) importByName(anime, "producer", producer)
} }
for _, info := range jikanAnime.Licensor { for _, producer := range malAnime.Licensors {
importByName(anime, "licensor", info) importByName(anime, "licensor", producer)
} }
} }
func importByName(anime *arn.Anime, companyType string, info []string) { func importByName(anime *arn.Anime, companyType string, producer *mal.Producer) {
studioMALID := info[0] company, exists := companies[producer.Name]
slashPos := strings.Index(studioMALID, "/")
if slashPos != -1 {
studioMALID = studioMALID[:slashPos]
}
studioName := info[1]
htmlPos := strings.Index(studioName, "<")
if htmlPos != -1 {
studioName = studioName[:htmlPos]
}
company, exists := companies[studioName]
if !exists { if !exists {
now = now.Add(-time.Second) now = now.Add(-time.Second)
@ -82,14 +67,14 @@ func importByName(anime *arn.Anime, companyType string, info []string) {
company = &arn.Company{ company = &arn.Company{
ID: arn.GenerateID("Company"), ID: arn.GenerateID("Company"),
Name: arn.CompanyName{ Name: arn.CompanyName{
English: studioName, English: producer.Name,
}, },
Created: now.UTC().Format(time.RFC3339), Created: now.UTC().Format(time.RFC3339),
CreatedBy: "", CreatedBy: "",
Mappings: []*arn.Mapping{ Mappings: []*arn.Mapping{
&arn.Mapping{ &arn.Mapping{
Service: "myanimelist/producer", Service: "myanimelist/producer",
ServiceID: studioMALID, ServiceID: producer.ID,
Created: arn.DateTimeUTC(), Created: arn.DateTimeUTC(),
CreatedBy: "", CreatedBy: "",
}, },
@ -99,7 +84,7 @@ func importByName(anime *arn.Anime, companyType string, info []string) {
Likes: []string{}, Likes: []string{},
} }
companies[studioName] = company companies[producer.Name] = company
} }
switch companyType { switch companyType {