Implement importing Delver Lens exports

This commit is contained in:
The Magician 2024-09-23 22:03:31 +01:00
parent 95f45a98fa
commit 70f0b4d4f7
3 changed files with 83 additions and 3 deletions

View File

@ -6,8 +6,8 @@ import (
"fmt" "fmt"
"os/exec" "os/exec"
"sevenkeys/database" "sevenkeys/database"
"sevenkeys/delverlens"
"sevenkeys/logic" "sevenkeys/logic"
"sevenkeys/manabox"
) )
var output string var output string
@ -96,9 +96,14 @@ func MainCliLoop(db *sql.DB) {
logic.Check(err) logic.Check(err)
copiesInserted++ copiesInserted++
break break
case "m", "manabox": case "d", "delverlens":
filename := GetStringResponse("Filename:") filename := GetStringResponse("Filename:")
manabox.ParseManaboxExport(filename)
cards, err := delverlens.ParseExportFile(filename)
logic.Check(err)
err = logic.ImportDelverLensCards(cards)
logic.Check(err)
break break
case "l", "locate": case "l", "locate":
filename := GetStringResponse("Filename:") filename := GetStringResponse("Filename:")

View File

@ -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
}

25
sevenkeys/logic/import.go Normal file
View File

@ -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)
}
}