-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgitmine.go
295 lines (236 loc) · 5.67 KB
/
gitmine.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
"bufio"
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"github.com/cdale77/gitmine/Godeps/_workspace/src/github.com/melvinmt/firebase"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
)
type Event struct {
Type string
Created_at string
Actor EventActor
Payload EventPayload
}
type EventActor struct {
Login string
Avatar_url string
}
type EventPayload struct {
Size int
Commits []CommitCommit
}
type StoredCommit struct {
SearchId string
Date string
Login string
Avatar string
Message string
Url string
}
type CommitCommit struct {
Message string
Url string
}
type Search struct {
Id string
Words []string
}
func main() {
fullDate := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
getData(fullDate)
}
func storeCommit(event Event, commitMessage string, commitUrl string) bool {
fmt.Println("storing event")
authToken := os.Getenv("FIREBASE_SECRET")
url := os.Getenv("FIREBASE_URL")
fireBase := firebase.NewReference(url).Auth(authToken)
var storedCommit StoredCommit
storedCommit.Date = event.Created_at
storedCommit.Login = event.Actor.Login
storedCommit.Avatar = event.Actor.Avatar_url
storedCommit.Message = commitMessage
storedCommit.Url = commitUrl
storedCommit.SearchId = "1"
err := fireBase.Push(storedCommit)
if err != nil {
fmt.Println("Firebase error")
fmt.Println(err)
fmt.Println("Attempting to store:")
fmt.Println(storedCommit)
fmt.Println("Firebase url:")
fmt.Println(url)
return false
} else {
fmt.Println("Firebase success")
return true
}
}
// There must be a better way to do this. Probably sort cussWords alpha
// and use a lookup table.
func isDirty(message string) bool {
result := false
cussWords := []string{
"fuck",
"bitch",
"stupid",
"tits",
"asshole",
"cocksucker",
"cunt",
"hell",
"douche",
"testicle",
"twat",
"bastard",
"sperm",
"shit",
"dildo",
"wanker",
"prick",
"penis",
"vagina",
"whore"}
var storedSearch Search
storedSearch.Words = cussWords
messageWords := strings.Split(message, " ")
for _, searchWord := range storedSearch.Words {
for _, word := range messageWords {
if word == searchWord {
result = true
}
}
}
return result
}
func parseEvent(line string) {
var event Event
jsonErr := json.Unmarshal([]byte(line), &event)
if jsonErr != nil {
fmt.Println("Could not parse json.")
fmt.Println(jsonErr)
}
if event.Type == "PushEvent" && event.Payload.Size > 0 {
// An event can have multiple commits.
commits := event.Payload.Commits
for _, commit := range commits {
if isDirty(commit.Message) {
//fmt.Println(commit.Message)
htmlUrl := makeHtmlUrl(commit.Url)
storeCommit(event, commit.Message, htmlUrl)
}
}
}
}
func parseFile(fName string) {
// https://groups.google.com/forum/#!topic/golang-nuts/GjIkryuCyAY
// TODO: standardize use of file api
// https://stackoverflow.com/questions/1821811/how-to-read-write-from-to-file
fileOS, err := os.Open(fName)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't open %s: error: %s\n", fName, err)
os.Exit(1)
}
//https://stackoverflow.com/questions/1821811/how-to-read-write-from-to-file
// close fi on exit and check for its returned error
defer func() {
if err := fileOS.Close(); err != nil {
panic(err)
}
}()
fileGzip, err := gzip.NewReader(fileOS)
if err != nil {
fmt.Printf("The file %v is not in gzip format.\n", fName)
os.Exit(1)
}
fileRead := bufio.NewReader(fileGzip)
i := 0
for {
line, err := fileRead.ReadString('\n')
if err != nil {
fmt.Println("Error reading file.")
fmt.Println(err)
break
}
parseEvent(line)
i++
}
os.Remove(fName)
}
func getData(fullDate string) {
urls := makeUrlArray(fullDate)
for i, value := range urls {
fmt.Println("fetching url", value)
resp, archiveErr := http.Get(value)
if resp != nil {
defer resp.Body.Close()
}
if archiveErr != nil {
handleError("Error getting github archive", archiveErr)
}
contents, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
handleError("Error converting response", readErr)
}
fname := makeFileName(fullDate, i)
fileErr := ioutil.WriteFile(fname, contents, 0644)
if fileErr != nil {
handleError("Error writing response to file", fileErr)
}
go parseFile(fname)
}
}
func makeUrlArray(fullDate string) [24]string {
baseUrl := makeUrlBase(fullDate)
urlEnd := ".json.gz"
var urls [24]string
for i := 0; i < 24; i++ {
var buffer bytes.Buffer
buffer.WriteString(baseUrl)
buffer.WriteString("-")
buffer.WriteString(strconv.Itoa(i))
buffer.WriteString(urlEnd)
url := buffer.String()
urls[i] = url
}
return urls
}
func makeUrlBase(fullDate string) string {
split := strings.Split(fullDate, "-")
var buffer bytes.Buffer
buffer.WriteString("http://data.githubarchive.org/")
buffer.WriteString(split[0]) //year
buffer.WriteString("-")
buffer.WriteString(split[1]) //month
buffer.WriteString("-")
buffer.WriteString(split[2]) //day
return buffer.String()
}
func makeFileName(fullDate string, i int) string {
var buffer bytes.Buffer
buffer.WriteString("data-")
buffer.WriteString(fullDate)
buffer.WriteString("-")
buffer.WriteString(strconv.Itoa(i))
buffer.WriteString(".gz")
return buffer.String()
}
// The data does not contain an url to make a proper html page. But we can
// deduce it from the supplied api url (which makes json)
func makeHtmlUrl(apiUrl string) string {
newUrl1 := strings.Replace(apiUrl, "api.", "", 1)
newUrl2 := strings.Replace(newUrl1, "repos/", "", 1)
newUrl3 := strings.Replace(newUrl2, "commits", "commit", 1)
return newUrl3
}
func handleError(message string, err error) {
fmt.Println(message, err)
os.Exit(1)
}