diff --git a/config.example.yaml b/config.example.yaml index 027dce3..b47afea 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -53,6 +53,9 @@ global: notification: - ntfy +# How long should we wait before polling Twickets? +refetchTime: 1m + # Individual ticket configuration # Available settings match the global ones # Settings here override global settings above diff --git a/config/config.go b/config/config.go index 3aa6f0a..7d67fd2 100644 --- a/config/config.go +++ b/config/config.go @@ -3,19 +3,22 @@ package config import ( "errors" "fmt" + "time" "github.com/ahobsonsayers/twigots" ) type Config struct { - APIKey string `json:"apiKey"` - Country twigots.Country `json:"country"` - Notification NotificationConfig `json:"notification"` - GlobalTicketConfig GlobalTicketListingConfig `json:"global"` - TicketConfigs []TicketListingConfig `json:"tickets"` + APIKey string `json:"apiKey"` + Country twigots.Country `json:"country"` + RefetchTime string `json:"refetchTime"` + RefetchTimeDuration time.Duration `json:"-"` + Notification NotificationConfig `json:"notification"` + GlobalTicketConfig GlobalTicketListingConfig `json:"global"` + TicketConfigs []TicketListingConfig `json:"tickets"` } -func (c Config) Validate() error { +func (c *Config) Validate() error { if c.APIKey == "" { return errors.New("api key must be set") } @@ -27,6 +30,13 @@ func (c Config) Validate() error { return fmt.Errorf("country '%s' is not valid", c.Country) } + var timeParseError error + c.RefetchTimeDuration, timeParseError = time.ParseDuration(c.RefetchTime) + + if timeParseError != nil { + c.RefetchTimeDuration = 1 * time.Minute // Default value + } + err := c.Notification.Validate() if err != nil { return fmt.Errorf("notification config is not valid: %w", err) diff --git a/config/config_test.go b/config/config_test.go index 0a318b1..3a6642f 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -2,6 +2,7 @@ package config_test import ( "testing" + "time" "github.com/ahobsonsayers/twigots" "github.com/ahobsonsayers/twitchets/config" @@ -23,10 +24,14 @@ func TestLoadConfig(t *testing.T) { // nolint: revive globalNumTickets := 2 globalMaxTicketPrice := 25.0 globalDiscount := 25.0 + refetchTime := "30s" + refetchTimeDuration := 30 * time.Second expectedConfig := config.Config{ - APIKey: "test", - Country: country, + APIKey: "test", + Country: country, + RefetchTime: refetchTime, + RefetchTimeDuration: refetchTimeDuration, GlobalTicketConfig: config.GlobalTicketListingConfig{ EventSimilarity: globalEventSimilarity, Regions: globalRegions, diff --git a/main.go b/main.go index 24d2194..9481fe7 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,6 @@ import ( const ( maxNumTickets = 250 - refetchTime = 1 * time.Minute ) var latestTicketTime time.Time @@ -75,7 +74,7 @@ func main() { fetchAndProcessTickets(client, notificationClients, listingConfigs) // Create ticker - ticker := time.NewTicker(refetchTime) + ticker := time.NewTicker(conf.RefetchTimeDuration) defer ticker.Stop() // Loop until exit diff --git a/test/data/config/config.yaml b/test/data/config/config.yaml index 257e874..d2cb7a4 100644 --- a/test/data/config/config.yaml +++ b/test/data/config/config.yaml @@ -23,6 +23,8 @@ global: maxTicketPrice: 25 discount: 25 +refetchTime: 30s + tickets: # Ticket with only event set - event: Event 1