Show the card's ID when printing missing cards

This commit is contained in:
Corentin Barreau 2023-04-24 15:54:26 +02:00
parent 96731b8fe0
commit 75838da8f2
No known key found for this signature in database
GPG Key ID: 924DCF0EF04D31EF
5 changed files with 36 additions and 21 deletions

View File

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

View File

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

View File

@ -2,6 +2,7 @@ package serra
import ( import (
"fmt" "fmt"
"sort"
"strconv" "strconv"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -20,7 +21,6 @@ var missingCmd = &cobra.Command{
cards you dont own (yet) :)`, cards you dont own (yet) :)`,
SilenceErrors: true, SilenceErrors: true,
RunE: func(cmd *cobra.Command, setname []string) error { RunE: func(cmd *cobra.Command, setname []string) error {
client := storage_connect() client := storage_connect()
coll := &Collection{client.Database("serra").Collection("cards")} coll := &Collection{client.Database("serra").Collection("cards")}
defer storage_disconnect(client) defer storage_disconnect(client)
@ -53,13 +53,29 @@ cards you dont own (yet) :)`,
} }
misses := missing(in_collection, complete_set) misses := missing(in_collection, complete_set)
// Fetch all missing cards
missingCards := []*Card{}
for _, m := range misses { 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 { if err != nil {
continue 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 return nil
}, },
} }

View File

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

View File

@ -79,7 +79,7 @@ var updateCmd = &cobra.Command{
) )
for _, card := range cards { for _, card := range cards {
bar.Add(1) 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 { if err != nil {
LogMessage(fmt.Sprintf("%v", err), "red") LogMessage(fmt.Sprintf("%v", err), "red")
continue continue