Skip to content
Permalink
Browse files

Trade plugin

  • Loading branch information...
Depado committed Feb 13, 2016
1 parent 777a745 commit e04bf9ca00ea08f29624c1828a2a6a657aebf014
Showing with 172 additions and 1 deletion.
  1. +1 −1 .drone.yml
  2. +99 −0 plugins/trade/command.go
  3. +51 −0 plugins/trade/options.go
  4. +20 −0 plugins/trade/trade.go
  5. +1 −0 pluginsinit/init.go
@@ -16,7 +16,7 @@ notify:
irc:
prefix: gob0tsec Build
nick: Depadrone
channel: n0sec
channel: n0sec-bot
server:
host: irc.freenode.net
port: 6667
@@ -0,0 +1,99 @@
package trade

import (
"fmt"
"strconv"
"strings"

"github.com/thoj/go-ircevent"

"github.com/depado/go-b0tsec/configuration"
"github.com/depado/go-b0tsec/plugins"
"github.com/depado/go-b0tsec/utils"
)

const (
command = "trade"
api = "https://www.cryptonator.com/api/full/%s-%s"
)

// Command is the plugin struct. It will be exposed as packagename.Command to keep the API stable and friendly.
type Command struct {
Started bool
}

func init() {
plugins.Commands[command] = new(Command)
}

// Help must send some help about what the command actually does and how to call it if there are any optional arguments.
func (c *Command) Help(ib *irc.Connection, from string) {
if !c.Started {
return
}
ib.Privmsg(from, "Displays current crypto-currency trade market")
ib.Privmsg(from, "Usage : !trade [ammount] <from> <to> [--market=all|market_name] [--nomarket] [--sort=price|volume]")
}

// Get is the actual call to your plugin.
func (c *Command) Get(ib *irc.Connection, from string, to string, args []string) {
if !c.Started {
return
}
var err error
var opt options
if to == configuration.Config.BotName {
to = from
}
if opt, err = parseOptions(args); err != nil {
ib.Privmsg(to, err.Error())
return
}
trade := Cryptonator{}
if err = utils.FetchURL(fmt.Sprintf(api, opt.Source, opt.Dest), &trade); err == nil {
rate, err := strconv.ParseFloat(trade.Ticker.Price, 64)
if err != nil {
ib.Privmsgf(to, "Couldn't get the price of %s or %s", opt.Source, opt.Dest)
return
}
if opt.Ammount == 1 {
ib.Privmsgf(to, "%v %s = %f %s", opt.Ammount, trade.Ticker.Base, float64(opt.Ammount)*rate, trade.Ticker.Target)
} else {
ib.Privmsgf(to, "%v %s = %f %s (1 %s = %f %s)", opt.Ammount, trade.Ticker.Base, float64(opt.Ammount)*rate, trade.Ticker.Target, trade.Ticker.Base, rate, trade.Ticker.Target)
}
if opt.Market != "" {
if opt.Market == "all" {
for _, m := range trade.Ticker.Markets {
ib.Privmsgf(to, "[%s] %s %s [%f]", m.Market, m.Price, trade.Ticker.Target, m.Volume)
}
} else {
for _, m := range trade.Ticker.Markets {
if strings.ToLower(m.Market) == strings.ToLower(opt.Market) {
ib.Privmsgf(to, "[%s] %s %s [%f]", m.Market, m.Price, trade.Ticker.Target, m.Volume)
}
}
}
}
} else {
fmt.Println(err)
}
}

// Start starts the plugin and returns any occurred error, nil otherwise
func (c *Command) Start() error {
if utils.StringInSlice(command, configuration.Config.Commands) {
c.Started = true
}
return nil
}

// Stop stops the plugin and returns any occurred error, nil otherwise
func (c *Command) Stop() error {
c.Started = false
return nil
}

// IsStarted returns the state of the plugin
func (c *Command) IsStarted() bool {
return c.Started
}
@@ -0,0 +1,51 @@
package trade

import (
"fmt"
"strconv"
"strings"
)

type options struct {
PriceSort bool
VolumeSort bool
Source string
Dest string
Ammount int
Market string
}

func parseOptions(args []string) (options, error) {
var err error
opt := options{}

var c []string
for _, v := range args {
if strings.HasPrefix(v, "--market=") {
opt.Market = strings.TrimPrefix(v, "--market=")
} else if strings.HasPrefix(v, "--sort=") {
switch strings.TrimPrefix(v, "--sort=") {
case "price":
opt.PriceSort = true
case "volume":
opt.VolumeSort = true
}
} else {
c = append(c, v)
}
}
if len(c) == 3 {
if opt.Ammount, err = strconv.Atoi(args[0]); err != nil {
return opt, fmt.Errorf("Wrong value of first argument : %v not a number.", c[0])
}
opt.Source = c[1]
opt.Dest = c[2]
} else if len(c) == 2 {
opt.Ammount = 1
opt.Source = c[0]
opt.Dest = c[1]
} else {
return opt, fmt.Errorf("Not enough arguments.")
}
return opt, err
}
@@ -0,0 +1,20 @@
package trade

// Cryptonator represents a structured response from the Cryptonator API
type Cryptonator struct {
Ticker struct {
Base string `json:"base"`
Target string `json:"target"`
Price string `json:"price"`
Volume string `json:"volume"`
Change string `json:"change"`
Markets []struct {
Market string `json:"market"`
Price string `json:"price"`
Volume float64 `json:"volume"`
} `json:"markets"`
} `json:"ticker"`
Timestamp int `json:"timestamp"`
Success bool `json:"success"`
Error string `json:"error"`
}
@@ -15,6 +15,7 @@ import (
_ "github.com/depado/go-b0tsec/plugins/markov"
_ "github.com/depado/go-b0tsec/plugins/seen"
_ "github.com/depado/go-b0tsec/plugins/title"
_ "github.com/depado/go-b0tsec/plugins/trade"
_ "github.com/depado/go-b0tsec/plugins/translate"
_ "github.com/depado/go-b0tsec/plugins/urban"
_ "github.com/depado/go-b0tsec/plugins/usercommand"

0 comments on commit e04bf9c

Please sign in to comment.
You can’t perform that action at this time.