-
Notifications
You must be signed in to change notification settings - Fork 1
/
sharecount.go
153 lines (126 loc) · 2.96 KB
/
sharecount.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
package sharecount
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/ikeikeikeike/gopkg/convert"
)
type Result struct {
Service string
Count int
}
var (
context http.ResponseWriter
)
func Fetch(url string) (results []Result) {
c := make(chan Result)
go func() { c <- hatena(url) }()
go func() { c <- twitter(url) }()
go func() { c <- facebook(url) }()
// go func() { c <- linkedin(url) }()
timeout := time.After(5000 * time.Millisecond)
// for i := 0; i < 4; i++ {
for i := 0; i < 3; i++ {
select {
case result := <-c:
results = append(results, result)
case <-timeout:
log.Println("timed out")
return
}
}
return
}
func fetchUrl(url string) (body []byte, err error) {
response, err := http.Get(url)
if err != nil {
return
}
defer response.Body.Close()
body, err = ioutil.ReadAll(response.Body)
if err != nil {
return
}
return
}
func hatena(url string) (result Result) {
result.Service = "hatena"
result.Count = -1
endpoint := fmt.Sprintf("http://api.b.st-hatena.com/entry.count?url=%s", url)
body, err := fetchUrl(endpoint)
if err != nil {
log.Printf("Error in getting response from Hatena: %s", err.Error())
return
}
i, _ := convert.StrTo(string(body)).Int()
result.Count = i
return
}
func twitter(url string) (result Result) {
result.Service = "twitter"
result.Count = -1
endpoint := fmt.Sprintf("http://urls.api.twitter.com/1/urls/count.json?url=%s", url)
body, err := fetchUrl(endpoint)
if err != nil {
log.Printf("Error in getting response from Twitter: %s", err.Error())
return
}
type twitterResult struct {
Count int
}
var t twitterResult
err = json.Unmarshal(body, &t)
if err != nil {
log.Printf("Error in parsing response from Twitter: %s", err.Error())
return
}
result.Count = t.Count
return
}
func facebook(url string) (result Result) {
result.Service = "facebook"
result.Count = -1
endpoint := fmt.Sprintf("http://api.ak.facebook.com/restserver.php?v=1.0&method=links.getStats&format=json&urls=%s", url)
body, err := fetchUrl(endpoint)
if err != nil {
log.Printf("Error in getting response from Facebook: %s", err.Error())
return
}
type facebookResult struct {
Total_count int
}
var f []facebookResult
err = json.Unmarshal(body, &f)
if err != nil {
log.Printf("Error in parsing response from Facebook: %s", err.Error())
return
}
if len(f) > 0 {
result.Count = f[0].Total_count
}
return
}
func linkedin(url string) (result Result) {
result.Service = "linkedin"
result.Count = -1
endpoint := fmt.Sprintf("http://www.linkedin.com/countserv/count/share?format=json&url=%s", url)
body, err := fetchUrl(endpoint)
if err != nil {
log.Printf("Error in getting response from LinkedIn: %s", err.Error())
return
}
type linkedinResult struct {
Count int
}
var l linkedinResult
err = json.Unmarshal(body, &l)
if err != nil {
log.Printf("Error in parsing response from LinkedIn: %s", err.Error())
return
}
result.Count = l.Count
return
}