2023-06-05 13:41:48 +00:00
|
|
|
package alert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.dev.m-and-m.ovh/mderasse/tesla/api"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var apiClient *api.Client
|
2023-06-11 16:15:47 +00:00
|
|
|
var alert = 0
|
2023-06-05 13:41:48 +00:00
|
|
|
|
|
|
|
// Init will initialize telegram bot.
|
|
|
|
func Init(alertChan chan api.Availability) {
|
|
|
|
log.Info("Starting alert ticker")
|
|
|
|
|
|
|
|
var err error
|
|
|
|
apiClient, err = api.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Fail to instantiate the HTTP Client. Error: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
priceAlertStr := os.Getenv("PRICE_ALERT")
|
|
|
|
if priceAlertStr != "" {
|
|
|
|
PriceAlert, err = strconv.ParseInt(priceAlertStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("invalid configuration. PRICE_ALERT environement variable should be an integer.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// define an interval and the ticker for this interval
|
2023-06-22 12:41:05 +00:00
|
|
|
interval := time.Duration(2) * time.Second
|
2023-06-05 13:41:48 +00:00
|
|
|
// create a new Ticker
|
|
|
|
tk := time.NewTicker(interval)
|
|
|
|
|
2023-06-11 16:15:47 +00:00
|
|
|
resetAlert := 0
|
2023-06-05 13:41:48 +00:00
|
|
|
for range tk.C {
|
2023-06-11 16:15:47 +00:00
|
|
|
if alert > 10 {
|
|
|
|
alert = 0
|
|
|
|
resetAlert = 10
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if resetAlert != 0 {
|
|
|
|
resetAlert--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-06-05 13:41:48 +00:00
|
|
|
checkPrice(alertChan)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkPrice(alertChan chan api.Availability) {
|
|
|
|
log.Info("Ticker - Checking price")
|
|
|
|
|
|
|
|
availabilities, err := apiClient.GetAvailabilities(context.Background(), &api.AvailabilityParams{
|
|
|
|
Query: carFilter,
|
2023-06-19 21:04:59 +00:00
|
|
|
Count: 100,
|
2023-06-05 13:41:48 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Fail to contact API. Error: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-21 13:50:27 +00:00
|
|
|
StdAlert := false
|
|
|
|
LrAlert := false
|
2023-06-19 21:04:59 +00:00
|
|
|
for _, availability := range availabilities.Results {
|
2023-06-21 13:47:10 +00:00
|
|
|
|
|
|
|
if availability.IsDemo {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-06-21 13:50:27 +00:00
|
|
|
if !StdAlert && availability.Price < 39000 && availability.Trim[0] == "SRRWD" {
|
2023-06-19 21:04:59 +00:00
|
|
|
log.Info("Launching an alert !")
|
|
|
|
alertChan <- availability
|
|
|
|
alert++
|
2023-06-21 13:50:27 +00:00
|
|
|
StdAlert = true
|
2023-06-19 21:04:59 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 13:50:27 +00:00
|
|
|
if !LrAlert && availability.Price < 47000 && availability.Trim[0] == "LRAWD" {
|
2023-06-19 21:04:59 +00:00
|
|
|
log.Info("Launching an alert !")
|
|
|
|
alertChan <- availability
|
|
|
|
alert++
|
2023-06-21 13:50:27 +00:00
|
|
|
LrAlert = true
|
2023-06-19 21:04:59 +00:00
|
|
|
}
|
2023-06-05 13:41:48 +00:00
|
|
|
}
|
|
|
|
}
|