Added MAL avatar source

This commit is contained in:
2017-06-13 17:06:30 +02:00
parent 688c09760b
commit 6836fea857
4 changed files with 111 additions and 34 deletions

View File

@ -1,9 +1,12 @@
package main
import (
"bytes"
"fmt"
"image"
"github.com/animenotifier/arn"
"github.com/parnurzeal/gorequest"
)
// Avatar represents a single image and the name of the format.
@ -13,3 +16,32 @@ type Avatar struct {
Data []byte
Format string
}
// String returns a text representation of the format, width and height.
func (avatar *Avatar) String() string {
return fmt.Sprint(avatar.Format, " | ", avatar.Image.Bounds().Dx(), "x", avatar.Image.Bounds().Dy())
}
// AvatarFromURL downloads and decodes the image from an URL and creates an Avatar.
func AvatarFromURL(url string, user *arn.User) *Avatar {
// Download
response, data, networkErr := gorequest.New().Get(url).EndBytes()
if networkErr != nil || response.StatusCode != 200 {
return nil
}
// Decode
img, format, decodeErr := image.Decode(bytes.NewReader(data))
if decodeErr != nil {
return nil
}
return &Avatar{
User: user,
Image: img,
Data: data,
Format: format,
}
}