From 30016f24ac23f6977966251827121c5a8e0d6538 Mon Sep 17 00:00:00 2001 From: Krishan Bhasin <8904718+KrishanBhasin@users.noreply.github.com> Date: Thu, 21 Aug 2025 22:53:03 +0100 Subject: [PATCH 1/4] make refetchtime configurable --- config.example.yaml | 2 ++ config/config.go | 6 ++++++ main.go | 3 +-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/config.example.yaml b/config.example.yaml index 027dce3..b44f404 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -53,6 +53,8 @@ global: notification: - ntfy + 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..e1574f7 100644 --- a/config/config.go +++ b/config/config.go @@ -3,6 +3,7 @@ package config import ( "errors" "fmt" + "time" "github.com/ahobsonsayers/twigots" ) @@ -10,6 +11,7 @@ import ( type Config struct { APIKey string `json:"apiKey"` Country twigots.Country `json:"country"` + RefetchTime time.Duration `json:"refetchTime"` Notification NotificationConfig `json:"notification"` GlobalTicketConfig GlobalTicketListingConfig `json:"global"` TicketConfigs []TicketListingConfig `json:"tickets"` @@ -27,6 +29,10 @@ func (c Config) Validate() error { return fmt.Errorf("country '%s' is not valid", c.Country) } + if c.RefetchTime <= 0 { + c.RefetchTime = 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/main.go b/main.go index 24d2194..51f7f11 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.RefetchTime) defer ticker.Stop() // Loop until exit From 448814d03c79c6b882992894d8be2c60126f9536 Mon Sep 17 00:00:00 2001 From: Krishan Bhasin <8904718+KrishanBhasin@users.noreply.github.com> Date: Fri, 22 Aug 2025 08:43:09 +0100 Subject: [PATCH 2/4] fixup! make refetchtime configurable --- config/config.go | 20 ++++++++++++-------- main.go | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/config/config.go b/config/config.go index e1574f7..682b3e6 100644 --- a/config/config.go +++ b/config/config.go @@ -9,12 +9,13 @@ import ( ) type Config struct { - APIKey string `json:"apiKey"` - Country twigots.Country `json:"country"` - RefetchTime time.Duration `json:"refetchTime"` - 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 { @@ -29,8 +30,11 @@ func (c Config) Validate() error { return fmt.Errorf("country '%s' is not valid", c.Country) } - if c.RefetchTime <= 0 { - c.RefetchTime = 1 * time.Minute // Default value + var timeParseError error + c.RefetchTimeDuration, timeParseError = time.ParseDuration(c.RefetchTime) + + if timeParseError != nil { + c.RefetchTimeDuration = 1 * time.Minute // Default value } err := c.Notification.Validate() diff --git a/main.go b/main.go index 51f7f11..9481fe7 100644 --- a/main.go +++ b/main.go @@ -74,7 +74,7 @@ func main() { fetchAndProcessTickets(client, notificationClients, listingConfigs) // Create ticker - ticker := time.NewTicker(conf.RefetchTime) + ticker := time.NewTicker(conf.RefetchTimeDuration) defer ticker.Stop() // Loop until exit From 1ad846d2d172ccb08190d5f23df07064bcdea4a5 Mon Sep 17 00:00:00 2001 From: Krishan Bhasin <8904718+KrishanBhasin@users.noreply.github.com> Date: Fri, 22 Aug 2025 08:49:16 +0100 Subject: [PATCH 3/4] pointer to config? --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 682b3e6..7d67fd2 100644 --- a/config/config.go +++ b/config/config.go @@ -18,7 +18,7 @@ type Config struct { 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") } From 05c19438708bcf4dbcba321a855a547ee89e2704 Mon Sep 17 00:00:00 2001 From: Krishan Bhasin <8904718+KrishanBhasin@users.noreply.github.com> Date: Fri, 22 Aug 2025 09:04:48 +0100 Subject: [PATCH 4/4] fix tests --- config.example.yaml | 3 ++- config/config_test.go | 9 +++++++-- test/data/config/config.yaml | 2 ++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/config.example.yaml b/config.example.yaml index b44f404..b47afea 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -53,7 +53,8 @@ global: notification: - ntfy - refetchTime: 1m +# How long should we wait before polling Twickets? +refetchTime: 1m # Individual ticket configuration # Available settings match the global ones 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/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