From 70f0b4d4f7b5fe428d0ac75821b48b4199b6a17d Mon Sep 17 00:00:00 2001 From: The Magician Date: Mon, 23 Sep 2024 22:03:31 +0100 Subject: [PATCH] Implement importing Delver Lens exports --- sevenkeys/cli/mainui.go | 11 +++++-- sevenkeys/delverlens/delverlens.go | 50 ++++++++++++++++++++++++++++++ sevenkeys/logic/import.go | 25 +++++++++++++++ 3 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 sevenkeys/delverlens/delverlens.go create mode 100644 sevenkeys/logic/import.go diff --git a/sevenkeys/cli/mainui.go b/sevenkeys/cli/mainui.go index 7a9d743..cd49829 100644 --- a/sevenkeys/cli/mainui.go +++ b/sevenkeys/cli/mainui.go @@ -6,8 +6,8 @@ import ( "fmt" "os/exec" "sevenkeys/database" + "sevenkeys/delverlens" "sevenkeys/logic" - "sevenkeys/manabox" ) var output string @@ -96,9 +96,14 @@ func MainCliLoop(db *sql.DB) { logic.Check(err) copiesInserted++ break - case "m", "manabox": + case "d", "delverlens": filename := GetStringResponse("Filename:") - manabox.ParseManaboxExport(filename) + + cards, err := delverlens.ParseExportFile(filename) + logic.Check(err) + + err = logic.ImportDelverLensCards(cards) + logic.Check(err) break case "l", "locate": filename := GetStringResponse("Filename:") diff --git a/sevenkeys/delverlens/delverlens.go b/sevenkeys/delverlens/delverlens.go new file mode 100644 index 0000000..84616d6 --- /dev/null +++ b/sevenkeys/delverlens/delverlens.go @@ -0,0 +1,50 @@ +package delverlens + +import ( + "encoding/csv" + "io" + "os" + "strings" +) + +type DelverLensCard struct { + ScryfallID string + IsFoil bool +} + +func ParseExportFile(filename string) ([]DelverLensCard, error) { + var cards []DelverLensCard + + file, err := os.Open("/home/viciouscirce/dox/sevenkeys_imports/" + filename) + if err != nil { + return cards, err + } + + r := csv.NewReader(file) + + var isHeader bool = true + for { + record, err := r.Read() + if err == io.EOF { + break + } else if err != nil { + return cards, err + } + + // Skip the header line + if isHeader { + isHeader = false + continue + } + + card := DelverLensCard{ + Name: record[0], + IsFoil: record[1] == "Foil", + CollectorNumber: record[2], + SetCode: strings.ToLower(record[3]), + } + cards = append(cards, card) + } + + return cards, nil +} diff --git a/sevenkeys/logic/import.go b/sevenkeys/logic/import.go new file mode 100644 index 0000000..146e67c --- /dev/null +++ b/sevenkeys/logic/import.go @@ -0,0 +1,25 @@ +package logic + +import ( + "database/sql" + "sevenkeys/database" + "sevenkeys/delverlens" +) + +func ImportDelverLensCards(db *sql.DB, cards []delverlens.DelverLensCard, storageAreaId int) error { + for _, card := range cards { + var cardPrintingId string + if card.IsFoil { + cardPrintingId = card.ScryfallID + "n" + } else { + cardPrintingId = card.ScryfallID + "f" + } + + cardLocation := database.CardLocation{ + CardPrintingId: cardPrintingId, + StorageAreaId: storageAreaId, + } + + StoreCard(cardLocation) + } +}