-
Notifications
You must be signed in to change notification settings - Fork 0
/
idx_downloader.go
172 lines (157 loc) · 4.09 KB
/
idx_downloader.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
package edgarwebcrawler
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
type ItemType uint8
const (
File ItemType = iota
Dir
)
type EdgarItem struct {
//file or dir
ItemType string `json:"type"`
Href, Size, Name string
LastModified string `json:"last-modified"`
}
type EdgarDirectory struct {
Name string `json:"name"`
ParentDir string `json:"parent-dir"`
Item []EdgarItem `json:"item"`
}
type IdxDownloader interface {
Download(baseEdgarDirectoryUrl string, recursive bool) error
}
//this implements IdxDownloader
type FormFourIdxDownloader struct {
wg sync.WaitGroup
}
func NewIdxDownloader() *FormFourIdxDownloader {
return &FormFourIdxDownloader{
wg: sync.WaitGroup{},
}
}
func (dl *FormFourIdxDownloader) Download(baseDir, baseEdgarDirectoryUrl string, recursive bool,
startFromYear uint16) error {
index, err := getIndex(baseEdgarDirectoryUrl)
if err != nil {
return err
}
filterYearDirectories(index, startFromYear)
errc := make(chan error)
wgDone := make(chan bool)
if recursive {
dl.wg.Add(1)
go dl.crawlDirectory(*index, baseDir, baseEdgarDirectoryUrl, errc)
}
go func() {
dl.wg.Wait()
wgDone <- true
close(wgDone)
}()
select {
case <-wgDone:
return nil
case e := <-errc:
close(errc)
return e
}
}
func getIndex(url string) (*EdgarDirectory, error) {
req_url := fmt.Sprintf("%s/index.json", url)
fmt.Printf("Requesting %s \n", req_url)
request, err := http.NewRequest("GET", req_url, nil)
request.Header.Set("User-Agent", "Golang_Spider_Bot/3.0")
resp, err := http.DefaultClient.Do(request)
if err != nil {
fmt.Printf("got error when requesting resource %s \n", err)
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
type tempParsedType struct {
Directory EdgarDirectory `json:"directory"`
}
var tempParsed tempParsedType
err = json.Unmarshal(data, &tempParsed)
if err != nil {
return nil, err
}
return &tempParsed.Directory, nil
}
func (dl *FormFourIdxDownloader) crawlDirectory(directory EdgarDirectory,
destdir, baseEdgarDirectoryUrl string, errc chan error) {
defer dl.wg.Done()
// sleep is needed to not exceed edgar request rate limit
time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
for _, item := range directory.Item {
fmt.Printf("item found: %s", item.Name)
if item.ItemType == "dir" {
//this is the full path from /full-index to the directory
newContextDir := filepath.Join(destdir, directory.Name, item.Name)
fmt.Printf("creating new directory: %s \n", newContextDir)
err := os.MkdirAll(newContextDir, os.ModePerm)
if err != nil {
errc <- err
return
}
//directory name starts with full-index so we remove it
newUrl := baseEdgarDirectoryUrl + directory.Name[10:] + item.Name
fmt.Printf("new Edgar url: %s \n", newUrl)
newIndex, err := getIndex(newUrl)
if err != nil {
errc <- err
return
}
dl.wg.Add(1)
go dl.crawlDirectory(*newIndex, destdir, baseEdgarDirectoryUrl, errc)
} else if item.ItemType == "file" && item.Name == "form.idx" {
fmt.Printf("Found form.idx \n")
fileUrl := baseEdgarDirectoryUrl + directory.Name[10:] + item.Href
err := downloadFile(filepath.Join(destdir, directory.Name, item.Name), fileUrl)
if err != nil {
errc <- err
}
}
}
}
func downloadFile(filepath string, url string) error {
fmt.Printf("getting file from url: %s \n", url)
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
if strings.Contains(resp.Header.Get("Content-Type"), "html") {
fmt.Println("got html response type from file url, retrying")
resp.Body.Close()
sleepDuration := time.Duration(rand.Intn(5)) * time.Second
fmt.Printf("sleeping for %d nanoseconds... \n", sleepDuration)
time.Sleep(sleepDuration)
downloadFile(filepath, url)
} else {
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
return nil
}