This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
reload.go
209 lines (175 loc) · 5.23 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
package main
import (
"errors"
"fmt"
"path"
"time"
"github.com/mtgban/go-mtgban/mtgban"
"github.com/mtgban/go-mtgban/tcgplayer"
"golang.org/x/exp/slices"
)
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() {
reloadMarket("tcg_index")
reloadMarket("tcg_market")
loadTCGDirectNet(Vendors)
ServerNotify("refresh", "tcg fully refreshed")
}
func reloadMarket(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
}
multiSellers, err := mtgban.Seller2Sellers(scraper.(mtgban.Market))
if err != nil {
msg := fmt.Sprintf("error separating %s: %s", name, err.Error())
ServerNotify("refresh", msg)
return
}
keepers := ScraperOptions[name].Keepers
for i := range multiSellers {
// Skip subsellers not explicitly enabled
if !slices.Contains(keepers, multiSellers[i].Info().Shorthand) {
continue
}
updateSellers(multiSellers[i])
}
// This can be done because only the already-registered scrapers
// will be updated, no effect otherwise
updateVendors(scraper)
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)
continue
}
ServerNotify("refresh", scraper.Info().Shorthand+" inventory updated")
}
}
}
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")
}
// Save seller in global array, making sure it's _only_ a Seller
// and not anything esle, so that filtering works like expected
Sellers[i] = mtgban.NewSellerFromInventory(inv, seller.Info())
targetDir := path.Join(InventoryDir, time.Now().Format("2006-01-02/15"))
go uploadSeller(Sellers[i], targetDir)
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)
continue
}
ServerNotify("refresh", scraper.Info().Shorthand+" buylist updated")
}
}
}
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")
}
// Save vendor in global array, making sure it's _only_ a Vendor
// and not anything esle, so that filtering works like expected
Vendors[i] = mtgban.NewVendorFromBuylist(bl, vendor.Info())
targetDir := path.Join(BuylistDir, time.Now().Format("2006-01-02/15"))
go uploadVendor(Vendors[i], targetDir)
return nil
}
// Load the fake buylist from the TCG Direct data
func loadTCGDirectNet(newVendors []mtgban.Vendor) {
for _, seller := range Sellers {
if seller != nil && seller.Info().Name == TCG_DIRECT {
for i := range newVendors {
if newVendors[i] != nil && newVendors[i].Info().Name == TCG_DIRECT_NET {
tcg := tcgplayer.NewTCGDirectNet()
tcg.DirectInventory, _ = seller.Inventory()
// No error possible
tcg.Buylist()
// Replace the vendor
newVendors[i] = tcg
break
}
}
break
}
}
}