Use new character structure

This commit is contained in:
Eduard Urbach 2018-03-27 05:25:25 +02:00
parent a1cfe624ae
commit fd6ab8eb02
9 changed files with 39 additions and 130 deletions

View File

@ -2,6 +2,8 @@ package main
import (
"fmt"
"path"
"strconv"
"strings"
"github.com/animenotifier/arn"
@ -18,10 +20,27 @@ func main() {
for kitsuCharacter := range kitsuCharacters {
character := &arn.Character{
ID: kitsuCharacter.ID,
Name: kitsuCharacter.Attributes.Name,
Image: kitsu.FixImageURL(kitsuCharacter.Attributes.Image.Original),
Name: arn.CharacterName{
Canonical: kitsuCharacter.Attributes.Name,
},
Image: arn.CharacterImage{
Extension: path.Ext(kitsu.FixImageURL(kitsuCharacter.Attributes.Image.Original)),
},
Description: kitsuCharacter.Attributes.Description,
Attributes: []*arn.CharacterAttribute{},
Mappings: []*arn.Mapping{
&arn.Mapping{
Service: "kitsu/character",
ServiceID: kitsuCharacter.ID,
},
},
}
if kitsuCharacter.Attributes.MalID != 0 {
character.Mappings = append(character.Mappings, &arn.Mapping{
Service: "myanimelist/character",
ServiceID: strconv.Itoa(kitsuCharacter.Attributes.MalID),
})
}
// We use markdown, so replace <br/> with two line breaks.

View File

@ -1,52 +0,0 @@
package main
import (
"fmt"
"time"
"github.com/animenotifier/jikan"
"github.com/animenotifier/arn"
"github.com/fatih/color"
)
var jikanDB = arn.Node.Namespace("jikan")
func main() {
color.Yellow("Syncing with Jikan API")
defer arn.Node.Close()
count := 0
for anime := range arn.StreamAnime() {
malID := anime.GetMapping("myanimelist/anime")
if malID != "" {
sync(anime, malID)
count++
}
}
color.Green("Finished syncing %d anime.", count)
// Give OS some time to write buffers, just to be safe
time.Sleep(10 * time.Second)
}
func sync(anime *arn.Anime, malID string) {
fmt.Printf("%s %s (MAL: %s)\n", anime.ID, anime.Title.Canonical, malID)
if jikanDB.Exists("Anime", malID) {
return
}
time.Sleep(2 * time.Second)
jikanAnime, err := jikan.GetAnime(malID)
if err == nil {
jikanDB.Set("Anime", malID, jikanAnime)
return
}
fmt.Printf("Error fetching %s: %v\n", malID, err)
}

View File

@ -1,63 +0,0 @@
package main
import (
"fmt"
"time"
"github.com/animenotifier/jikan"
"github.com/animenotifier/arn"
"github.com/fatih/color"
)
var jikanDB = arn.Node.Namespace("jikan")
func main() {
color.Yellow("Syncing characters with Jikan API")
defer arn.Node.Close()
allAnime := jikanDB.All("Anime")
count := 0
for animeObj := range allAnime {
anime := animeObj.(*jikan.Anime)
if len(anime.Character) == 0 {
continue
}
fmt.Println(anime.Title)
for _, character := range anime.Character {
characterID := jikan.GetCharacterIDFromURL(character.URL)
if characterID == "" {
fmt.Println("Invalid character ID")
continue
}
fetchCharacter(characterID)
}
}
color.Green("Finished syncing %d characters.", count)
}
func fetchCharacter(malCharacterID string) {
fmt.Printf("Fetching character ID %s\n", malCharacterID)
if jikanDB.Exists("Character", malCharacterID) {
return
}
time.Sleep(500 * time.Millisecond)
character, err := jikan.GetCharacter(malCharacterID)
if err == nil {
jikanDB.Set("Character", malCharacterID, character)
return
}
fmt.Printf("Error fetching %s: %v\n", malCharacterID, err)
}

View File

@ -1,7 +1,7 @@
component Character(character *arn.Character)
a.character(href="/character/" + character.ID)
img.character-image.lazy(data-src=character.Image, alt=character.Name, title=character.Name)
img.character-image.lazy(data-src=character.ImageLink("medium"), alt=character.Name, title=character.Name)
component CharacterSmall(character *arn.Character)
a.character(href="/character/" + character.ID)
img.character-image.character-image-small.lazy(data-src=character.Image, alt=character.Name, title=character.Name)
img.character-image.character-image-small.lazy(data-src=character.ImageLink("small"), alt=character.Name, title=character.Name)

View File

@ -49,8 +49,8 @@ func Get(ctx *aero.Context) string {
ctx.Data = &arn.OpenGraph{
Tags: map[string]string{
"og:title": character.Name,
"og:image": "https:" + character.Image,
"og:title": character.Name.Canonical,
"og:image": "https:" + character.ImageLink("large"),
"og:url": "https://" + ctx.App.Config.Domain + character.Link(),
"og:site_name": "notify.moe",
"og:description": description,
@ -61,7 +61,7 @@ func Get(ctx *aero.Context) string {
},
Meta: map[string]string{
"description": description,
"keywords": character.Name + ",anime,character",
"keywords": character.Name.Canonical + ",anime,character",
},
}

View File

@ -3,11 +3,16 @@ component CharacterDetails(character *arn.Character, characterAnime []*arn.Anime
.character-left-column
.character-header
.character-image-container
img.character-image-fullsize(src=character.Image, alt=character.Name)
img.character-image-fullsize(src=character.ImageLink("large"), alt=character.Name.Canonical)
.character-description-container
h1.character-name= character.Name
h1.character-name= character.Name.Canonical
.anime-alternative-title
if character.Name.Japanese != ""
Japanese(character.Name.Japanese)
else
Japanese("日本語の名前無し")
.character-description!= markdown.Render(character.Description)
h3 Anime

View File

@ -29,7 +29,7 @@ func Edit(ctx *aero.Context) string {
}
if quote.Character() != nil {
ctx.Data.(*arn.OpenGraph).Tags["og:image"] = quote.Character().Image
ctx.Data.(*arn.OpenGraph).Tags["og:image"] = quote.Character().ImageLink("large")
}
return ctx.HTML(components.QuoteTabs(quote, user) + editform.Render(quote, "Edit quote", user))

View File

@ -32,8 +32,8 @@ func Get(ctx *aero.Context) string {
character, _ := arn.GetCharacter(quote.CharacterID)
if character != nil {
openGraph.Tags["og:title"] = character.Name + "'s quote"
openGraph.Tags["og:image"] = "https:" + character.Image
openGraph.Tags["og:title"] = character.Name.Canonical + "'s quote"
openGraph.Tags["og:image"] = "https:" + character.ImageLink("large")
}
ctx.Data = openGraph

View File

@ -157,7 +157,7 @@ func RenderField(b *bytes.Buffer, v *reflect.Value, field reflect.StructField, i
character, err := arn.GetCharacter(characterID)
if err == nil {
b.WriteString(components.EditFormImagePreview(character.Link(), character.Image, false))
b.WriteString(components.EditFormImagePreview(character.Link(), character.ImageLink("small"), false))
}
case "":