Skip to content

Commit

Permalink
add currency conversin
Browse files Browse the repository at this point in the history
  • Loading branch information
tlanfer committed Mar 28, 2022
1 parent 68c7190 commit 84ec19c
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 8 deletions.
22 changes: 15 additions & 7 deletions companion/adapter/inbound/streamlabs/streamlabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions companion/adapter/inbound/yamlconfig/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"gopkg.in/yaml.v3"
"os"
"strings"
"time"
)

Expand Down Expand Up @@ -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
}
Expand Down
55 changes: 55 additions & 0 deletions companion/adapter/outbound/exchangerate/convert.go
Original file line number Diff line number Diff line change
@@ -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"`
}
10 changes: 9 additions & 1 deletion companion/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions companion/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package companion

type CurrencyConverter interface {
Convert(fromAmount int, fromCurrency string, toCurrency string) int
}

0 comments on commit 84ec19c

Please sign in to comment.