114 lines
2.8 KiB
Go
Raw Normal View History

2017-07-13 05:53:36 +00:00
package main
import (
"fmt"
2018-03-27 03:25:25 +00:00
"path"
"strconv"
2017-11-12 11:44:29 +00:00
"strings"
2017-07-13 05:53:36 +00:00
2019-04-23 05:52:55 +00:00
"github.com/akyoto/color"
2017-07-13 05:53:36 +00:00
"github.com/animenotifier/arn"
"github.com/animenotifier/kitsu"
)
func main() {
color.Yellow("Syncing characters with Kitsu DB")
2018-03-28 22:26:19 +00:00
defer color.Green("Finished.")
2017-11-01 10:16:31 +00:00
defer arn.Node.Close()
2017-07-13 05:53:36 +00:00
kitsuCharacters := kitsu.StreamCharacters()
for kitsuCharacter := range kitsuCharacters {
character := &arn.Character{
2018-04-25 21:46:00 +00:00
HasID: arn.HasID{
ID: kitsuCharacter.ID,
},
2018-03-27 03:25:25 +00:00
Name: arn.CharacterName{
2018-03-27 04:07:52 +00:00
Canonical: kitsuCharacter.Attributes.CanonicalName,
English: kitsuCharacter.Attributes.Names.En,
Japanese: kitsuCharacter.Attributes.Names.JaJp,
Synonyms: kitsuCharacter.Attributes.OtherNames,
2018-03-27 03:25:25 +00:00
},
Image: arn.CharacterImage{
Extension: path.Ext(kitsu.FixImageURL(kitsuCharacter.Attributes.Image.Original)),
},
2017-11-12 11:44:29 +00:00
Description: kitsuCharacter.Attributes.Description,
2017-11-12 12:41:19 +00:00
Attributes: []*arn.CharacterAttribute{},
2018-04-04 13:21:52 +00:00
HasMappings: arn.HasMappings{
Mappings: []*arn.Mapping{
2019-04-23 05:52:55 +00:00
{
2018-04-04 13:21:52 +00:00
Service: "kitsu/character",
ServiceID: kitsuCharacter.ID,
},
2018-03-27 03:25:25 +00:00
},
},
}
if kitsuCharacter.Attributes.MalID != 0 {
character.Mappings = append(character.Mappings, &arn.Mapping{
Service: "myanimelist/character",
ServiceID: strconv.Itoa(kitsuCharacter.Attributes.MalID),
})
2017-07-13 05:53:36 +00:00
}
2017-11-12 11:44:29 +00:00
// We use markdown, so replace <br/> with two line breaks.
character.Description = strings.Replace(character.Description, "<br/>", "\n\n", -1)
2017-11-12 12:09:05 +00:00
// Parse attributes like these:
// - Position: Club Manager
// - Height: 162 cm (5' 4")
// - Weight: 48 kg (106 lb)
// - Birthday: November 24
// - Hair color: Brown
// - Eyes: Blue (anime), Green (manga)
lines := strings.Split(character.Description, "\n\n")
finalLines := make([]string, 0, len(lines))
for _, line := range lines {
originalLine := line
if strings.HasPrefix(line, "(") {
line = strings.TrimPrefix(line, "(")
line = strings.TrimSuffix(line, ")")
}
line = strings.TrimSpace(line)
colonPos := strings.Index(line, ":")
2017-11-12 12:48:21 +00:00
if colonPos == -1 || colonPos < 2 || colonPos > 25 {
2017-11-12 12:09:05 +00:00
finalLines = append(finalLines, originalLine)
continue
}
key := line[:colonPos]
value := line[colonPos+1:]
value = strings.TrimSpace(value)
2017-11-12 12:48:21 +00:00
if key == "source" {
key = "Source"
}
2017-11-12 12:41:19 +00:00
character.Attributes = append(character.Attributes, &arn.CharacterAttribute{
Name: key,
Value: value,
})
2017-11-12 12:09:05 +00:00
fmt.Println(color.CyanString(key), color.YellowString(value))
}
character.Description = strings.Join(finalLines, "\n\n")
2018-04-25 23:14:23 +00:00
character.Description = strings.Trim(character.Description, "\n")
2017-11-01 10:16:31 +00:00
character.Save()
2017-11-12 11:44:29 +00:00
2018-03-27 04:07:52 +00:00
// Save Kitsu character in Kitsu DB
arn.Kitsu.Set("Character", kitsuCharacter.ID, kitsuCharacter)
// Log
2017-11-12 11:44:29 +00:00
fmt.Printf("%s %s %s\n", color.GreenString("✔"), character.ID, character.Name)
2017-07-13 05:53:36 +00:00
}
}