diff --git a/companion/adapter/inbound/streamlabs/streamlabs.go b/companion/adapter/inbound/streamlabs/streamlabs.go index 3776e51..296bf38 100644 --- a/companion/adapter/inbound/streamlabs/streamlabs.go +++ b/companion/adapter/inbound/streamlabs/streamlabs.go @@ -7,19 +7,22 @@ import ( "github.com/ambelovsky/gosf-socketio/transport" "log" "strconv" + "strings" "time" ) -func New(token, currency string) companion.StreamEventSource { +func New(token, currency string, converter companion.CurrencyConverter) companion.StreamEventSource { return &streamlabs{ - token: token, - currency: currency, + token: token, + currency: currency, + converter: converter, } } type streamlabs struct { - token string - currency string + token string + currency string + converter companion.CurrencyConverter } type Channel struct { @@ -59,9 +62,14 @@ func (s *streamlabs) Connect(events chan<- companion.StreamEvent, messages chan< } err = client.On("event", func(c *gosocketio.Channel, data Ev) { - if data.Type == "donation" && data.Message[0].Currency == s.currency { + if data.Type == "donation" { amount := parseAmount(data.Message[0].Amount) * 100 - log.Println("Amount:", amount) + sourceCurrency := strings.ToLower(data.Message[0].Currency) + if sourceCurrency != s.currency { + converted := s.converter.Convert(int(amount), sourceCurrency, s.currency) + log.Printf("Converted %v %v to %v %v", amount, sourceCurrency, converted, s.currency) + amount = converted + } events <- companion.StreamEvent{ EventType: companion.EventTypeDono, Amount: amount, diff --git a/companion/adapter/inbound/yamlconfig/loader.go b/companion/adapter/inbound/yamlconfig/loader.go index 445f794..8f55911 100644 --- a/companion/adapter/inbound/yamlconfig/loader.go +++ b/companion/adapter/inbound/yamlconfig/loader.go @@ -5,6 +5,7 @@ import ( "errors" "gopkg.in/yaml.v3" "os" + "strings" "time" ) @@ -48,6 +49,7 @@ func (l *loader) Load() (*companion.Config, error) { if c.Currency == "" { c.Currency = "EUR" } + c.Currency = strings.ToLower(c.Currency) return c, nil } diff --git a/companion/adapter/outbound/exchangerate/convert.go b/companion/adapter/outbound/exchangerate/convert.go new file mode 100644 index 0000000..506f65a --- /dev/null +++ b/companion/adapter/outbound/exchangerate/convert.go @@ -0,0 +1,55 @@ +package exchangerate + +import ( + "companion" + "encoding/json" + "fmt" + "log" + "net/http" + "strings" +) + +func New(targetCurrency string) (companion.CurrencyConverter, error) { + rates, err := getRates(targetCurrency) + if err != nil { + return nil, err + } + return &converter{ + rates: *rates, + }, nil +} + +type converter struct { + rates ConversionRates +} + +func (c *converter) Convert(fromAmount int, fromCurrency string, toCurrency string) int { + + fromCurrency = strings.ToLower(fromCurrency) + + return int(c.rates[fromCurrency].InverseRate * float64(fromAmount)) + +} + +func getRates(toCurrency string) (*ConversionRates, error) { + + log.Println("download exchange rates for", toCurrency) + resp, err := http.Get(fmt.Sprintf("http://www.floatrates.com/daily/%v.json", strings.ToLower(toCurrency))) + if err != nil { + return nil, err + } + rates := &ConversionRates{} + json.NewDecoder(resp.Body).Decode(rates) + + return rates, nil +} + +type ConversionRates map[string]struct { + Code string `json:"code"` + AlphaCode string `json:"alphaCode"` + NumericCode string `json:"numericCode"` + Name string `json:"name"` + Rate float64 `json:"rate"` + Date string `json:"date"` + InverseRate float64 `json:"inverseRate"` +} diff --git a/companion/cmd/main.go b/companion/cmd/main.go index c95e3f8..bfbbe27 100644 --- a/companion/cmd/main.go +++ b/companion/cmd/main.go @@ -5,6 +5,7 @@ import ( "companion/adapter/inbound/streamlabs" "companion/adapter/inbound/twitchchat" "companion/adapter/inbound/yamlconfig" + "companion/adapter/outbound/exchangerate" "companion/adapter/outbound/squirter" "companion/adapter/outbound/trayicon" "log" @@ -43,11 +44,18 @@ func main() { } } + converter, err := exchangerate.New(conf.Currency) + if err != nil { + ui.ErrorMessage("Currency %v can not be converted to.", conf.Currency) + ui.Quit() + os.Exit(1) + } + events := make(chan companion.StreamEvent) messages := make(chan companion.ChatMessage) if conf.Streamlabs != "" { - sl := streamlabs.New(conf.Streamlabs, conf.Currency) + sl := streamlabs.New(conf.Streamlabs, conf.Currency, converter) err := sl.Connect(events, messages) if err != nil { diff --git a/companion/converter.go b/companion/converter.go new file mode 100644 index 0000000..a6f1d01 --- /dev/null +++ b/companion/converter.go @@ -0,0 +1,5 @@ +package companion + +type CurrencyConverter interface { + Convert(fromAmount int, fromCurrency string, toCurrency string) int +}