/
track.go
239 lines (209 loc) · 4.79 KB
/
track.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path"
"strings"
"github.com/PuerkitoBio/goquery"
"gopkg.in/yaml.v2"
)
type TCases []TCase
type TCase struct {
CaseNumber string `json:"casenumber" yaml:"casenumber"`
Name string `json:"name" yaml:"name"`
Charges []string `json:"charges" yaml:"charges"`
Links []string `json:"links" yaml:"links"`
Residency string `json:"residency" yaml:"residency"`
CaseStatus []string `json:"casestatus" yaml:"casestatus"`
LastUpdated string `json:"lastupdated" yaml:"lastupdated"`
}
const baseURL = "https://www.justice.gov"
const fnCases = "cases"
func main() {
tcases := initialize(fnCases)
track(tcases, "/opa/investigations-regarding-violence-capitol")
fmt.Println("Review/edit cases.json then press enter to copy to cases.yaml")
fmt.Scanln()
tcases = initialize(fnCases)
copy(tcases)
}
func initialize(fn string) TCases {
tcases := TCases{}
fp := fmt.Sprintf("%s.json", fn)
file, err := os.Open(fp)
if err != nil {
return tcases
}
defer file.Close()
b, err := ioutil.ReadAll(file)
if err != nil {
return tcases
}
err = json.Unmarshal(b, &tcases)
if err != nil {
return tcases
}
return tcases
}
func document(p string) *goquery.Document {
u, err := url.Parse(baseURL)
u.Path = path.Join(u.Path, p)
res, err := http.Get(u.String())
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
return doc
}
func output(tcases TCases) {
// output JSON
dJSON, err := json.MarshalIndent(&tcases, "", " ")
if err != nil {
log.Fatalf("error: %v", err)
}
err = ioutil.WriteFile("cases.json", dJSON, 0644)
if err != nil {
log.Fatalf("error: %v", err)
}
log.Printf("saved cases.json\n")
}
func copy(tcases TCases) {
// output YAML
dYAML, err := yaml.Marshal(&tcases)
if err != nil {
log.Fatalf("error: %v", err)
}
err = ioutil.WriteFile("cases.yml", dYAML, 0644)
if err != nil {
log.Fatalf("error: %v", err)
}
log.Printf("saved cases.yaml\n")
}
func track(tcases TCases, p string) {
doc := document(p)
doc.Find("tr").Each(func(i int, c *goquery.Selection) {
if i == 0 {
return // skip header row
}
tcase := extract(c)
if ok := update(tcases, tcase); ok {
log.Printf("updated : %s : %s\n", tcase.Name, tcase.CaseNumber)
} else {
tcases = append(tcases, tcase)
log.Printf("added : %s : %s\n", tcase.Name, tcase.CaseNumber)
}
})
output(tcases)
}
func update(tcases TCases, tcase TCase) bool {
for i, tc := range tcases {
if match(tc, tcase) {
if len(tcase.Links) != 0 {
tcases[i].Links = tcase.Links
}
if len(tcase.Charges) != 0 {
tcases[i].Charges = tcase.Charges
}
if len(tcase.CaseStatus) != 0 {
tcases[i].CaseStatus = tcase.CaseStatus
}
if tcase.LastUpdated != "" {
tcases[i].LastUpdated = tcase.LastUpdated
}
return true
}
}
return false
}
func match(tc1, tc2 TCase) bool {
if tc1.CaseNumber == "" || tc2.CaseNumber == "" {
if tc1.Name == "" || tc2.Name == "" {
return false
}
if tc1.Name == tc2.Name {
return true
}
}
if tc1.CaseNumber == tc2.CaseNumber {
if tc1.Name != tc2.Name {
return false
}
return true
}
return false
}
func extract(c *goquery.Selection) TCase {
tcase := TCase{}
c.Find("td").Each(func(i int, x *goquery.Selection) {
switch i {
case 0:
tcase.CaseNumber = clean(x.Text())
case 1:
tcase.Name = clean(x.Text())
case 2:
tcase.Charges = texts(x)
case 3:
tcase.Links = links(x)
case 4:
tcase.Residency = clean(x.Text())
case 5:
tcase.CaseStatus = texts(x)
case 6:
tcase.LastUpdated = clean(x.Text())
}
})
return tcase
}
func texts(s *goquery.Selection) []string {
texts := []string{}
s.Find("p").Each(func(i int, x *goquery.Selection) {
texts = append(texts, clean(x.Text()))
})
if len(texts) == 0 {
texts = append(texts, clean(s.Text()))
}
return texts
}
func links(l *goquery.Selection) []string {
links := []string{}
l.Find("a").Each(func(i int, x *goquery.Selection) {
if link, ok := (x.Attr("href")); ok {
links = append(links, baseURL+clean(link))
}
})
return links
}
func clean(t string) string {
t = strings.TrimSpace(t)
t = replaceReplacer(t)
return t
}
/////////////////////////////////////////////////////////////////////////////////////////////////
// https://stackoverflow.com/questions/52594005/golang-replace-any-and-all-newline-characters?rq=1
func replaceReplacer(s string) string {
var replacer = strings.NewReplacer(
"\t", " ",
"\r\n", " ",
"\r", " ",
"\n", " ",
"\v", " ",
"\f", " ",
"\u00A0", " ",
"\u0085", " ",
"\u2028", " ",
"\u2029", " ",
)
return replacer.Replace(s)
}