github.com/diadata-org/diadata@v1.4.593/internal/pkg/ratescrapers/updateSAFR.go (about) 1 package ratescrapers 2 3 import ( 4 "bytes" 5 "encoding/xml" 6 "fmt" 7 "os" 8 "strconv" 9 "time" 10 11 models "github.com/diadata-org/diadata/pkg/model" 12 utils "github.com/diadata-org/diadata/pkg/utils" 13 log "github.com/sirupsen/logrus" 14 ) 15 16 type ( 17 18 // Define the fields associated with the rss document. 19 RssSAFR struct { 20 ItemInd ItemInd `xml:"item"` 21 } 22 ItemInd struct { 23 DescriptionInd string `xml:"description"` 24 DateInd string `xml:"date"` 25 StatisticsInd StatisticsInd `xml:"statistics"` 26 } 27 StatisticsInd struct { 28 RateInd RateInd `xml:"interestRate"` 29 } 30 RateInd struct { 31 ValueInd string `xml:"value"` 32 RateTypeInd string `xml:"rateType"` 33 } 34 ) 35 36 // UpdateSAFR makes a GET request from an rss feed and sends updated value through 37 // Channel s.chanInterestRate 38 func (s *RateScraper) UpdateSAFR() error { 39 log.Printf("SAFRScraper update") 40 41 // Get rss from fed webpage 42 XMLdata, _, err := utils.GetRequest("https://apps.newyorkfed.org/rss/feeds/sofr-avg-ind") 43 if err != nil { 44 return err 45 } 46 47 // Decode the body 48 rss := new(RssSAFR) 49 buffer := bytes.NewBuffer(XMLdata) 50 decoded := xml.NewDecoder(buffer) 51 err = decoded.Decode(rss) 52 53 if err != nil { 54 fmt.Println(err) 55 os.Exit(1) 56 } 57 58 // Collext entries of InterestRate struct ----------------------------------- 59 symbol := rss.ItemInd.StatisticsInd.RateInd.RateTypeInd 60 61 // Convert interest rate from string to float64 62 rate, err := strconv.ParseFloat(rss.ItemInd.StatisticsInd.RateInd.ValueInd, 64) 63 if err != nil { 64 fmt.Println(err) 65 } 66 67 // Convert time string to Time type in UTC and pass date (without daytime) 68 dateTime, err := time.Parse(time.RFC3339, rss.ItemInd.DateInd) 69 if err != nil { 70 fmt.Println(err) 71 } else { 72 dateTime = dateTime.UTC() 73 } 74 75 effDate, err := time.Parse("2006-01-02", rss.ItemInd.DateInd[:10]) 76 if err != nil { 77 log.Error("Error parsing effective date for SAFR: ", err) 78 } 79 80 t := &models.InterestRate{ 81 Symbol: symbol, 82 Value: rate, 83 PublicationTime: dateTime, 84 EffectiveDate: effDate, 85 Source: "FED", 86 } 87 88 // Send new data through channel chanInterestRate 89 log.Printf("Write interestRate %#v in %v\n", t, s.chanInterestRate) 90 s.chanInterestRate <- t 91 92 log.Info("Update complete") 93 94 return err 95 }