forked from piquette/edgr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.go
More file actions
174 lines (144 loc) · 3.79 KB
/
Copy pathcore.go
File metadata and controls
174 lines (144 loc) · 3.79 KB
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
package edgr
import (
"encoding/xml"
"fmt"
"regexp"
"strings"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/hodlgap/edgr/request"
"golang.org/x/net/html/charset"
"github.com/hodlgap/edgr/model"
)
var (
secCompanyURL = "https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK=%s&start=0&count=1&output=atom"
dirRegex = regexp.MustCompile(`<td><a href="(.*?)"><img`)
urlRegex = regexp.MustCompile(`.*<a href="(.*?)index.html"><img`)
)
// Filer models
// -----------------
// Company is a simple struct for a single company.
type Company struct {
Name string
Symbol string
}
// rssFeed is the feed obj.
type rssFeed struct {
Info secFilerInfo `xml:"company-info"`
}
type secFilerInfo struct {
CIK string `xml:"cik"`
SIC string `xml:"assigned-sic,omitempty"`
SICDesc string `xml:"assigned-sic-desc,omitempty"`
Name string `xml:"conformed-name"`
}
// GetFiler gets a single filer from the SEC website based on symbol.
func GetFiler(symbol string) (*model.Filer, error) {
// get the cik for each symbol.
// tedious process...
url := fmt.Sprintf(secCompanyURL, symbol)
respBody, err := request.GetPage(url)
if err != nil {
return nil, err
}
var feed rssFeed
decoder := xml.NewDecoder(strings.NewReader(respBody))
decoder.CharsetReader = charset.NewReaderLabel
if err := decoder.Decode(&feed); err != nil {
return nil, errors.WithStack(err)
}
if feed.Info.CIK == "" {
return nil, errors.New("no cik found in response data")
}
if feed.Info.Name == "" {
return nil, errors.New("no name found in response data")
}
return &model.Filer{
CIK: feed.Info.CIK,
Symbol: symbol,
SIC: feed.Info.SIC,
SICDescription: feed.Info.SICDesc,
Name: feed.Info.Name,
}, nil
}
// Filings models
// -----------------
// SECFiling contains a single instance of an sec filing.
type SECFiling struct {
Filing *model.Filing
Docs []*model.Document
}
// Filings methods
// -----------------
// GetFilings gets a list of filings for a single CIK.
func GetFilings(cik, formtype, stoptime string) (filings []SECFiling, err error) {
var stop *time.Time
if stoptime != "" {
t, err := time.Parse("2006-01-02", stoptime)
if err != nil {
return filings, err
}
stop = &t
}
dirPage, err := request.GetPage("https://www.sec.gov/Archives/edgar/data/" + cik)
if err != nil {
return
}
urls := findListURLs(dirPage)
for _, u := range urls {
docsPage, getErr := request.GetPage(u)
if getErr != nil {
log.Errorf("couldnt find page: %s", getErr)
continue
}
idxURL := findIdxURL(docsPage)
if idxURL == "" {
log.Error("couldnt regex idx url")
continue
}
filing, buildErr := buildFiling(cik, idxURL)
if buildErr != nil {
log.Error(buildErr)
continue
}
if formtype != "" {
// check form type.
if filing.Filing.FormType != formtype {
continue
}
}
if stop != nil {
// check cutoff time.
if filing.Filing.EdgarTime.Before(*stop) {
return
}
}
// Do stuff with the filing...
filing.Filing.AllSymbols = []string{filing.Filing.Symbol}
filings = append(filings, filing)
}
return
}
// findIdxURL parses the text document url out of the index page.
func findIdxURL(html string) string {
matches := urlRegex.FindStringSubmatch(html)
if matches == nil || len(matches) == 1 {
log.Warn("could not find matches")
return ""
}
return "https://sec.gov" + matches[1] + "index.html"
}
// findListURLs parses the list of idx urls out of the directory page.
func findListURLs(html string) []string {
matches := dirRegex.FindAllStringSubmatch(html, -1)
if matches == nil || len(matches) == 1 {
log.Warn("could not find matches")
return nil
}
urls := make([]string, len(matches))
for i, m := range matches {
urls[i] = "https://sec.gov" + m[1]
}
return urls
}