-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.go
159 lines (122 loc) · 3.43 KB
/
timer.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
157
158
159
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/jackc/pgx/v4"
"io"
"log"
"oenugs-patreon/cache"
"oenugs-patreon/patreon"
"oenugs-patreon/sql"
"oenugs-patreon/structs"
"os"
"os/signal"
"strings"
"time"
)
func UpdatePatrons() {
log.Println("Updating patrons")
patrons, err := patreon.FetchPatrons(cache.Tokens)
// 401 response, refresh the tokens
if err != nil && err.Error() == "StatusUnauthorized" {
newTokens, refreshErr := patreon.RefreshToken(cache.Tokens)
if refreshErr != nil {
log.Println("Error while refreshing tokens", refreshErr.Error())
return
}
cache.Tokens = newTokens
patrons, err = patreon.FetchPatrons(cache.Tokens)
}
if err != nil {
log.Println(err)
return
}
newCache := structs.PatronOutput{
Patrons: make([]structs.PatronDisplay, 0),
}
// for {key}, {value} := range {list}
for i, patron := range patrons.Data {
attr := patron.Attributes
// is an active patron that pays $3 or more
if attr.PatronStatus == "active_patron" && attr.WillPayAmountCents >= 300 {
userId := patron.Relationships.User.Data.Id
imageUrl := patrons.Included[i].Attributes.ImageUrl
newCache.Patrons = append(newCache.Patrons, structs.PatronDisplay{
Id: userId,
Name: attr.FullName,
ImageUrl: imageUrl,
})
}
}
log.Println("Found", len(newCache.Patrons), "patrons")
cache.PatronCache = newCache
log.Println("Updating database")
updatePatronsInDatabase(patrons.Data)
log.Println("Database update complete")
}
func StartUpdatePatronTimer() {
go UpdatePatrons()
// tick every 24 hours to update the patrons
ticker := time.NewTicker(24 * time.Hour)
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
go func() {
for {
select {
case <-ticker.C:
UpdatePatrons()
case <-sigint:
ticker.Stop()
return
}
}
}()
}
func LoadPatronCredentials() {
if _, err := os.Stat("/storage/oengus-patreon/patreon-credentials.json"); os.IsNotExist(err) {
// fetch credentials
log.Fatal("Failed to load credentials file at \"/storage/oengus-patreon/patreon-credentials.json\"")
}
// Open our jsonFile
jsonFile, err := os.Open("/storage/oengus-patreon/patreon-credentials.json")
// if we os.Open returns an error then handle it
if err != nil {
log.Fatal(err)
}
defer jsonFile.Close()
byteValue, _ := io.ReadAll(jsonFile)
json.Unmarshal(byteValue, &cache.Tokens)
}
func updatePatronsInDatabase(data []structs.PatreonMembersData) {
conn := sql.GetConnection()
defer sql.CloseConnection(conn)
batch := &pgx.Batch{}
for _, patron := range data {
attr := patron.Attributes
query := "INSERT INTO patreon_status(patreon_id, status, pledge_amount) VALUES($1, $2, $3) " +
"ON CONFLICT (patreon_id) DO UPDATE SET status = $2, pledge_amount = $3;"
userId := patron.Relationships.User.Data.Id
status := strings.ToUpper(attr.PatronStatus)
payAmount := attr.WillPayAmountCents
log.Println(userId, "is an", status, "paying", payAmount, "cents")
if status == "" {
// Ignore blank statuses
continue
}
batch.Queue(query, userId, status, payAmount)
//_, err := conn.Query(context.Background(), query, userId, status, payAmount)
//
//if err != nil {
// fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
//}
}
br := conn.SendBatch(context.Background(), batch)
for range batch.Len() {
_, err := br.Exec()
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
}
defer br.Close()
}