/
main.go
155 lines (119 loc) · 3 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
package main
import (
"fmt"
"strings"
"golang.org/x/oauth2"
"github.com/google/go-github/github"
"github.com/tj/docopt"
c "github.com/hstove/gender/classifier"
)
var client *github.Client
const (
Version = `1.0.0`
Usage = `Gender Stats.
Usage:
stats <owner> <repo> <token>
stats -h | --help
stats --version
Options:
-h --help Show this screen.
--token Your Github token.
--version Show version.`
)
func main() {
arguments, err := docopt.Parse(Usage, nil, true, Version, false)
check(err)
owner := arguments["<owner>"].(string)
repo := arguments["<repo>"].(string)
token := arguments["<token>"].(string)
initializeClient(token)
contributors := getContributors(owner, repo)
names := getNames(contributors)
percentFemale, percentMale := predictGenderStats(names)
percentUnknown := (100 - percentFemale - percentMale)
fmt.Println("\nContributors by Gender:")
fmt.Printf("\n - Female: %.2f%%\n", percentFemale)
fmt.Printf("\n - Male: %.2f%%\n", percentMale)
fmt.Printf("\n - Unknown: %.2f%%\n\n", percentUnknown)
}
func initializeClient(token string) {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
client = github.NewClient(tc)
}
func getContributors(owner, repo string) []github.Contributor {
var contributors []github.Contributor
options := &github.ListContributorsOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
for {
c, response, err := client.Repositories.ListContributors(owner, repo, options)
check(err)
contributors = append(contributors, c...)
if response.NextPage == 0 {
break
}
options.ListOptions.Page = response.NextPage
}
return contributors
}
func getNames(contributors []github.Contributor) []string {
var names []string
for _, c := range contributors {
user, _, err := client.Users.Get(*c.Login)
check(err)
var name string
if user.Name == nil {
name = ""
} else {
name = strings.Split(*user.Name, " ")[0]
}
names = append(names, name)
}
return names
}
func check(err error) {
if err != nil {
panic(err.Error())
}
}
func predictGenderStats(names []string) (f, m float64) {
classifier := c.Classifier()
var numFemale int
var femaleNames []string
var numMale int
var maleNames []string
for _, name := range names {
gender, _ := c.Classify(classifier, name)
if gender == string(c.Girl) {
numFemale += 1
femaleNames = append(femaleNames, name)
}
if gender == string(c.Boy) {
numMale += 1
maleNames = append(maleNames, name)
}
}
printNames(maleNames, femaleNames)
numTotal := len(names)
f = percent(numFemale, numTotal)
m = percent(numMale, numTotal)
return
}
func printNames(maleNames, femaleNames []string) {
fmt.Printf(
"\nMALE (%d):\n%s\n",
len(maleNames),
strings.Join(maleNames, "\n"),
)
fmt.Printf(
"\nFEMALE (%d):\n%s\n",
len(femaleNames),
strings.Join(femaleNames, "\n"),
)
}
func percent(numForGender, numTotal int) float64 {
return (float64(numForGender) / float64(numTotal) * 100)
}