Show the card's ID when printing missing cards

This commit is contained in:
Corentin Barreau 2023-04-24 15:54:26 +02:00 committed by Florian Baumann
parent 40b08d9846
commit 03a15d998e
5 changed files with 36 additions and 21 deletions

View File

@ -112,9 +112,8 @@ func addCards(cards []string, unique bool, count int64) error {
// If card is not already in collection, fetching from scyfall
} else {
// Fetch card from scryfall
c, err := fetch_card(card)
c, err := fetchCard(card)
if err != nil {
LogMessage(fmt.Sprintf("%v", err), "red")
continue

View File

@ -40,13 +40,11 @@ otherwise you'll get a list of cards as a search result.`,
}
func ShowCard(cardids []string) {
client := storage_connect()
coll := &Collection{client.Database("serra").Collection("cards")}
defer storage_disconnect(client)
for _, v := range cardids {
if len(strings.Split(v, "/")) < 2 || strings.Split(v, "/")[1] == "" {
LogMessage(fmt.Sprintf("Invalid card %s", v), "red")
continue
@ -61,7 +59,6 @@ func ShowCard(cardids []string) {
}
func Cards(rarity, set, sortby, name, oracle, cardType string) []Card {
client := storage_connect()
coll := &Collection{client.Database("serra").Collection("cards")}
defer storage_disconnect(client)

View File

@ -2,6 +2,7 @@ package serra
import (
"fmt"
"sort"
"strconv"
"github.com/spf13/cobra"
@ -20,7 +21,6 @@ var missingCmd = &cobra.Command{
cards you dont own (yet) :)`,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, setname []string) error {
client := storage_connect()
coll := &Collection{client.Database("serra").Collection("cards")}
defer storage_disconnect(client)
@ -53,13 +53,29 @@ cards you dont own (yet) :)`,
}
misses := missing(in_collection, complete_set)
// Fetch all missing cards
missingCards := []*Card{}
for _, m := range misses {
ncard, err := fetch_card(fmt.Sprintf("%s/%s", setname[0], m))
card, err := fetchCard(fmt.Sprintf("%s/%s", setname[0], m))
if err != nil {
continue
}
fmt.Printf("%.02f %s\t%s (%s)\n", ncard.getValue(false), getCurrency(), ncard.Name, ncard.SetName)
missingCards = append(missingCards, card)
}
// Sort the missing cards by ID
sort.Slice(missingCards, func(i, j int) bool {
id1, _ := strconv.Atoi(missingCards[i].CollectorNumber)
id2, _ := strconv.Atoi(missingCards[j].CollectorNumber)
return id1 < id2
})
for _, card := range missingCards {
fmt.Printf("[%s] %.02f %s\t%s (%s)\n", card.CollectorNumber, card.getValue(false), getCurrency(), card.Name, card.SetName)
}
return nil
},
}

View File

@ -3,7 +3,6 @@ package serra
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
@ -28,7 +27,7 @@ type Card struct {
Booster bool `json:"booster"`
BorderColor string `json:"border_color"`
CardBackID string `json:"card_back_id"`
Cmc int64 `json:"cmc"`
Cmc float64 `json:"cmc"`
CollectorNumber string `json:"collector_number"`
ColorIdentity []string `json:"color_identity"`
Colors []string `json:"colors"`
@ -170,11 +169,9 @@ type Set struct {
URI string `json:"uri"`
}
func fetch_card(path string) (*Card, error) {
func fetchCard(path string) (*Card, error) {
if !strings.Contains(path, "/") {
err := errors.New(fmt.Sprintf("Card must follow format <set>/<number>, for example: ath/15"))
return &Card{}, err
return &Card{}, fmt.Errorf("Card must follow format <set>/<number>, for example: ath/15")
}
// TODO better URL Building...
@ -185,21 +182,24 @@ func fetch_card(path string) (*Card, error) {
}
if resp.StatusCode != 200 {
err := errors.New(fmt.Sprintf("Error: %s not found", path))
return &Card{}, err
return &Card{}, fmt.Errorf("Card %s not found", path)
}
//We Read the response body on the line below.
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
log.Fatalf("%s", err)
return &Card{}, err
}
r := bytes.NewReader(body)
decoder := json.NewDecoder(r)
val := &Card{}
err = decoder.Decode(val)
if err != nil {
log.Fatalf("%s", err)
}
// Set created Time
val.SerraCreated = primitive.NewDateTimeFromTime(time.Now())
@ -213,15 +213,14 @@ func fetch_card(path string) (*Card, error) {
func fetch_sets() (*SetList, error) {
// TODO better URL Building...
resp, err := http.Get(fmt.Sprintf("https://api.scryfall.com/sets"))
resp, err := http.Get("https://api.scryfall.com/sets")
if err != nil {
log.Fatalln(err)
return &SetList{}, err
}
if resp.StatusCode != 200 {
err := errors.New(fmt.Sprintf("Error: /sets not found"))
return &SetList{}, err
return &SetList{}, fmt.Errorf("/sets not found")
}
//We Read the response body on the line below.
@ -234,7 +233,11 @@ func fetch_sets() (*SetList, error) {
r := bytes.NewReader(body)
decoder := json.NewDecoder(r)
val := &SetList{}
err = decoder.Decode(val)
if err != nil {
log.Fatalln(err)
}
return val, nil
}

View File

@ -79,7 +79,7 @@ var updateCmd = &cobra.Command{
)
for _, card := range cards {
bar.Add(1)
updated_card, err := fetch_card(fmt.Sprintf("%s/%s", card.Set, card.CollectorNumber))
updated_card, err := fetchCard(fmt.Sprintf("%s/%s", card.Set, card.CollectorNumber))
if err != nil {
LogMessage(fmt.Sprintf("%v", err), "red")
continue