-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhouseParse.go
82 lines (69 loc) · 2.12 KB
/
houseParse.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
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
type HouseLobbyist struct {
FirstName string `xml:"lobbyistFirstName"`
LastName string `xml:"lobbyistLastName"`
}
type HouseFiling struct {
OrganizationName string `xml:"organizationName"`
ClientName string `xml:"clientName"`
SenateID string `xml:"senateID"`
HouseID string `xml:"houseID"`
ReportYear string `xml:"reportYear"`
ReportType string `xml:"reportType"`
Lobbyist []HouseLobbyist `xml:"alis>ali_info>lobbyists>lobbyist"` //different formats for quarterly vs aggregate reports?
//Lobbyist []Lobbyist `xml:"lobbyists>lobbyist"`
}
func parseHouseFilings(recordDir string, combinedFilings *[]GenericFiling, mutex *sync.Mutex, wg *sync.WaitGroup) {
beginParseTime := time.Now()
files, err := ioutil.ReadDir("./" + recordDir + "/")
if err != nil {
panic(err)
}
fmt.Println("Reading " + strconv.Itoa(len(files)) + " files from " + recordDir + "...")
a := 0 //counter for number of files successfully read
for _, f := range files {
data, err := ioutil.ReadFile(recordDir + "/" + f.Name())
if err != nil {
fmt.Println("error reading", err)
continue
} else {
if strings.Contains(filepath.Ext(f.Name()), "xml") {
oneFiling := HouseFiling{}
//unmarshal data and put into struct array
err = xml.Unmarshal(data, &oneFiling)
if err != nil {
fmt.Println("error decoding %v: %v", f.Name(), err)
continue
} else {
mutex.Lock()
combineSingleFiling(oneFiling, combinedFilings)
mutex.Unlock()
a++ //increment number of files successfully parsed
}
}
}
if a%10000 == 0 {
fmt.Println(strconv.Itoa(a), "House files read")
}
}
fmt.Println("Successfully read ", a, " / ", len(files), "House files in", time.Since(beginParseTime).String())
fmt.Println("Removing record directory " + recordDir + "...")
err = os.RemoveAll(recordDir)
if err != nil {
panic(err)
}
fmt.Println("Removed record directory " + recordDir)
//Waitgroup done
wg.Done()
}