-
Notifications
You must be signed in to change notification settings - Fork 0
/
idx_reader.go
74 lines (66 loc) · 1.87 KB
/
idx_reader.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
package edgarwebcrawler
import (
"bufio"
"errors"
"io"
"strings"
)
type IdxRow struct {
FileName, FormType, CompanyName, CIK, DateFiled string
}
type IdxReader interface {
ReadRow() (*IdxRow, error)
}
//SimpleIdxReader implements IdxReader
type SimpleIdxReader struct {
formTypeIndex, companyNameIndex, cikIndex, dateIndex, fileNameIndex int
reader bufio.Reader
}
func NewIdxReader(reader io.Reader) (*SimpleIdxReader, error) {
bufReader := bufio.NewReader(reader)
for {
line, err := ReadLine(bufReader)
if err != nil {
break
}
formTypeIndex := strings.Index(line, "Form Type")
companyNameIndex := strings.Index(line, "Company Name")
cikIndex := strings.Index(line, "CIK")
dateIndex := strings.Index(line, "Date Filed")
fileNameIndex := strings.Index(line, "File Name")
if formTypeIndex == -1 ||
companyNameIndex == -1 ||
cikIndex == -1 ||
dateIndex == -1 ||
fileNameIndex == -1 {
continue
}
return &SimpleIdxReader{
formTypeIndex: formTypeIndex,
companyNameIndex: companyNameIndex,
cikIndex: cikIndex,
dateIndex: dateIndex,
fileNameIndex: fileNameIndex,
reader: *bufReader,
}, nil
}
return nil, errors.New("could not find IDX Header")
}
func (p *SimpleIdxReader) ReadRow() (*IdxRow, error) {
idxLine, err := ReadLine(&p.reader)
if err != nil {
return nil, err
}
formType := strings.Trim(idxLine[p.formTypeIndex:p.companyNameIndex], " ")
companyName := strings.Trim(idxLine[p.companyNameIndex:p.cikIndex], " ")
cik := strings.Trim(idxLine[p.cikIndex:p.dateIndex], " ")
date := strings.Trim(idxLine[p.dateIndex:p.fileNameIndex], " ")
fileName := strings.Trim(idxLine[p.fileNameIndex:], " ")
return &IdxRow{
FileName: fileName,
CompanyName: companyName,
CIK: cik,
DateFiled: date,
FormType: formType,
}, nil
}