-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgravatar.go
92 lines (75 loc) · 1.77 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
/*
Copyright (c) 2017, AverageSecurityGuy
# All rights reserved.
Gather any information Gravatar has about a particular email address.
Usage:
$ go run gravatar.go email_address
*/
package main
import (
"crypto/md5"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
func check(e error) {
if e != nil {
fmt.Printf("Error: %s\n", e.Error())
}
}
func get(url string) []byte {
resp, err := http.Get(url)
check(err)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
check(err)
if resp.StatusCode == 200 {
return body
} else {
fmt.Printf("Document not available: %d\n", resp.StatusCode)
return []byte("")
}
}
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: go run gravatar.go email_address")
os.Exit(1)
}
email := string(os.Args[1])
email = strings.ToLower(email)
email = strings.Trim(email, " ")
hash := fmt.Sprintf("%x", md5.Sum([]byte(email)))
/*
Get Information From Gravatar.
*/
fmt.Printf("Getting information for: %s\n", email)
url := fmt.Sprintf("https://www.gravatar.com/%s.json", hash)
data := get(url)
/*
Parse JSON Response
*/
var root map[string][]interface{}
err := json.Unmarshal(data, &root)
check(err)
for _, entry := range root["entry"] {
e := entry.(map[string]interface{})
fmt.Printf(" Profile Url: %s\n", e["profileUrl"])
fmt.Printf(" Preferred Username: %s\n", e["preferredUsername"])
fmt.Printf(" Display Name: %s\n", e["displayName"])
photos := e["photos"].([]interface{})
for i, photo := range photos {
p := photo.(map[string]interface{})
fmt.Printf(" Photo %d: %s\n", i+1, p["value"])
}
switch name := e["name"].(type) {
case map[string]interface{}:
fmt.Printf(" Name: %s\n", name["formatted"])
default:
// Do nothing
}
fmt.Println()
}
}