Improved avatar generator

This commit is contained in:
Eduard Urbach 2017-07-08 17:31:04 +02:00
parent 567d146b0e
commit c910e82a0a
2 changed files with 59 additions and 2 deletions

View File

@ -0,0 +1,48 @@
package main
import (
"bytes"
"image"
"io/ioutil"
"github.com/animenotifier/arn"
)
var fileSystemLog = avatarLog.NewChannel("SSD")
// FileSystem loads avatar from the local filesystem.
type FileSystem struct {
Directory string
}
// GetAvatar returns the local image for the user.
func (source *FileSystem) GetAvatar(user *arn.User) *Avatar {
fullPath := arn.FindFileWithExtension(user.ID, source.Directory, arn.OriginalImageExtensions)
if fullPath == "" {
fileSystemLog.Error(user.Nick, "Not found on file system")
return nil
}
data, err := ioutil.ReadFile(fullPath)
if err != nil {
fileSystemLog.Error(user.Nick, err)
return nil
}
// Decode
img, format, decodeErr := image.Decode(bytes.NewReader(data))
if decodeErr != nil {
fileSystemLog.Error(user.Nick, decodeErr)
return nil
}
return &Avatar{
User: user,
Image: img,
Data: data,
Format: format,
}
}

View File

@ -54,6 +54,9 @@ func main() {
&MyAnimeList{ &MyAnimeList{
RequestLimiter: time.NewTicker(250 * time.Millisecond), RequestLimiter: time.NewTicker(250 * time.Millisecond),
}, },
&FileSystem{
Directory: "images/avatars/large/",
},
} }
// Define the avatar outputs // Define the avatar outputs
@ -118,7 +121,6 @@ func StartWorkers(queue chan *arn.User, work func(*arn.User)) {
// Work handles a single user. // Work handles a single user.
func Work(user *arn.User) { func Work(user *arn.User) {
fmt.Println(user.ID, "|", user.Nick)
user.AvatarExtension = "" user.AvatarExtension = ""
for _, source := range avatarSources { for _, source := range avatarSources {
@ -129,6 +131,13 @@ func Work(user *arn.User) {
continue continue
} }
sourceType := reflect.TypeOf(source).Elem().Name()
// Avoid quality loss (if it's on the file system, we don't need to write it again)
if sourceType == "FileSystem" {
continue
}
for _, writer := range avatarOutputs { for _, writer := range avatarOutputs {
err := writer.SaveAvatar(avatar) err := writer.SaveAvatar(avatar)
@ -137,7 +146,7 @@ func Work(user *arn.User) {
} }
} }
fmt.Println(color.GreenString("✔"), reflect.TypeOf(source).Elem().Name(), "|", user.Nick, "|", avatar) fmt.Println(color.GreenString("✔"), sourceType, "|", user.Nick, "|", avatar)
break break
} }