forked from enrichman/portfolio-performance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfonte.go
84 lines (69 loc) · 1.73 KB
/
fonte.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
package fonte
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/charmbracelet/log"
"github.com/enrichman/portfolio-performance/pkg/security/quotes"
)
// QuoteLoader struct for FonTe.
type QuoteLoader struct {
name string
isin string
urlName string
}
// New creates a FonTe QuoteLoader.
func New(name, isin string) (*QuoteLoader, error) {
isinURLName := strings.Split(isin, ".")
if len(isinURLName) != 2 {
return nil, fmt.Errorf("wrong ISIN format for FonTe QuoteLoader: \"%s\" - should be \"ISIN.URLName\"", isin)
}
return &QuoteLoader{
name: name,
isin: isinURLName[0],
urlName: isinURLName[1],
}, nil
}
// Name returns the QuoteLoader name.
func (f *QuoteLoader) Name() string {
return f.name
}
// ISIN returns the QuoteLoader isin.
func (f *QuoteLoader) ISIN() string {
return f.isin
}
// URLName returns the QuoteLoader urlName.
func (f *QuoteLoader) URLName() string {
return f.urlName
}
// LoadQuotes fetches quotes from FonTe.
func (f *QuoteLoader) LoadQuotes() ([]quotes.Quote, error) {
years, err := fetchData(f.urlName)
if err != nil {
return nil, err
}
quotesData := []quotes.Quote{}
for _, y := range years {
for i, month := range y.months {
dateString := fmt.Sprintf("%s %s", y.year, month)
tt, err := time.Parse("2006 January", dateString)
if err != nil {
log.Warnf("Error parsing date: %s", err)
continue
}
tt = tt.AddDate(0, 1, -1)
y.values[i] = strings.ReplaceAll(y.values[i], ",", ".")
closeQuote, err := strconv.ParseFloat(y.values[i], 32)
if err != nil {
log.Warnf("Error parsing quote: %s", err)
continue
}
quotesData = append(quotesData, quotes.Quote{
Date: tt,
Close: float32(closeQuote),
})
}
}
return quotesData, nil
}