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
/
api_banprice.go
694 lines (627 loc) · 18.2 KB
/
api_banprice.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"log"
"net/http"
"path"
"sort"
"strconv"
"strings"
"time"
"github.com/mtgban/go-mtgban/mtgmatcher"
"golang.org/x/exp/slices"
)
const (
APIVersion = "1"
)
type BanPrice struct {
Regular float64 `json:"regular,omitempty"`
Foil float64 `json:"foil,omitempty"`
Etched float64 `json:"etched,omitempty"`
Qty int `json:"qty,omitempty"`
QtyFoil int `json:"qty_foil,omitempty"`
QtyEtched int `json:"qty_etched,omitempty"`
Conditions map[string]float64 `json:"conditions,omitempty"`
}
type PriceAPIOutput struct {
Error string `json:"error,omitempty"`
Meta struct {
Date time.Time `json:"date"`
Version string `json:"version"`
BaseURL string `json:"base_url"`
} `json:"meta"`
// uuid > store > price {regular/foil/etched}
Retail map[string]map[string]*BanPrice `json:"retail,omitempty"`
Buylist map[string]map[string]*BanPrice `json:"buylist,omitempty"`
}
func PriceAPI(w http.ResponseWriter, r *http.Request) {
sig := r.FormValue("sig")
out := PriceAPIOutput{}
out.Meta.Date = time.Now()
out.Meta.Version = APIVersion
out.Meta.BaseURL = getBaseURL(r) + "/go/"
urlPath := strings.TrimPrefix(r.URL.Path, "/api/mtgban/")
if !strings.HasSuffix(urlPath, ".json") && !strings.HasSuffix(urlPath, ".csv") {
out.Error = "Not found"
json.NewEncoder(w).Encode(&out)
return
}
storesOpt := GetParamFromSig(sig, "API")
if DevMode && !SigCheck && storesOpt == "" {
storesOpt = "DEV_ACCESS"
}
var enabledStores []string
switch storesOpt {
case "ALL_ACCESS":
for _, seller := range Sellers {
if seller != nil && !slices.Contains(Config.SearchRetailBlockList, seller.Info().Shorthand) {
enabledStores = append(enabledStores, seller.Info().Shorthand)
}
}
for _, vendor := range Vendors {
if vendor != nil && !slices.Contains(Config.SearchBuylistBlockList, vendor.Info().Shorthand) {
enabledStores = append(enabledStores, vendor.Info().Shorthand)
}
}
case "DEV_ACCESS":
for _, seller := range Sellers {
if seller != nil {
enabledStores = append(enabledStores, seller.Info().Shorthand)
}
}
for _, vendor := range Vendors {
if vendor != nil {
enabledStores = append(enabledStores, vendor.Info().Shorthand)
}
}
default:
enabledStores = strings.Split(storesOpt, ",")
}
enabledModes := strings.Split(GetParamFromSig(sig, "APImode"), ",")
idOpt := r.FormValue("id")
qty, _ := strconv.ParseBool(r.FormValue("qty"))
conds, _ := strconv.ParseBool(r.FormValue("conds"))
filterByFinish := r.FormValue("finish")
showFullName, _ := strconv.ParseBool(r.FormValue("full"))
// Filter by user preference, as long as it's listed in the enebled stores
filterByVendor := r.FormValue("vendor")
if slices.Contains(enabledStores, filterByVendor) {
enabledStores = []string{filterByVendor}
}
filterByEdition := ""
var filterByHash []string
if strings.Contains(urlPath, "/") {
base := path.Base(urlPath)
if strings.HasSuffix(urlPath, ".json") {
base = strings.TrimSuffix(base, ".json")
} else if strings.HasSuffix(urlPath, ".csv") {
base = strings.TrimSuffix(base, ".csv")
}
// Check if the path element is a set name or a hash
set, err := mtgmatcher.GetSet(base)
if err == nil {
filterByEdition = set.Code
} else {
for _, opts := range [][]bool{
// Check for nonfoil, foil, etched
[]bool{false, false}, []bool{true, false}, []bool{false, true},
} {
uuid, err := mtgmatcher.MatchId(base, opts...)
if err != nil {
continue
}
// Skip if hash is already present
if slices.Contains(filterByHash, uuid) {
continue
}
filterByHash = append(filterByHash, uuid)
}
// Speed up search by keeping only the needed edition
if len(filterByHash) > 0 {
co, err := mtgmatcher.GetUUID(filterByHash[0])
if err == nil {
filterByEdition = co.SetCode
}
}
}
if filterByEdition == "" && filterByHash == nil {
out.Error = "Not found"
json.NewEncoder(w).Encode(&out)
return
}
}
// Only filtered output can have csv encoding, and only for retail or buylist requests
checkCSVoutput := (filterByEdition == "" && filterByHash == nil && filterByFinish == "") || strings.HasPrefix(urlPath, "all")
if strings.HasSuffix(urlPath, ".csv") && checkCSVoutput {
out.Error = "Invalid request"
json.NewEncoder(w).Encode(&out)
return
}
// Only search conditions when a single store is enabled, or if a list of card is requested
if len(enabledStores) == 1 {
conds = true
} else if conds {
conds = filterByHash != nil
}
start := time.Now()
dumpType := ""
canRetail := slices.Contains(enabledModes, "retail") || (slices.Contains(enabledModes, "all") || (DevMode && !SigCheck))
canBuylist := slices.Contains(enabledModes, "buylist") || (slices.Contains(enabledModes, "all") || (DevMode && !SigCheck))
if (strings.HasPrefix(urlPath, "retail") || strings.HasPrefix(urlPath, "all")) && canRetail {
dumpType += "retail"
out.Retail = getSellerPrices(idOpt, enabledStores, filterByEdition, filterByHash, filterByFinish, qty, conds)
}
if (strings.HasPrefix(urlPath, "buylist") || strings.HasPrefix(urlPath, "all")) && canBuylist {
dumpType += "buylist"
out.Buylist = getVendorPrices(idOpt, enabledStores, filterByEdition, filterByHash, filterByFinish, qty, conds)
}
user := GetParamFromSig(sig, "UserEmail")
msg := fmt.Sprintf("[%v] %s requested a '%s' API dump ('%s','%q','%s')", time.Since(start), user, dumpType, filterByEdition, filterByHash, filterByFinish)
if qty {
msg += " with quantities"
}
if conds {
msg += " with conditions"
}
if strings.HasSuffix(urlPath, ".json") {
msg += " in json"
} else if strings.HasSuffix(urlPath, ".csv") {
msg += " in csv"
}
if DevMode {
log.Println(msg)
} else {
UserNotify("api", msg)
}
if out.Retail == nil && out.Buylist == nil {
out.Error = "Not found"
json.NewEncoder(w).Encode(&out)
return
}
if strings.HasSuffix(urlPath, ".json") {
json.NewEncoder(w).Encode(&out)
return
} else if strings.HasSuffix(urlPath, ".csv") {
w.Header().Set("Content-Type", "text/csv")
var err error
csvWriter := csv.NewWriter(w)
if out.Retail != nil {
err = BanPrice2CSV(csvWriter, out.Retail, qty, conds, showFullName)
} else if out.Buylist != nil {
err = BanPrice2CSV(csvWriter, out.Buylist, qty, conds, showFullName)
}
if err != nil {
log.Println(err)
}
return
}
out.Error = "Internal Server Error"
json.NewEncoder(w).Encode(&out)
}
func getIdFunc(mode string) func(co *mtgmatcher.CardObject) string {
switch mode {
case "tcg":
return func(co *mtgmatcher.CardObject) string {
if co.Etched {
id, found := co.Identifiers["tcgplayerEtchedProductId"]
if found {
return id
}
}
return co.Identifiers["tcgplayerProductId"]
}
case "scryfall":
return func(co *mtgmatcher.CardObject) string {
return co.Identifiers["scryfallId"]
}
case "mtgjson":
return func(co *mtgmatcher.CardObject) string {
return co.Identifiers["mtgjsonId"]
}
case "mkm":
return func(co *mtgmatcher.CardObject) string {
return co.Identifiers["mcmId"]
}
case "ck":
return func(co *mtgmatcher.CardObject) string {
if co.Etched {
id, found := co.Identifiers["cardKingdomEtchedId"]
if found {
return id
}
} else if co.Foil {
return co.Identifiers["cardKingdomFoilId"]
}
return co.Identifiers["cardKingdomId"]
}
}
return func(co *mtgmatcher.CardObject) string {
return co.UUID
}
}
func getSellerPrices(mode string, enabledStores []string, filterByEdition string, filterByHash []string, filterByFinish string, qty bool, conds bool) map[string]map[string]*BanPrice {
out := map[string]map[string]*BanPrice{}
idFunc := getIdFunc(mode)
for _, seller := range Sellers {
if seller == nil {
continue
}
sellerTag := seller.Info().Shorthand
// Only keep singles
if seller.Info().SealedMode {
continue
}
// Skip any seller that are not enabled
if !slices.Contains(enabledStores, sellerTag) {
continue
}
// Get inventory
inventory, err := seller.Inventory()
if err != nil {
log.Println(err)
continue
}
// Loop through cards
for cardId := range inventory {
// No price no dice
if len(inventory[cardId]) == 0 || inventory[cardId][0].Price == 0 {
continue
}
co, err := mtgmatcher.GetUUID(cardId)
if err != nil {
continue
}
if filterByEdition != "" && co.SetCode != filterByEdition {
continue
}
if filterByHash != nil && !slices.Contains(filterByHash, cardId) {
continue
}
if filterByFinish != "" && checkFinish(co, filterByFinish) {
continue
}
id := idFunc(co)
_, found := out[id]
if !found {
out[id] = map[string]*BanPrice{}
}
if out[id][sellerTag] == nil {
out[id][sellerTag] = &BanPrice{}
}
// Determine whether the response should include qty information
// Needs to be explicitly requested, all the index prices are skipped,
// TCG is too due to how quantities are stored in mtgban (FIXME?)
// (only for retail).
shouldQty := qty && !seller.Info().MetadataOnly && sellerTag != "TCG Player" && sellerTag != "TCG Direct"
if co.Etched {
out[id][sellerTag].Etched = inventory[cardId][0].Price
if shouldQty {
for i := range inventory[cardId] {
out[id][sellerTag].QtyEtched += inventory[cardId][i].Quantity
}
}
if conds {
if out[id][sellerTag].Conditions == nil {
out[id][sellerTag].Conditions = map[string]float64{}
}
for i := range inventory[cardId] {
condTag := inventory[cardId][i].Conditions
out[id][sellerTag].Conditions[condTag+"_etched"] = inventory[cardId][i].Price
}
}
} else if co.Foil {
out[id][sellerTag].Foil = inventory[cardId][0].Price
if shouldQty {
for i := range inventory[cardId] {
out[id][sellerTag].QtyFoil += inventory[cardId][i].Quantity
}
}
if conds {
if out[id][sellerTag].Conditions == nil {
out[id][sellerTag].Conditions = map[string]float64{}
}
for i := range inventory[cardId] {
condTag := inventory[cardId][i].Conditions
out[id][sellerTag].Conditions[condTag+"_foil"] = inventory[cardId][i].Price
}
}
} else {
out[id][sellerTag].Regular = inventory[cardId][0].Price
if shouldQty {
for i := range inventory[cardId] {
out[id][sellerTag].Qty += inventory[cardId][i].Quantity
}
}
if conds {
if out[id][sellerTag].Conditions == nil {
out[id][sellerTag].Conditions = map[string]float64{}
}
for i := range inventory[cardId] {
out[id][sellerTag].Conditions[inventory[cardId][i].Conditions] = inventory[cardId][i].Price
}
}
}
}
}
return out
}
func getVendorPrices(mode string, enabledStores []string, filterByEdition string, filterByHash []string, filterByFinish string, qty bool, conds bool) map[string]map[string]*BanPrice {
out := map[string]map[string]*BanPrice{}
idFunc := getIdFunc(mode)
for _, vendor := range Vendors {
if vendor == nil {
continue
}
vendorTag := vendor.Info().Shorthand
// Only keep singles
if vendor.Info().SealedMode {
continue
}
// Skip any vendor that are not enabled
if !slices.Contains(enabledStores, vendorTag) {
continue
}
// Get buylist
buylist, err := vendor.Buylist()
if err != nil {
log.Println(err)
continue
}
// Loop through cards
for cardId := range buylist {
// No price no dice
if len(buylist[cardId]) == 0 || buylist[cardId][0].BuyPrice == 0 {
continue
}
co, err := mtgmatcher.GetUUID(cardId)
if err != nil {
continue
}
if filterByEdition != "" && co.SetCode != filterByEdition {
continue
}
if filterByHash != nil && !slices.Contains(filterByHash, cardId) {
continue
}
if filterByFinish != "" && checkFinish(co, filterByFinish) {
continue
}
id := idFunc(co)
_, found := out[id]
if !found {
out[id] = map[string]*BanPrice{}
}
if out[id][vendorTag] == nil {
out[id][vendorTag] = &BanPrice{}
}
if co.Etched {
out[id][vendorTag].Etched = buylist[cardId][0].BuyPrice
if qty && !vendor.Info().MetadataOnly {
for i := range buylist[cardId] {
out[id][vendorTag].QtyEtched += buylist[cardId][i].Quantity
}
}
if conds {
if out[id][vendorTag].Conditions == nil {
out[id][vendorTag].Conditions = map[string]float64{}
}
for i := range buylist[cardId] {
condTag := buylist[cardId][i].Conditions
out[id][vendorTag].Conditions[condTag+"_etched"] = buylist[cardId][i].BuyPrice
}
}
} else if co.Foil {
out[id][vendorTag].Foil = buylist[cardId][0].BuyPrice
if qty && !vendor.Info().MetadataOnly {
for i := range buylist[cardId] {
out[id][vendorTag].QtyFoil += buylist[cardId][i].Quantity
}
}
if conds {
if out[id][vendorTag].Conditions == nil {
out[id][vendorTag].Conditions = map[string]float64{}
}
for i := range buylist[cardId] {
condTag := buylist[cardId][i].Conditions
out[id][vendorTag].Conditions[condTag+"_foil"] = buylist[cardId][i].BuyPrice
}
}
} else {
out[id][vendorTag].Regular = buylist[cardId][0].BuyPrice
if qty && !vendor.Info().MetadataOnly {
for i := range buylist[cardId] {
out[id][vendorTag].Qty += buylist[cardId][i].Quantity
}
}
if conds {
if out[id][vendorTag].Conditions == nil {
out[id][vendorTag].Conditions = map[string]float64{}
}
for i := range buylist[cardId] {
condTag := buylist[cardId][i].Conditions
out[id][vendorTag].Conditions[condTag] = buylist[cardId][i].BuyPrice
}
}
}
}
}
return out
}
func checkFinish(co *mtgmatcher.CardObject, finish string) bool {
switch finish {
case "nonfoil":
return co.Foil || co.Etched
case "foil":
return !co.Foil || co.Etched
case "etched":
return co.Foil || !co.Etched
}
return false
}
func BanPrice2CSV(w *csv.Writer, pm map[string]map[string]*BanPrice, shouldQty, shouldCond, shouldFullName bool) error {
var condKeys []string
header := []string{"UUID"}
if shouldFullName {
header = append(header, "TCG Product Id", "Card Name", "Edition", "Number", "Rarity")
}
header = append(header, "Store", "Regular Price", "Foil Price", "Etched Price")
if shouldQty {
header = append(header, "Regular Quantity", "Foil Quantity", "Etched Quantity")
}
if shouldCond {
condKeys = []string{
"NM", "SP", "MP", "HP", "PO",
"NM_foil", "SP_foil", "MP_foil", "HP_foil", "PO_foil",
"NM_etched", "SP_etched", "MP_etched", "HP_etched", "PO_etched",
}
header = append(header, condKeys...)
}
err := w.Write(header)
if err != nil {
return err
}
for id := range pm {
var cardName, edition, number, tcgId, rarity string
if shouldFullName {
co, err := mtgmatcher.GetUUID(id)
if err != nil {
co, err = mtgmatcher.GetUUID(mtgmatcher.Scryfall2UUID(id))
if err != nil {
continue
}
}
cardName = co.Name
edition = co.Edition
number = co.Number
rarity = co.Rarity
tcgId = co.Identifiers["tcgplayerProductId"]
if co.Etched {
tcgId = co.Identifiers["tcgplayerEtchedProductId"]
}
}
for scraper, entry := range pm[id] {
var regular, foil, etched string
var regularQty, foilQty, etchedQty string
if entry.Regular != 0 {
regular = fmt.Sprintf("%0.2f", entry.Regular)
if shouldQty && entry.Qty != 0 {
regularQty = fmt.Sprintf("%d", entry.Qty)
}
}
if entry.Foil != 0 {
foil = fmt.Sprintf("%0.2f", entry.Foil)
if shouldQty && entry.QtyFoil != 0 {
foilQty = fmt.Sprintf("%d", entry.QtyFoil)
}
}
if entry.Etched != 0 {
etched = fmt.Sprintf("%0.2f", entry.Etched)
if shouldQty && entry.QtyEtched != 0 {
etchedQty = fmt.Sprintf("%d", entry.QtyEtched)
}
}
record := []string{id}
if shouldFullName {
record = append(record, tcgId, cardName, edition, number, rarity)
}
record = append(record, scraper, regular, foil, etched)
if shouldQty {
record = append(record, regularQty, foilQty, etchedQty)
}
if shouldCond {
for _, tag := range condKeys {
var priceStr string
price := entry.Conditions[tag]
if price != 0 {
priceStr = fmt.Sprintf("%0.2f", price)
}
record = append(record, priceStr)
}
}
err = w.Write(record)
if err != nil {
return err
}
}
w.Flush()
}
return nil
}
func SimplePrice2CSV(w *csv.Writer, pm map[string]map[string]*BanPrice, uploadedDada []UploadEntry) error {
allScrapersMap := map[string]int{}
for id := range pm {
for scraper := range pm[id] {
allScrapersMap[scraper] = 0
}
}
allScrapers := make([]string, 0, len(allScrapersMap))
for scraper := range allScrapersMap {
allScrapers = append(allScrapers, scraper)
}
sort.Slice(allScrapers, func(i, j int) bool {
return allScrapers[i] < allScrapers[j]
})
header := []string{"UUID", "Card Name", "Set Code", "Number", "Finish"}
header = append(header, allScrapers...)
header = append(header, "Loaded Price", "Loaded Condition", "Loaded Quantity", "Notes")
err := w.Write(header)
if err != nil {
return err
}
for j := range uploadedDada {
if uploadedDada[j].MismatchError != nil {
continue
}
id := uploadedDada[j].CardId
_, found := pm[id]
if !found {
continue
}
var cardName, code, number string
co, err := mtgmatcher.GetUUID(id)
if err != nil {
continue
}
cardName = co.Name
code = co.SetCode
number = co.Number
prices := make([]string, len(allScrapers))
for i, scraper := range allScrapers {
entry, found := pm[id][scraper]
if !found {
continue
}
price := getPrice(entry, uploadedDada[j].OriginalCondition)
prices[i] = fmt.Sprintf("%0.2f", price)
}
ogPrice := ""
if uploadedDada[j].OriginalPrice != 0 {
ogPrice = fmt.Sprintf("%0.2f", uploadedDada[j].OriginalPrice)
}
prices = append(prices, ogPrice)
prices = append(prices, uploadedDada[j].OriginalCondition)
qty := ""
if uploadedDada[j].HasQuantity {
qty = fmt.Sprint(uploadedDada[j].Quantity)
}
prices = append(prices, qty)
prices = append(prices, uploadedDada[j].Notes)
record := []string{id, cardName, code, number}
if co.Etched {
record = append(record, "etched")
} else if co.Foil {
record = append(record, "foil")
} else {
record = append(record, "nonfoil")
}
record = append(record, prices...)
err = w.Write(record)
if err != nil {
return err
}
w.Flush()
}
return nil
}