Skip to content

Commit

Permalink
Drop !crypto due to premium API requirement
Browse files Browse the repository at this point in the history
Signed-off-by: Jared Ledvina <[email protected]>
  • Loading branch information
jaredledvina committed Dec 6, 2024
1 parent 901cdbf commit e0ac159
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 114 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# stock-plugin
Simple plugin for [seabird](https://github.com/seabird-chat/seabird-core) adding the `!stock` and `!crypto` command backed by https://finnhub.io/
Simple plugin for [seabird](https://github.com/seabird-chat/seabird-core) adding the `!stock` command backed by https://finnhub.io/
113 changes: 0 additions & 113 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"fmt"
"log"
"math"
"sort"
"strings"
"time"

finnhub "github.com/Finnhub-Stock-API/finnhub-go/v2"
"github.com/seabird-chat/seabird-go"
Expand Down Expand Up @@ -125,96 +123,6 @@ func (c *SeabirdClient) stockCallback(event *pb.CommandEvent) {

}

func (c *SeabirdClient) exchangeCallback(event *pb.CommandEvent) {
cryptoExchange, _, err := c.finnhubClient.CryptoExchanges(c.Context).Execute()
if err != nil {
log.Println(err)
}

sort.Strings(cryptoExchange)

c.MentionReplyf(event.Source, "Supported Exchanges: %v", strings.Join(cryptoExchange, ", "))
}

func (c *SeabirdClient) symbolsCallback(event *pb.CommandEvent) {
cryptoSymbol, _, err := c.finnhubClient.CryptoSymbols(c.Context).Exchange(strings.ToUpper(event.Arg)).Execute()
if err != nil {
log.Println(err)
return
}

var symbols []string
for _, symbol := range cryptoSymbol {
symbols = append(symbols, *symbol.DisplaySymbol)
}

sort.Strings(symbols)

c.MentionReplyf(
event.Source,
"Supported Symbols on %s: %v",
strings.Title(strings.ToLower(event.Arg)),
strings.Join(symbols, ", "))
}

func (c *SeabirdClient) cryptoCallback(event *pb.CommandEvent) {
ticker := strings.ToUpper(strings.TrimSpace(event.Arg))
exchange := "COINBASE"
currency := "USD"

split := strings.SplitN(ticker, ":", 2)
if len(split) == 2 {
exchange = split[0]
ticker = split[1]
}

split = strings.SplitN(ticker, "/", 2)
if len(split) == 2 {
ticker = split[0]
currency = split[1]
}

// TODO: we probably don't need to look this up every time - it should be
// fine to cache this and use it later. For now, it's not a huge deal to
// have an extra call for every lookup.
symbols, _, err := c.finnhubClient.CryptoSymbols(c.Context).Exchange(exchange).Execute()
if err != nil {
c.MentionReply(event.Source, "failed to look up exchange symbols")
return
}

// Convert from human readable form to internal symbol. This could be more
// efficient, but it works for now.
var query string
target := ticker + "/" + currency
for _, symbol := range symbols {
if *symbol.DisplaySymbol == target {
query = *symbol.Symbol
break
}
}

if query == "" {
c.MentionReply(event.Source, "ticker or conversion not found on that exchange")
return
}

// Gets the candle sticks for the last day and returns the Closed price which appears to an accurate current price
cryptoCandles, _, err := c.finnhubClient.CryptoCandles(c.Context).Symbol(query).Resolution("D").From(time.Now().AddDate(0, 0, -1).Unix()).To(time.Now().Unix()).Execute()
if err != nil {
log.Println(err)
return
}

if len(*cryptoCandles.C) == 0 {
c.MentionReply(event.Source, "no results")
return
}

current := cryptoCandles.C
c.MentionReplyf(event.Source, "%s - Current: $%.2f on %s", event.Arg, current, strings.Title(strings.ToLower(exchange)))
}

// Run runs
func (c *SeabirdClient) Run() error {
events, err := c.StreamEvents(map[string]*pb.CommandMetadata{
Expand All @@ -223,21 +131,6 @@ func (c *SeabirdClient) Run() error {
ShortHelp: "<ticker>",
FullHelp: "Returns current stock price for given ticker",
},
"crypto": {
Name: "crypto",
ShortHelp: "[<exchange:]<symbol>[/<conversion]",
FullHelp: "Returns current crypto price for given symbol",
},
"exchange": {
Name: "exchange",
ShortHelp: "",
FullHelp: "Returns supported crypto exchanges",
},
"symbols": {
Name: "symbols",
ShortHelp: "<exchange>",
FullHelp: "Returns supported crypto symbols for a given exchange",
},
})
if err != nil {
return err
Expand All @@ -250,12 +143,6 @@ func (c *SeabirdClient) Run() error {
switch v.Command.Command {
case "stock", "stocks", "stonk", "stonks":
go c.stockCallback(v.Command)
case "crypto":
go c.cryptoCallback(v.Command)
case "exchange":
go c.exchangeCallback(v.Command)
case "symbols":
go c.symbolsCallback(v.Command)
}
}
}
Expand Down

0 comments on commit e0ac159

Please sign in to comment.