-
Notifications
You must be signed in to change notification settings - Fork 126
/
updateSAFR.go
95 lines (79 loc) · 2.19 KB
/
updateSAFR.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
package ratescrapers
import (
"bytes"
"encoding/xml"
"fmt"
"os"
"strconv"
"time"
models "github.com/diadata-org/diadata/pkg/model"
utils "github.com/diadata-org/diadata/pkg/utils"
log "github.com/sirupsen/logrus"
)
type (
// Define the fields associated with the rss document.
RssSAFR struct {
ItemInd ItemInd `xml:"item"`
}
ItemInd struct {
DescriptionInd string `xml:"description"`
DateInd string `xml:"date"`
StatisticsInd StatisticsInd `xml:"statistics"`
}
StatisticsInd struct {
RateInd RateInd `xml:"interestRate"`
}
RateInd struct {
ValueInd string `xml:"value"`
RateTypeInd string `xml:"rateType"`
}
)
// UpdateSAFR makes a GET request from an rss feed and sends updated value through
// Channel s.chanInterestRate
func (s *RateScraper) UpdateSAFR() error {
log.Printf("SAFRScraper update")
// Get rss from fed webpage
XMLdata, _, err := utils.GetRequest("https://apps.newyorkfed.org/rss/feeds/sofr-avg-ind")
if err != nil {
return err
}
// Decode the body
rss := new(RssSAFR)
buffer := bytes.NewBuffer(XMLdata)
decoded := xml.NewDecoder(buffer)
err = decoded.Decode(rss)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Collext entries of InterestRate struct -----------------------------------
symbol := rss.ItemInd.StatisticsInd.RateInd.RateTypeInd
// Convert interest rate from string to float64
rate, err := strconv.ParseFloat(rss.ItemInd.StatisticsInd.RateInd.ValueInd, 64)
if err != nil {
fmt.Println(err)
}
// Convert time string to Time type in UTC and pass date (without daytime)
dateTime, err := time.Parse(time.RFC3339, rss.ItemInd.DateInd)
if err != nil {
fmt.Println(err)
} else {
dateTime = dateTime.UTC()
}
effDate, err := time.Parse("2006-01-02", rss.ItemInd.DateInd[:10])
if err != nil {
log.Error("Error parsing effective date for SAFR: ", err)
}
t := &models.InterestRate{
Symbol: symbol,
Value: rate,
PublicationTime: dateTime,
EffectiveDate: effDate,
Source: "FED",
}
// Send new data through channel chanInterestRate
log.Printf("Write interestRate %#v in %v\n", t, s.chanInterestRate)
s.chanInterestRate <- t
log.Info("Update complete")
return err
}