Skip to content

Commit

Permalink
mtgmatcher: Factor out common code into BuildSealedProductMap()
Browse files Browse the repository at this point in the history
MKM and CT share a similar structure, this allows MKM to correctly export
additional SLD products.
  • Loading branch information
kodawah committed May 30, 2024
1 parent 291f6a1 commit 70b43e1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 48 deletions.
27 changes: 10 additions & 17 deletions cardmarket/sealed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cardmarket
import (
"fmt"
"net/url"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -72,7 +71,7 @@ var notSealedComments = []string{
"without",
}

func (mkm *CardMarketSealed) processProduct(channel chan<- responseChan, idProduct int, uuid string) error {
func (mkm *CardMarketSealed) processProduct(channel chan<- responseChan, idProduct int, uuids []string) error {
u, err := url.Parse("https://www.cardmarket.com/en/Magic/Products/Search")
if err != nil {
return err
Expand Down Expand Up @@ -102,6 +101,11 @@ func (mkm *CardMarketSealed) processProduct(channel chan<- responseChan, idProdu
continue
}

uuid := uuids[0]
if article.IsFoil && len(uuids) > 1 {
uuid = uuids[1]
}

// Skip all the silly non-really-sealed listings
skip := false
for _, comment := range notSealedComments {
Expand Down Expand Up @@ -148,18 +152,7 @@ func (mkm *CardMarketSealed) processProduct(channel chan<- responseChan, idProdu
}

func (mkm *CardMarketSealed) scrape() error {
productMap := map[int]string{}
for _, uuid := range mtgmatcher.GetSealedUUIDs() {
co, err := mtgmatcher.GetUUID(uuid)
if err != nil || !co.Sealed || co.Identifiers["mcmId"] == "" {
continue
}
mcmId, err := strconv.Atoi(co.Identifiers["mcmId"])
if err != nil {
continue
}
productMap[mcmId] = uuid
}
productMap := mtgmatcher.BuildSealedProductMap("mcmId")
mkm.printf("Loaded %d sealed products", len(productMap))

productList, err := mkm.client.MKMProductList()
Expand All @@ -186,11 +179,11 @@ func (mkm *CardMarketSealed) scrape() error {
wg.Add(1)
go func() {
for idProduct := range products {
uuid := productMap[idProduct]
co, _ := mtgmatcher.GetUUID(uuid)
uuids := productMap[idProduct]
co, _ := mtgmatcher.GetUUID(uuids[0])
mkm.printf("Processing %s (%d/%d)...", co, slices.Index(productIds, idProduct)+1, len(productIds))

err = mkm.processProduct(channel, idProduct, uuid)
err = mkm.processProduct(channel, idProduct, uuids)
if err != nil {
mkm.printf("%s (%d) %s", co, idProduct, err.Error())
continue
Expand Down
32 changes: 1 addition & 31 deletions cardtrader/sealed.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cardtrader

import (
"fmt"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -115,36 +114,7 @@ func (ct *CardtraderSealed) processEntry(channel chan<- resultChan, expansionId
}

func (ct *CardtraderSealed) scrape() error {
productMap := map[int][]string{}
for _, uuid := range mtgmatcher.GetSealedUUIDs() {
co, err := mtgmatcher.GetUUID(uuid)
if err != nil || !co.Sealed {
continue
}
ctId := co.Identifiers["cardtraderId"]

// Some products do not carry an id because they are already assigned
// For specific cases, look for them since we have the canonical number
if ctId == "" && co.SetCode == "SLD" && strings.HasSuffix(co.Name, " Foil") {
uuids, err := mtgmatcher.SearchSealedEquals(strings.TrimSuffix(co.Name, " Foil"))
if err != nil {
continue
}
subco, err := mtgmatcher.GetUUID(uuids[0])
if err != nil {
continue
}
ctId = subco.Identifiers["cardtraderId"]
}
cardtraderId, err := strconv.Atoi(ctId)
if err != nil {
continue
}
// We also know that nonfoil comes before foil since product names are sorted
// so we can guarantee that the first element is nonfoil, and the second one
// is actually foil
productMap[cardtraderId] = append(productMap[cardtraderId], uuid)
}
productMap := mtgmatcher.BuildSealedProductMap("cardtraderId")
ct.printf("Loaded %d sealed products", len(productMap))

expansionsRaw, err := ct.client.Expansions()
Expand Down
39 changes: 39 additions & 0 deletions mtgmatcher/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"math/rand"
"regexp"
"strconv"
"strings"

"github.com/mroth/weightedrand/v2"
Expand Down Expand Up @@ -943,3 +944,41 @@ func GetProbabilitiesForSealed(setCode, sealedUUID string) ([]ProductProbabiliti

return probs, nil
}

// Provide a map of ids with a slice of uuids
// For most cases the slice will be of size one, but some ids may hold
// a second uuid representing the foil version of the product
func BuildSealedProductMap(idName string) map[int][]string {
productMap := map[int][]string{}
for _, uuid := range backend.AllSealedUUIDs {
co, err := GetUUID(uuid)
if err != nil {
continue
}
id := co.Identifiers[idName]

// Some products do not carry an id because they are already assigned
// For specific cases, look for them since we have the canonical number
if id == "" && co.SetCode == "SLD" && strings.HasSuffix(co.Name, " Foil") {
uuids, err := SearchSealedEquals(strings.TrimSuffix(co.Name, " Foil"))
if err != nil {
continue
}
subco, found := backend.UUIDs[uuids[0]]
if !found {
continue
}
id = subco.Identifiers[idName]
}

idNum, err := strconv.Atoi(id)
if err != nil {
continue
}
// We also know that nonfoil comes before foil since product names are sorted
// so we can guarantee that the first element is nonfoil, and the second one
// is actually foil
productMap[idNum] = append(productMap[idNum], uuid)
}
return productMap
}

0 comments on commit 70b43e1

Please sign in to comment.