98 lines
2.1 KiB
Go
98 lines
2.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
type PriceRecord struct {
|
||
|
MTGO map[string]PriceList `json:"mtgo,omitempty"`
|
||
|
Paper map[string]PriceList `json:"paper,omitempty"`
|
||
|
}
|
||
|
|
||
|
type PricePoints struct {
|
||
|
Normal map[string]float64 `json:"normal,omitempty"`
|
||
|
Foil map[string]float64 `json:"foil,omitempty"`
|
||
|
}
|
||
|
|
||
|
type PriceList struct {
|
||
|
Retail *PricePoints `json:"retail,omitempty"`
|
||
|
Currency string `json:"currency"`
|
||
|
Buylist *PricePoints `json:"buylist,omitempty"`
|
||
|
}
|
||
|
|
||
|
type PriceDB struct {
|
||
|
Data map[string]PriceRecord `json:"data"`
|
||
|
}
|
||
|
|
||
|
type CardPriceEntry struct {
|
||
|
UUID string `json:"uuid"`
|
||
|
Price float64 `json:"price"`
|
||
|
Currency string `json:"currency"`
|
||
|
Seller string `json:"seller"`
|
||
|
}
|
||
|
|
||
|
func loadPrices(pricesFile string, priceFilter float64) func() ([]CardPriceEntry, error) {
|
||
|
return func() ([]CardPriceEntry, error) {
|
||
|
// Check if prices file exists
|
||
|
_, err := os.Stat(pricesFile)
|
||
|
if os.IsNotExist(err) {
|
||
|
// Download prices file
|
||
|
err = downloadFile(pricesURL, pricesFile)
|
||
|
}
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// Read prices file
|
||
|
var priceDB PriceDB
|
||
|
err = readJSONFile(pricesFile, &priceDB)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// Filter cards we care about
|
||
|
entries := make([]CardPriceEntry, 0)
|
||
|
for uuid, priceCatalog := range priceDB.Data {
|
||
|
// Get all paper retail prices
|
||
|
lowestPrice, currency, seller := getLowestPrice(priceCatalog.Paper)
|
||
|
|
||
|
if priceFilter < lowestPrice {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
entries = append(entries, CardPriceEntry{
|
||
|
UUID: uuid,
|
||
|
Price: lowestPrice,
|
||
|
Currency: currency,
|
||
|
Seller: seller,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return entries, nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func getLowestPrice(sellers map[string]PriceList) (lowestPrice float64, currency string, seller string) {
|
||
|
lowestPrice = 9999
|
||
|
for cardSeller, priceList := range sellers {
|
||
|
if priceList.Retail == nil {
|
||
|
continue
|
||
|
}
|
||
|
for _, price := range priceList.Retail.Normal {
|
||
|
if price < lowestPrice {
|
||
|
lowestPrice = price
|
||
|
currency = priceList.Currency
|
||
|
seller = cardSeller
|
||
|
}
|
||
|
}
|
||
|
for _, price := range priceList.Retail.Foil {
|
||
|
if price < lowestPrice {
|
||
|
lowestPrice = price
|
||
|
currency = priceList.Currency
|
||
|
seller = cardSeller
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return
|
||
|
}
|