-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (101 loc) · 1.92 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"sort"
"time"
)
type URLShareCnt struct {
URL string
Twitter ShareCnt
FaceBook ShareCnt
Hatebu ShareCnt
Pocket ShareCnt
}
type ShareCnt struct {
Count int
FetchAt time.Time
}
func main() {
fbcache, err := os.Open("resource/cache_facebook.json")
if err != nil {
panic(err)
}
fbAll, err := io.ReadAll(fbcache)
if err != nil {
panic(err)
}
var fbmap map[string]int
if err := json.Unmarshal(fbAll, &fbmap); err != nil {
panic(err)
}
hatebucache, err := os.Open("resource/cache_hatebu.json")
if err != nil {
panic(err)
}
hatebuAll, err := io.ReadAll(hatebucache)
if err != nil {
panic(err)
}
var hatebumap map[string]int
if err := json.Unmarshal(hatebuAll, &hatebumap); err != nil {
panic(err)
}
pocketcache, err := os.Open("resource/cache_pocket.json")
if err != nil {
panic(err)
}
pocketAll, err := io.ReadAll(pocketcache)
if err != nil {
panic(err)
}
var pocketmap map[string]int
if err := json.Unmarshal(pocketAll, &pocketmap); err != nil {
panic(err)
}
twcache, err := os.Open("resource/cache_twitter.json")
if err != nil {
panic(err)
}
twAll, err := io.ReadAll(twcache)
if err != nil {
panic(err)
}
var twmap map[string]int
if err := json.Unmarshal(twAll, &twmap); err != nil {
panic(err)
}
now := time.Now()
var shares []URLShareCnt
for k, _ := range pocketmap {
shares = append(shares, URLShareCnt{
URL: k,
Twitter: ShareCnt{
Count: twmap[k],
FetchAt: now,
},
FaceBook: ShareCnt{
Count: fbmap[k],
FetchAt: now,
},
Hatebu: ShareCnt{
Count: hatebumap[k],
FetchAt: now,
},
Pocket: ShareCnt{
Count: pocketmap[k],
FetchAt: now,
},
})
}
sort.SliceStable(shares, func(i, j int) bool {
return shares[i].URL > shares[j].URL
})
marshal, err := json.MarshalIndent(shares, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(marshal))
}