-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolder.go
159 lines (145 loc) · 3.54 KB
/
folder.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
package edgar
import (
"encoding/json"
"errors"
"io"
"log"
"sort"
"sync"
"time"
)
type company struct {
sync.Mutex
Company string `json:"Company"`
cik string
FilingLinks map[FilingType]map[string]string `json:"-"`
Reports map[FilingType]map[string]*filing `json:"Financial Reports"`
}
func (c *company) String() string {
data, err := json.MarshalIndent(c, "", " ")
if err != nil {
log.Fatal("Error marshaling Company data")
}
return string(data)
}
func newCompany(ticker string) *company {
return &company{
Company: ticker,
cik: getCompanyCIK(ticker),
FilingLinks: make(map[FilingType]map[string]string),
Reports: make(map[FilingType]map[string]*filing),
}
}
func (c *company) Ticker() string {
return c.Company
}
func (c *company) Filing(fileType FilingType, ts time.Time) (Filing, error) {
file, ok := c.getReport(fileType, ts)
if !ok {
link, ok1 := c.getFilingLink(fileType, ts)
if !ok1 {
log.Println(c.AvailableFilings(fileType))
return nil, errors.New("No filing available for given date " + getDateString(ts))
}
file = new(filing)
var err error
file.FinData, err = getFinancialData(link, fileType)
if file.FinData != nil {
file.Date = Timestamp(ts)
file.Company = c.Ticker()
c.AddReport(file)
if err != nil {
log.Println(file.Company + "-Filed on: " + getDateString(ts) + ":" + err.Error())
}
return file, nil
}
return nil, err
}
return file, nil
}
// Get multiple filings in parallel
func (c *company) Filings(fileType FilingType, ts ...time.Time) ([]Filing, error) {
var wg sync.WaitGroup
var ret []Filing
var retErrors []error
var m sync.Mutex
for _, t := range ts {
wg.Add(1)
go func(filed time.Time) {
defer wg.Done()
file, err := c.Filing(fileType, filed)
m.Lock()
if err == nil {
ret = append(ret, file)
} else {
err = errors.New(getDateString(filed) + ":" + err.Error())
retErrors = append(retErrors, err)
}
m.Unlock()
}(t)
}
wg.Wait()
if len(ts) != len(ret) && len(retErrors) > 0 {
errString := "Failed to retrieve some filings: \n"
for _, e := range retErrors {
errString = errString + e.Error() + "\n"
}
return ret, errors.New(errString)
}
return ret, nil
}
func (c *company) AddReport(file *filing) {
t, err := file.Type()
if err != nil {
log.Fatal("Adding invalid report")
return
}
c.Lock()
defer c.Unlock()
if c.Reports[t] == nil {
c.Reports[t] = make(map[string]*filing)
}
c.Reports[t][file.Date.String()] = file
}
func (c *company) getReport(fileType FilingType, ts time.Time) (*filing, bool) {
c.Lock()
defer c.Unlock()
file, ok := c.Reports[fileType][getDateString(ts)]
return file, ok
}
func (c *company) AvailableFilings(filingType FilingType) []time.Time {
var d []time.Time
c.Lock()
links := c.FilingLinks[filingType]
for key := range links {
d = append(d, time.Time(getDate(key)))
}
c.Unlock()
sort.Slice(d, func(i, j int) bool {
return d[i].After(d[j])
})
return d
}
func (c *company) CIK() string {
return c.cik
}
func (c *company) getFilingLink(fileType FilingType, ts time.Time) (string, bool) {
c.Lock()
defer c.Unlock()
link, ok := c.FilingLinks[fileType][getDateString(ts)]
return link, ok
}
func (c *company) addFilingLinks(fileType FilingType, files map[string]string) {
c.Lock()
defer c.Unlock()
c.FilingLinks[fileType] = files
}
// Save the Company folder into the writer in JSON format
func (c *company) SaveFolder(w io.Writer) error {
_, err := w.Write([]byte(c.String()))
if err != nil {
log.Println("Failed to save data")
return err
}
return nil
}