13
"xelbot.com/reprogl/container"
17
GravatarNotFound = errors.New("gravatar: not found")
22
chanBuf = make(chan struct{}, (runtime.NumCPU()/2)+1)
25
type MaybeGravatar interface {
26
NeedToCheckGravatar() bool
30
func tryGravatar(object MaybeGravatar, size int, logger *log.Logger) (image.Image, error) {
31
if !object.NeedToCheckGravatar() {
32
return nil, GravatarNotFound
35
hash := gravatarHash(object.GetEmail())
36
request, err := http.NewRequest(
39
"https://www.gravatar.com/avatar/%s?s=%d&d=404",
49
logger.Printf("[IMG] check gravatar hash %s on gravatar.com\n", hash)
50
resp, err := sendRequest(request)
55
if resp.StatusCode != http.StatusOK {
56
return nil, GravatarNotFound
59
contentType, ok := resp.Header["Content-Type"]
60
if !ok || !strings.HasPrefix(contentType[0], "image") {
61
return nil, GravatarNotFound
64
defer resp.Body.Close()
65
img, _, err := image.Decode(resp.Body)
73
func gravatarHash(s string) string {
74
return container.MD5(strings.ToLower(strings.TrimSpace(s)))
77
func sendRequest(req *http.Request) (*http.Response, error) {
79
defer func() { <-chanBuf }()
81
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11")
82
client := http.Client{
83
Timeout: 5 * time.Second,