diff --git a/app/console/commands/alert.go b/app/console/commands/alert.go index a0e53e1..88fb016 100644 --- a/app/console/commands/alert.go +++ b/app/console/commands/alert.go @@ -16,7 +16,6 @@ func init() { cmd.Add(&Alert{}) } - type Alert struct { } @@ -28,7 +27,6 @@ func (hw *Alert) Description() string { return "Alert crypto ticker" } - func (hw *Alert) Handler(arg *cmd.Arg) error { pair, err := arg.Get("pair") if err != nil { @@ -57,7 +55,7 @@ func (hw *Alert) Handler(arg *cmd.Arg) error { if err != nil { return err } - a, err := alert.New(*pair, zone.Duration(durationInt64) * zone.Minute, *difference) + a, err := alert.New(*pair, zone.Duration(durationInt64)*zone.Minute, *difference) if err != nil { return err } @@ -65,4 +63,3 @@ func (hw *Alert) Handler(arg *cmd.Arg) error { // you can diy your own Fetchers or Notifiers return a.Fetch(new(fetchers.HuoBi), new(notifiers.Pushover)) } - diff --git a/app/logics/alert/alert.go b/app/logics/alert/alert.go index 2792b0c..562dff9 100644 --- a/app/logics/alert/alert.go +++ b/app/logics/alert/alert.go @@ -14,12 +14,11 @@ import ( "strings" ) - type alert struct { - pair string - duration zone.Duration - difference string - + pair string + duration zone.Duration + difference string + upDifference bigfloat.BigFloat downDifference bigfloat.BigFloat } @@ -30,7 +29,6 @@ func New(pair string, duration zone.Duration, difference string) (*alert, error) return a, err } - type Response struct { Status string `json:"status"` Ch string `json:"ch"` @@ -50,7 +48,7 @@ type TickData struct { Direction string `json:"direction"` } -func (alert *alert)Fetch(fetcher Fetcher, notifier Notifier) error { +func (alert *alert) Fetch(fetcher Fetcher, notifier Notifier) error { resp, err := fetcher.Fetch(alert.pair) if err != nil { return err @@ -76,13 +74,13 @@ func (alert *alert)Fetch(fetcher Fetcher, notifier Notifier) error { func (alert *alert) notify(notifier Notifier, direction Direction, nowData *TickData) error { - cache.Put(alert.pair + "_notified", nowData.Price.String(), zone.Now().Add(alert.duration)) + cache.Put(alert.pair+"_notified", nowData.Price.String(), zone.Now().Add(alert.duration)) diff, _ := strconv.ParseFloat(alert.difference, 64) dataStr, _ := json.Marshal(nowData) - return notifier.Notify( strings.ToUpper(alert.pair), direction, fmt.Sprintf("%.2f%%", diff * 100), nowData.Price.String(), string(dataStr)) + return notifier.Notify(strings.ToUpper(alert.pair), direction, fmt.Sprintf("%.2f%%", diff*100), nowData.Price.String(), string(dataStr)) } func nowData(resp *Response) *TickData { diff --git a/app/logics/alert/fetchers/huobi.go b/app/logics/alert/fetchers/huobi.go index 48e580b..1cef789 100644 --- a/app/logics/alert/fetchers/huobi.go +++ b/app/logics/alert/fetchers/huobi.go @@ -10,16 +10,14 @@ import ( "totoval/app/logics/alert" ) - const ( HuobiMarketTradeBaseUrl = "https://api.huobi.pro/market/trade" ) type HuoBi struct { - } -func (hb *HuoBi)Fetch(pair string)(resp *alert.Response, err error){ +func (hb *HuoBi) Fetch(pair string) (resp *alert.Response, err error) { statusCode, err := biu.Ready(biu.MethodGet, HuobiMarketTradeBaseUrl, &biu.Options{ ProxyUrl: config.GetString("alert.proxy"), UrlParam: &biu.UrlParam{ diff --git a/app/logics/alert/notifiers/pushover.go b/app/logics/alert/notifiers/pushover.go index 8559c18..58a547a 100644 --- a/app/logics/alert/notifiers/pushover.go +++ b/app/logics/alert/notifiers/pushover.go @@ -6,28 +6,29 @@ import ( "github.com/totoval/framework/config" "totoval/app/logics/alert" ) + const ( PushOverMessageUrl = "https://api.pushover.net/1/messages.json" ) type Pushover struct { - } -func (po *Pushover)Notify(pair string, direction alert.Direction, differencePercentage string, price string, rawDataStr string) error { + +func (po *Pushover) Notify(pair string, direction alert.Direction, differencePercentage string, price string, rawDataStr string) error { directionStr := "-" if direction == alert.Up { directionStr = "📈" - }else{ + } else { directionStr = "📉" } _, err := biu.Ready(biu.MethodPost, PushOverMessageUrl, &biu.Options{ Body: &biu.Body{ - "token": config.GetString("pushover.token"), - "user": config.GetString("pushover.user"), - "device": config.GetString("pushover.device"), + "token": config.GetString("pushover.token"), + "user": config.GetString("pushover.user"), + "device": config.GetString("pushover.device"), - "title": fmt.Sprintf("[%s] %s %s !!! {%s}", pair, directionStr, differencePercentage, price), + "title": fmt.Sprintf("[%s] %s %s !!! {%s}", pair, directionStr, differencePercentage, price), "message": string(rawDataStr), }, }).Biu().Status() diff --git a/bootstrap/app.go b/bootstrap/app.go index 0b8745a..5dc8af3 100644 --- a/bootstrap/app.go +++ b/bootstrap/app.go @@ -4,8 +4,8 @@ import ( "github.com/totoval/framework/cache" "github.com/totoval/framework/helpers/zone" "github.com/totoval/framework/logs" - "github.com/totoval/framework/validator" "github.com/totoval/framework/sentry" + "github.com/totoval/framework/validator" "totoval/config" "totoval/resources/lang"