-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
156 lines (134 loc) · 3.69 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
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
package main
import (
"comment_phone_analyse/crawler"
"comment_phone_analyse/picexport"
"comment_phone_analyse/pojo"
"flag"
"fmt"
"sort"
"time"
)
var limit int = 1000
var brandDict = map[string]bool{
"华为": true,
"小米": true,
"OPPO": true,
"Vivo": true,
"苹果": true,
"三星": true,
"魅族": true,
"真我": true,
"红米": true,
"一加": true,
"荣耀": true,
"中兴": true,
"努比亚": true,
"IQOO": true,
"未知Android": true,
}
func main() {
uid := flag.String("uid", "Default", "User ID to be counted")
cookie := flag.String("cookie", "Defaulta", "Weibo Cookie")
limit := flag.Int("limit", limit, "Number of counted,default 1000")
flag.Parse()
if *uid == "Default" || *cookie == "Default" {
fmt.Println("请输入待统计用户的uid or cookie ,go run main.go -uid xxxxxxx -cookie \"xxxxxx\"")
return
}
fmt.Println("统计中...")
unknowDevice := make(map[string]int)
userSet := make(map[string]bool)
phoneMap := make(map[string]int)
// total users
var cnt = 0
// blog page
var page = 1
for cnt < *limit {
// get blog
result := crawler.GetBlog(*uid, *cookie, page)
if result == nil || len(result.Data.List) == 0 {
fmt.Println("No more blog")
break
}
// get comment user
for _, blog := range result.Data.List {
// filter others blog , example: like, forward
if blog.User.Uid != *uid {
continue
}
userUrls := crawler.GetCommentUser(*cookie, blog.MblogId, *uid)
// statistics
for _, userUrl := range userUrls.Data {
// filter duplicate user
if _, ok := userSet[userUrl.User.Idstr]; ok {
continue
}
userSet[userUrl.User.Idstr] = true
time.Sleep(1 * time.Second)
// get phone type
phoneType := crawler.GetSource(userUrl.User.Idstr, *cookie)
phoneMap[phoneType]++
if _, exists := brandDict[phoneType]; !exists {
unknowDevice[phoneType]++
continue
}
}
// print phone type
cnt = 0
for phoneType, num := range phoneMap {
if _, exists := brandDict[phoneType]; exists {
cnt = cnt + num
fmt.Printf("PhoneType: %s, Num: %d\n", phoneType, phoneMap[phoneType])
}
}
fmt.Printf("cnt: %d\n", cnt)
fmt.Println("=====================================")
fmt.Println("统计中...")
if cnt >= *limit {
break
}
}
time.Sleep(3 * time.Second)
page++
}
// sort by value
sortByValue := func(m map[string]int) []string {
type kv struct {
Key string
Value int
}
var sortedSlice []kv
for k, v := range m {
sortedSlice = append(sortedSlice, kv{k, v})
}
sort.Slice(sortedSlice, func(i, j int) bool {
return sortedSlice[i].Value > sortedSlice[j].Value
})
sortedKeys := make([]string, len(sortedSlice))
for i, kv := range sortedSlice {
sortedKeys[i] = kv.Key
}
return sortedKeys
}
unknownKeys := sortByValue(unknowDevice)
knownKeys := sortByValue(phoneMap)
fmt.Println("==========================最终统计结果:未知机型============================")
for _, phoneType := range unknownKeys {
if _, exists := brandDict[phoneType]; !exists {
fmt.Printf("PhoneType: %s, Num: %d\n", phoneType, unknowDevice[phoneType])
}
}
fmt.Println("==========================最终统计结果:已知机型============================")
dataArr := []pojo.StatisticsData{}
for _, phoneType := range knownKeys {
if _, exists := brandDict[phoneType]; exists {
fmt.Printf("PhoneType: %s, Num: %d\n", phoneType, phoneMap[phoneType])
dataArr = append(dataArr, pojo.StatisticsData{
PhoneType: phoneType,
Count: phoneMap[phoneType],
})
}
}
picexport.Export(*uid, dataArr)
picexport.ExportPieChart(*uid, dataArr)
}