Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 16 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config_test

import (
"testing"
"time"

"github.com/ahobsonsayers/twigots"
"github.com/ahobsonsayers/twitchets/config"
Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

const (
maxNumTickets = 250
refetchTime = 1 * time.Minute
)

var latestTicketTime time.Time
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/data/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ global:
maxTicketPrice: 25
discount: 25

refetchTime: 30s

tickets:
# Ticket with only event set
- event: Event 1
Expand Down