Added MAL avatar source
This commit is contained in:
@ -1,17 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"time"
|
||||
|
||||
"github.com/animenotifier/arn"
|
||||
"github.com/parnurzeal/gorequest"
|
||||
gravatar "github.com/ungerik/go-gravatar"
|
||||
)
|
||||
|
||||
// Gravatar - https://gravatar.com/
|
||||
type Gravatar struct{}
|
||||
type Gravatar struct {
|
||||
Rating string
|
||||
RequestLimiter *time.Ticker
|
||||
}
|
||||
|
||||
// GetAvatar returns the Gravatar image for a user (if available).
|
||||
func (source *Gravatar) GetAvatar(user *arn.User) *Avatar {
|
||||
@ -21,26 +22,11 @@ func (source *Gravatar) GetAvatar(user *arn.User) *Avatar {
|
||||
}
|
||||
|
||||
// Build URL
|
||||
gravatarURL := gravatar.Url(user.Email) + "?s=" + fmt.Sprint(arn.AvatarMaxSize) + "&d=404&r=pg"
|
||||
gravatarURL := gravatar.Url(user.Email) + "?s=" + fmt.Sprint(arn.AvatarMaxSize) + "&d=404&r=" + source.Rating
|
||||
|
||||
// Wait for request limiter to allow us to send a request
|
||||
<-source.RequestLimiter.C
|
||||
|
||||
// Download
|
||||
response, data, networkErr := gorequest.New().Get(gravatarURL).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,
|
||||
}
|
||||
return AvatarFromURL(gravatarURL, user)
|
||||
}
|
||||
|
Reference in New Issue
Block a user