-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhouseRetrieve.go
162 lines (134 loc) · 4.08 KB
/
houseRetrieve.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
package main
import (
"code.google.com/p/go.net/html"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"
)
type Tokens struct {
ViewState, EventValidation string
FileNames []string //max of four quarters and one new registration option a year
}
var savePathHouse = "./houseFilings/"
var tmpNameHouse = "tmp"
var fileExtHouse = ".zip"
var linkHouse = "http://disclosures.house.gov/ld/LDDownload.aspx?KeepThis=true"
func download(t Tokens, i int, file string, wg *sync.WaitGroup) {
//set POST data
v := url.Values{}
v.Set("__VIEWSTATE", t.ViewState)
v.Set("__EVENTVALIDATION", t.EventValidation)
v.Set("selFilesXML", t.FileNames[i])
v.Set("btnDownloadXML", "Download")
fmt.Println("Downloading " + file + " to " + savePathHouse + tmpNameHouse + strconv.Itoa(i) + fileExtHouse + "...")
//pres, err := http.PostForm("http://vm-2.ansonl.koding.kd.io/php.php", v)
pres, err := http.PostForm(linkHouse, v)
robots, err := ioutil.ReadAll(pres.Body)
pres.Body.Close()
if err != nil {
panic(err)
}
err = ioutil.WriteFile(savePathHouse+tmpNameHouse+strconv.Itoa(i)+fileExtHouse, robots, 0644)
if err != nil {
panic(err)
}
fmt.Println("Downloaded " + file + " to " + savePathHouse + tmpNameHouse + strconv.Itoa(i) + fileExtHouse)
//Unzipping
fmt.Println("Unzipping " + savePathHouse + tmpNameHouse + strconv.Itoa(i) + fileExtHouse + " to " + savePathHouse + "...")
err = Unzip(savePathHouse+tmpNameHouse+strconv.Itoa(i)+fileExtHouse, savePathHouse)
if err != nil {
panic(err)
}
fmt.Println("Unzipped " + savePathHouse + tmpNameHouse + strconv.Itoa(i) + fileExtHouse + " to " + savePathHouse)
//Waitgroup done
wg.Done()
}
func Extend(slice []string, element string) []string {
n := len(slice)
if n == cap(slice) {
// Slice is full; must grow.
// We double its size and add 1, so if the size is zero we still grow.
newSlice := make([]string, len(slice), 2*len(slice)+1)
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0 : n+1]
slice[n] = element
return slice
}
func scrape() string {
fmt.Println("Sending GET request to " + linkHouse + "...")
res, err := http.Get(linkHouse)
if err != nil {
panic(err)
}
doc, err := html.Parse(res.Body)
token := Tokens{}
token.FileNames = make([]string, 0)
//documentation https://godoc.org/code.google.com/p/go.net/html#Attribute
var f func(*html.Node)
f = func(n *html.Node) {
//'n' is a node representing ONE object on the page
//fmt.Println(n.Attr)
if n.Type == html.ElementNode {
for _, a := range n.Attr {
//fmt.Println(a)
//fmt.Println(a.Namespace, a.Key, a.Val)
if len(a.Val) > 3 && strings.Contains(a.Val[0:2], strconv.Itoa(time.Now().Year())[0:2]) { //check first two characters of attr value to get year
token.FileNames = Extend(token.FileNames, a.Val)
//fmt.Println(a.Val)
}
if a.Val == "__VIEWSTATE" {
//fmt.Println(n.Attr[2].Key, n.Attr[2].Val)
for _, b := range n.Attr { //loop through this same object again, looking for the attribute in the slice which has key of "value"
if b.Key == "value" {
token.ViewState = b.Val
}
}
}
if a.Val == "__EVENTVALIDATION" {
for _, b := range n.Attr { //loop through this same object again, looking for the attribute in the slice which has key of "value"
if b.Key == "value" {
token.EventValidation = b.Val
}
}
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling { //advance node
f(c)
}
}
f(doc)
//Download each file in a thread
if _, err := os.Stat(savePathHouse); err != nil {
if os.IsNotExist(err) {
err = os.Mkdir(savePathHouse, 0777)
if err != nil {
panic(err)
}
} else {
panic(err)
}
}
fmt.Println(savePathHouse + " records directory made")
var wg sync.WaitGroup
for fileNumber, file := range token.FileNames[len(token.FileNames)-6 : len(token.FileNames)] {
if file != "" {
wg.Add(1)
go download(token, fileNumber, file, &wg)
}
}
wg.Wait()
fmt.Println("All House files downloaded.")
return savePathHouse //return saved path
}
func downloadHouseData() string {
return scrape()
}