-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgravatar.go
257 lines (207 loc) · 5.5 KB
/
gravatar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package gravatar
import (
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
// GravatarProfile stores profile information associated with an email.
type GravatarProfile struct {
AboutMe string
Accounts []struct {
ShortName string
Domain string
Url string
Verified bool `json:",string"`
Username string
Display string
}
CurrentLocation string
DisplayName string
Emails []struct {
Primary bool `json:",string"`
Value string
}
Hash string
Id int `json:",string"`
Ims []struct {
Type string
Value string
}
Name struct {
Family string
Formatted string
Given string
}
PhoneNumbers []struct {
Type string
Value string
}
Photos []struct {
Type string
Value string
}
PreferredUsername string
ProfileBackground struct {
Color string
Position string
Repeat string
Url string
}
ProfileUrl string
ThumbnailUrl string
Urls []struct {
Title string
Value string
}
}
type rating string
const gravatarHost = "gravatar.com"
// List of (optional) values for a "default action" option for GetAvatar.
// Each option defines what GetAvatar has to do in case of non-existing
// user.
const (
// DefaultBlank defaults to a transparent PNG image.
DefaultBlank = "blank"
// DefaultError defaults to an error.
DefaultError = "404"
// DefaultIdentIcon defaults to a generated geometric pattern.
DefaultIdentIcon = "identicon"
// DefaultMonster defaults to a generated 'monster' with different colors
// and faces.
DefaultMonster = "monsterid"
// DefaultMysteryMan defaults to a simple, cartoon-style silhouetted outline
// of a person.
DefaultMysteryMan = "mm"
// DefaultRetro defaults to a generated 8-bit arcade-style pixelated faces.
DefaultRetro = "retro"
// DefaultWavatar defaults to a generated faces with differing features and
// backgrounds.
DefaultWavatar = "wavatar"
)
// List of (optional) values to specify allowed rating (up to and including
// that).
// If the requested email doesn't have any image of allowed level, a default
// image will be used.
const (
// RatingG is suitable for display on all websites with any audience type.
RatingG = rating("g")
// RatingPG may contain rude gestures, provocatively dressed individuals, the
// lesser swear words, or mild violence.
RatingPG = rating("pg")
// RatingR may contain such things as harsh profanity, intense violence,
// nudity, or hard drug use.
RatingR = rating("r")
// RatingX may contain hardcore sexual imagery or extremely disturbing
// violence.
RatingX = rating("x")
)
var client = new(http.Client)
// EmailHash converts an email to lowercase and returns its MD5 hash as hex
// string.
func EmailHash(email string) string {
m := md5.New()
io.WriteString(m, strings.ToLower(email))
return fmt.Sprintf("%x", m.Sum(nil))
}
// GetAvatar does a HTTP(S) request and returns an avatar image.
//
// Optional arguments include Default* (default actions), image size and
// Rating* (rating level, default is RatingG).
//
// Instead of Default* predefined constants you may also use a direct URL to an
// image.
func GetAvatar(scheme, emailHash string, opts ...interface{}) (data []byte, err error) {
url := GetAvatarURL(scheme, emailHash, opts...)
err = run(url, get_avatar(&data))
return
}
// GetAvatarURL returns an URL to avatar image.
//
// Optional arguments include Default* (default actions), image size and
// Rating* (rating level, default is RatingG).
//
// Instead of Default* predefined constants you may also use a direct URL to an
// image.
func GetAvatarURL(scheme, emailHash string, opts ...interface{}) *url.URL {
url := &url.URL{
Scheme: scheme,
Host: gravatarHost,
Path: "/avatar/" + emailHash,
}
return SetAvatarURLOptions(url, opts...)
}
// GetProfile does a HTTP(S) request and returns gravatar profile.
func GetProfile(scheme, emailHash string) (g GravatarProfile, err error) {
url := &url.URL{
Scheme: scheme,
Host: gravatarHost,
Path: "/" + emailHash + ".json",
}
err = run(url, unmarshal_json(&g))
return
}
// SetAvatarURLOptions sets options for an URL to avatar image.
//
// Options include Default* (default actions), image size and Rating* (rating
// level, default is RatingG).
//
// Instead of Default* predefined constants you may also use a direct URL to an
// image.
//
// Calling SetAvatarURLOptions(url), e.g. without any options, resets the
// options.
func SetAvatarURLOptions(u *url.URL, opts ...interface{}) *url.URL {
values := make(url.Values)
for _, opt := range opts {
switch o := opt.(type) {
case int:
values.Set("s", strconv.Itoa(o))
case rating:
values.Set("r", string(o))
case string:
values.Set("d", o)
}
}
u.RawQuery = values.Encode()
return u
}
func get_avatar(dst *[]byte) func([]byte) error {
return func(data []byte) (err error) {
*dst = data[:]
return
}
}
func run(url *url.URL, f func([]byte) error) (err error) {
var res *http.Response
res, err = client.Get(url.String())
if err == nil {
var data []byte
defer res.Body.Close()
if data, err = ioutil.ReadAll(res.Body); err == nil {
if res.StatusCode == http.StatusOK {
err = f(data)
} else {
err = errors.New(string(data))
}
}
}
return
}
func unmarshal_json(g *GravatarProfile) func([]byte) error {
return func(data []byte) (err error) {
obj := struct {
Entries []GravatarProfile `json:"entry"`
}{[]GravatarProfile{}}
if err = json.Unmarshal(data, &obj); err == nil && len(obj.Entries) > 0 {
*g = obj.Entries[0]
}
return
}
}