Skip to content

Instantly share code, notes, and snippets.

@ericraio
Created January 3, 2021 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericraio/813bcff2a05626508a7c76ec6a5fe6f0 to your computer and use it in GitHub Desktop.
Save ericraio/813bcff2a05626508a7c76ec6a5fe6f0 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/ericraio/landlord/src/scryfall"
)
func main() {
var scryfallCards []scryfall.ScryfallCard
if len(os.Args) != 3 {
fmt.Printf("Expected 2 arguments, json path and output binary path but received %d - %+v", len(os.Args), os.Args)
os.Exit(0)
}
input_path := os.Args[1]
output_path := os.Args[2]
jsonFile, err := os.Open(input_path)
if err != nil {
fmt.Println(err)
}
defer func() {
err := jsonFile.Close()
if err != nil {
fmt.Println(err)
os.Exit(0)
}
}()
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
fmt.Println(err)
os.Exit(0)
}
err = json.Unmarshal(byteValue, &scryfallCards)
if err != nil {
fmt.Println(err)
os.Exit(0)
}
// Filter out any cards that are not legal in all formats
// This should filter out any tokens
scryfallCards = scryfall.FilterNotLegalCards(&scryfallCards)
// Flatten the card_faces out into scryfall_cards
// To do that, we clone and update the image_uris to that of the parent card
scryfallCards = scryfall.FlattenCardFaces(&scryfallCards)
fmt.Println("Generating landlord output")
buf, err := scryfall.Encode(&scryfallCards)
if err != nil {
panic(err)
}
var b bytes.Buffer
w := gzip.NewWriter(&b)
_, err = w.Write(buf.Bytes())
if err != nil {
panic(err)
}
err = w.Close() // You must close this first to flush the bytes to the buffer.
if err != nil {
panic(err)
}
fmt.Println("Writing all_cards.landlord")
err = ioutil.WriteFile(output_path+"/all_cards.landlord", b.Bytes(), 0666)
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment