-
Notifications
You must be signed in to change notification settings - Fork 3
/
download.go
214 lines (192 loc) · 5.14 KB
/
download.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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"eswiac.me/filingsdb/models"
"github.com/antchfx/htmlquery"
"github.com/briandowns/spinner"
"github.com/dustin/go-humanize"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func GetArchivesURLs() []string {
secGov := "https://www.sec.gov"
url := secGov + "/dera/data/financial-statement-and-notes-data-set.html"
doc, err := htmlquery.LoadURL(url)
if err != nil {
log.Fatal(err)
}
list := htmlquery.Find(doc, "//a/@href")
links := []string{}
for _, n := range list {
link := htmlquery.SelectAttr(n, "href") // output @href value
if strings.HasSuffix(link, "_notes.zip") {
links = append(links, secGov+link)
}
}
return links
}
func (wc *WriteCounter) Write(p []byte) (int, error) {
n := len(p)
wc.Total += uint64(n)
wc.PrintProgress()
return n, nil
}
type WriteCounter struct {
Total uint64
}
// PrintProgress prints the progress of a file write
func (wc WriteCounter) PrintProgress() {
// Clear the line by using a character return to go back to the start and remove
// the remaining characters by filling it with spaces
fmt.Printf("\r%s", strings.Repeat(" ", 50))
// Return again and print current status of download
// We use the humanize package to print the bytes in a meaningful way (e.g. 10 MB)
fmt.Printf("\rDownloading... %s complete", humanize.Bytes(wc.Total))
}
func (d *Downloader) DownloadFile(url string, filepath string) {
// Create the file with .tmp extension, so that we won't overwrite a
// file until it's downloaded fully
out, err := os.Create(filepath + ".tmp")
if err != nil {
log.Fatal(err)
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// Create our bytes counter and pass it to be used alongside our writer
counter := &WriteCounter{}
_, err = io.Copy(out, io.TeeReader(resp.Body, counter))
if err != nil {
log.Fatal(err)
}
// The progress use the same line so print a new line once it's finished downloading
fmt.Println()
// Rename the tmp file back to the original file
err = os.Rename(filepath+".tmp", filepath)
if err != nil {
log.Fatal(err)
}
}
type Downloader struct {
db *gorm.DB
yearUrls []string
year string
}
func dbName(year string) string {
return fmt.Sprintf("filings_%v.db", year)
}
func New(year string) *Downloader {
yearInt, err := strconv.Atoi(os.Args[1])
if err != nil {
log.Fatal(err)
}
if yearInt < 2009 || yearInt > 2100 {
log.Fatalf("Filings are not available before 2009 and after 2100")
}
yearUrls := []string{} // 4 qtr in a year
for _, url := range GetArchivesURLs() {
if strings.Contains(url, year) {
yearUrls = append(yearUrls, url)
}
}
if len(yearUrls) == 0 {
log.Fatalf("Couldn't find any filings from sec.gov filed in %v", year)
}
_, err = os.Stat(dbName(year))
if !os.IsNotExist(err) {
log.Fatalf("filings database already exists. This script does not perform differential updates. Please rename or move %v in order to rebuild the filings database.", dbName(year))
}
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Minute, // Slow SQL threshold
LogLevel: logger.Silent, // Log level
Colorful: true, // Disable color
},
)
db, err := gorm.Open(sqlite.Open(dbName(year)+"?_journal_mode=WAL"), &gorm.Config{
Logger: newLogger,
})
if err != nil {
log.Fatal(err)
}
db.AutoMigrate(
&models.DataSUB{},
&models.DataTAG{},
&models.DataDIM{},
&models.DataNUM{},
&models.DataTXT{},
&models.DataPRE{},
&models.DataREN{},
&models.DataCAL{},
&models.DataTicker{},
)
return &Downloader{yearUrls: yearUrls, db: db, year: year}
}
func (d Downloader) Start() {
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
s.Start()
fmt.Println("Processing started, please be patient. This may take a while!")
d.downloadTickers()
for _, url := range d.yearUrls {
d.handle(url)
}
s.Stop()
fmt.Printf("Processing complete. You can now open your %v filings database with `sqlite3 %v`", d.year, dbName((d.year)))
fmt.Println()
}
func (d Downloader) handle(url string) {
tmpFile, err := ioutil.TempFile("", "")
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmpFile.Name())
d.DownloadFile(url, tmpFile.Name())
ExtractFromZip(d.db, tmpFile.Name())
}
func (d Downloader) downloadTickers() {
tickersList := models.DataTickers{}
// Get the data
resp, err := http.Get("https://www.sec.gov/files/company_tickers.json")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(body, &tickersList)
if err != nil {
log.Fatal(err)
}
tickers := []models.DataTicker{}
for _, ticker := range tickersList {
ticker.CikString = strconv.Itoa(ticker.Cik)
tickers = append(tickers, ticker)
if len(tickers) > BATCH_SIZE {
if err := d.db.Create(&tickers).Error; err != nil {
log.Fatal(err)
}
tickers = []models.DataTicker{}
}
}
// last batch
if err := d.db.Create(&tickers).Error; err != nil {
log.Fatal(err)
}
}