-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreload.go
241 lines (200 loc) · 5.85 KB
/
reload.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"errors"
"fmt"
"path"
"time"
"github.com/mtgban/go-mtgban/mtgban"
)
func reloadCK() {
reloadSingle("cardkingdom")
}
func reloadSCG() {
reloadSingle("starcitygames")
}
func reloadSingle(name string) {
defer recoverPanicScraper()
ServerNotify("refresh", "Reloading "+name)
// Lock because we plan to load both sides of the scraper
ScraperOptions[name].Mutex.Lock()
ScraperOptions[name].Busy = true
defer func() {
ScraperOptions[name].Busy = false
ScraperOptions[name].Mutex.Unlock()
}()
scraper, err := ScraperOptions[name].Init(ScraperOptions[name].Logger)
if err != nil {
msg := fmt.Sprintf("error initializing %s: %s", name, err.Error())
ServerNotify("refresh", msg, true)
return
}
// These functions will update the global scraper only if it was
// previously added to the slice of Sellers or Vendors via the
// client Register functions
updateSellers(scraper)
updateVendors(scraper)
ServerNotify("refresh", name+" refresh completed")
}
func reloadTCG() {
reloadMarketOrTrader("tcg_index")
reloadMarketOrTrader("tcg_market")
reloadSingle("tcg_directnet")
ServerNotify("refresh", "tcg fully refreshed")
}
func reloadMarketOrTrader(name string) {
defer recoverPanicScraper()
ServerNotify("refresh", "Reloading "+name)
opt, found := ScraperOptions[name]
if !found {
msg := fmt.Sprintf("refresh %s not found", name)
ServerNotify("refresh", msg, true)
return
}
// Lock because we plan to load both sides of the scraper
opt.Mutex.Lock()
opt.Busy = true
defer func() {
opt.Busy = false
opt.Mutex.Unlock()
}()
scraper, err := opt.Init(opt.Logger)
if err != nil {
msg := fmt.Sprintf("error initializing %s: %s", name, err.Error())
ServerNotify("refresh", msg, true)
return
}
newbc := mtgban.NewClient()
if !ScraperOptions[name].OnlyVendor {
if len(opt.Keepers) == 0 {
newbc.RegisterSeller(scraper)
}
for _, keeper := range opt.Keepers {
newbc.RegisterMarket(scraper, keeper)
}
}
if !ScraperOptions[name].OnlySeller {
if len(opt.KeepersBL) == 0 {
newbc.RegisterVendor(scraper)
}
for _, keeper := range opt.KeepersBL {
newbc.RegisterTrader(scraper, keeper)
}
}
err = newbc.Load()
if err != nil {
msg := fmt.Sprintf("error loading new data for %s: %s", name, err.Error())
ServerNotify("refresh", msg, true)
return
}
sellers := newbc.Sellers()
for _, seller := range sellers {
updateSellers(seller)
}
vendors := newbc.Vendors()
for _, vendor := range vendors {
updateVendors(vendor)
}
ServerNotify("refresh", name+" market refresh completed")
}
func updateSellers(scraper mtgban.Scraper) {
for i := range Sellers {
if Sellers[i] != nil && Sellers[i].Info().Shorthand == scraper.Info().Shorthand {
err := updateSellerAtPosition(scraper.(mtgban.Seller), i, false)
if err != nil {
msg := fmt.Sprintf("seller %s %s - %s", scraper.Info().Name, scraper.Info().Shorthand, err.Error())
ServerNotify("refresh", msg, true)
} else {
msg := fmt.Sprintf("%s inventory updated at position %d", scraper.Info().Shorthand, i)
ServerNotify("refresh", msg)
currentDir := path.Join(InventoryDir, fmt.Sprintf("%03d", time.Now().YearDay()))
fname := path.Join(InventoryDir, scraper.Info().Shorthand+"-latest.json")
dumpInventoryToFile(scraper.(mtgban.Seller), currentDir, fname)
}
return
}
}
}
func updateSellerAtPosition(seller mtgban.Seller, i int, andLock bool) error {
opts := ScraperOptions[ScraperMap[seller.Info().Shorthand]]
if andLock {
opts.Mutex.Lock()
opts.Busy = true
defer func() {
opts.Busy = false
opts.Mutex.Unlock()
}()
}
// Load inventory
inv, err := seller.Inventory()
if err != nil {
return err
}
if len(inv) == 0 {
return errors.New("empty inventory")
}
// Do not update in case the new inventory wasn't completely loaded
// for example due to API problems
if i >= 0 {
old, _ := Sellers[i].Inventory()
if len(inv) < len(old)/2 {
return errors.New("new inventory is missing too many entries")
}
}
// Make sure the input seller is _only_ a Seller and not anything
// else, so that filtering works like expected
outSeller := mtgban.NewSellerFromInventory(inv, seller.Info())
// Save seller in global array
Sellers[i] = outSeller
return nil
}
func updateVendors(scraper mtgban.Scraper) {
for i := range Vendors {
if Vendors[i] != nil && Vendors[i].Info().Shorthand == scraper.Info().Shorthand {
err := updateVendorAtPosition(scraper.(mtgban.Vendor), i, false)
if err != nil {
msg := fmt.Sprintf("vendor %s %s - %s", scraper.Info().Name, scraper.Info().Shorthand, err.Error())
ServerNotify("refresh", msg, true)
} else {
msg := fmt.Sprintf("%s buylist updated at position %d", scraper.Info().Shorthand, i)
ServerNotify("refresh", msg)
currentDir := path.Join(BuylistDir, fmt.Sprintf("%03d", time.Now().YearDay()))
fname := path.Join(BuylistDir, scraper.Info().Shorthand+"-latest.json")
dumpBuylistToFile(scraper.(mtgban.Vendor), currentDir, fname)
}
return
}
}
}
func updateVendorAtPosition(vendor mtgban.Vendor, i int, andLock bool) error {
opts := ScraperOptions[ScraperMap[vendor.Info().Shorthand]]
if andLock {
opts.Mutex.Lock()
opts.Busy = true
defer func() {
opts.Busy = false
opts.Mutex.Unlock()
}()
}
// Load buylist
bl, err := vendor.Buylist()
if err != nil {
return err
}
if len(bl) == 0 {
return errors.New("empty buylist")
}
// Do not update in case the new inventory wasn't completely loaded
// for example due to API problems
if i >= 0 {
old, _ := Vendors[i].Buylist()
if len(bl) < len(old)/2 {
return errors.New("new buylist is missing too many entries")
}
}
// Save vendor in global array, making sure it's _only_ a Vendor
// and not anything esle, so that filtering works like expected
outVendor := mtgban.NewVendorFromBuylist(bl, vendor.Info())
// Save vendor in global array
Vendors[i] = outVendor
return nil
}