36 lines
822 B
Go
Raw Normal View History

2017-06-13 11:23:54 +00:00
package main
import (
"fmt"
2017-06-13 15:06:30 +00:00
"time"
2017-06-13 11:23:54 +00:00
"github.com/animenotifier/arn"
gravatar "github.com/ungerik/go-gravatar"
)
2017-06-16 11:48:11 +00:00
var gravatarLog = avatarLog.NewChannel("GRA")
2017-06-13 11:23:54 +00:00
// Gravatar - https://gravatar.com/
2017-06-13 15:06:30 +00:00
type Gravatar struct {
Rating string
RequestLimiter *time.Ticker
}
2017-06-13 11:23:54 +00:00
// GetAvatar returns the Gravatar image for a user (if available).
func (source *Gravatar) GetAvatar(user *arn.User) *Avatar {
// If the user has no Email registered we can't get a Gravatar.
if user.Email == "" {
2017-06-16 11:48:11 +00:00
gravatarLog.Error(user.Nick, "No Email")
2017-06-13 11:23:54 +00:00
return nil
}
// Build URL
2017-06-13 15:06:30 +00:00
gravatarURL := gravatar.Url(user.Email) + "?s=" + fmt.Sprint(arn.AvatarMaxSize) + "&d=404&r=" + source.Rating
2017-06-13 11:23:54 +00:00
2017-06-13 15:06:30 +00:00
// Wait for request limiter to allow us to send a request
<-source.RequestLimiter.C
2017-06-13 11:23:54 +00:00
2017-06-13 15:06:30 +00:00
// Download
return AvatarFromURL(gravatarURL, user)
2017-06-13 11:23:54 +00:00
}