-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsenateRetrieve.go
95 lines (75 loc) · 2.12 KB
/
senateRetrieve.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"sync"
"time"
)
var savePathSenate = "./senateFilings/"
var tmpNameSenate = "tmp"
var fileExtSenate = ".zip"
var linkSenate = "http://soprweb.senate.gov/downloads/"
func generateFilenames() []string {
//Create 2014_1 - 2014_4 and 2013_1 - 2013_4
filenameArray := make([]string, 0)
for i := 1; i < 5; i++ {
filenameArray = append(filenameArray, strconv.Itoa(time.Now().Year()-1)+"_"+strconv.Itoa(i)+fileExtSenate)
filenameArray = append(filenameArray, strconv.Itoa(time.Now().Year())+"_"+strconv.Itoa(i)+fileExtSenate)
}
return filenameArray
}
func downloadSenateFiles(filename string, wg *sync.WaitGroup) {
fmt.Println("Downloading " + filename + " to " + savePathSenate + filename + "...")
//Send GET request to download
pres, err := http.Get(linkSenate + filename)
robots, err := ioutil.ReadAll(pres.Body)
pres.Body.Close()
if err != nil {
fmt.Println(err)
}
err = ioutil.WriteFile(savePathSenate+filename, robots, 0644)
if err != nil {
fmt.Println(err)
}
fmt.Println("Downloaded " + filename + " to " + savePathSenate + filename)
//Unzipping
fmt.Println("Unzipping " + savePathSenate + filename + " to " + savePathSenate + "...")
err = Unzip(savePathSenate+filename, savePathSenate)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Unzipped " + savePathSenate + filename + " to " + savePathSenate)
}
//Waitgroup done
wg.Done()
}
func spawnDownloads() string {
filenameArray := generateFilenames()
//Check if save directory exists and create it if nonexistent
if _, err := os.Stat(savePathSenate); err != nil {
if os.IsNotExist(err) {
err = os.Mkdir(savePathSenate, 0777)
if err != nil {
panic(err)
}
} else {
panic(err)
}
}
fmt.Println(savePathSenate + " records directory exists or was made")
//Download each file in a thread
var wg sync.WaitGroup
for _, filename := range filenameArray {
wg.Add(1)
go downloadSenateFiles(filename, &wg)
}
wg.Wait()
fmt.Println("All Senate files downloaded and unzipped.")
return savePathSenate
}
func downloadSenateData() string {
return spawnDownloads()
}