-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
156 lines (137 loc) · 3.32 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"context"
"fmt"
"log"
"log/slog"
"os"
"path/filepath"
"strings"
"time"
"github.com/ahobsonsayers/twigots"
"github.com/ahobsonsayers/twitchets/config"
"github.com/ahobsonsayers/twitchets/notification"
"github.com/joho/godotenv"
)
const (
maxNumTickets = 250
refetchTime = 1 * time.Minute
)
var lastCheckTime = time.Now()
func init() {
_ = godotenv.Load()
}
func main() {
cwd, err := os.Getwd()
if err != nil {
log.Fatalf("failed to get working directory:, %v", err)
}
// Twickets client
client := twigots.NewClient(nil)
configPath := filepath.Join(cwd, "config.yaml")
conf, err := config.Load(configPath)
if err != nil {
log.Fatalf("config error:, %v", err)
}
// Notification Clients
notificationClients, err := conf.Notification.Clients()
if err != nil {
log.Fatal(err)
}
// Event names
eventNames := make([]string, 0, len(conf.TicketsConfig))
for _, event := range conf.TicketsConfig {
eventNames = append(eventNames, event.Event)
}
slog.Info(
fmt.Sprintf("Monitoring: %s", strings.Join(eventNames, ", ")),
)
// Initial execution
fetchAndProcessTickets(client, conf, notificationClients)
// Create ticker
ticker := time.NewTicker(refetchTime)
defer ticker.Stop()
// Loop until exit
exitChan := make(chan struct{})
for {
select {
case <-ticker.C:
fetchAndProcessTickets(client, conf, notificationClients)
case <-exitChan:
return
}
}
}
func fetchAndProcessTickets(
client *twigots.Client,
conf config.Config,
notificationClients map[config.NotificationType]notification.Client,
) {
checkTime := time.Now()
defer func() {
lastCheckTime = checkTime
}()
listings, err := client.FetchTicketListings(
context.Background(),
twigots.FetchTicketListingsInput{
// Required
APIKey: conf.APIKey,
Country: twigots.CountryUnitedKingdom,
// Optional
CreatedBefore: time.Now(),
CreatedAfter: lastCheckTime,
MaxNumber: maxNumTickets,
},
)
if err != nil {
slog.Error(err.Error())
return
}
if len(listings) == maxNumTickets {
slog.Warn("Fetched the max number of tickets allowed. It is possible tickets have been missed.")
}
ticketConfigs := conf.CombineGlobalAndTicketConfig()
for _, ticketConfig := range ticketConfigs {
filter := ticketConfig.Filter()
filteredListings, err := listings.Filter(
twigots.Filter{
Event: filter.Event,
EventSimilarity: filter.EventSimilarity,
Regions: filter.Regions,
NumTickets: filter.NumTickets,
MinDiscount: filter.MinDiscount,
CreatedAfter: filter.CreatedAfter,
},
)
if err != nil {
slog.Error(
"Failed to filter listings",
"err", err,
)
continue
}
for _, listing := range filteredListings {
slog.Info(
"Found tickets for monitored event",
"eventName", listing.Event.Name,
"numTickets", listing.NumTickets,
"ticketPrice", listing.TotalPriceInclFee().String(),
"originalTicketPrice", listing.OriginalTicketPrice().String(),
"link", listing.URL(),
)
for _, notificationType := range ticketConfig.Notification {
notificationClient, ok := notificationClients[notificationType]
if !ok {
continue
}
err := notificationClient.SendTicketNotification(listing)
if err != nil {
slog.Error(
"Failed to send notification",
"err", err,
)
}
}
}
}
}