-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_moxfield.go
154 lines (128 loc) · 3.49 KB
/
api_moxfield.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"sort"
"strings"
cleanhttp "github.com/hashicorp/go-cleanhttp"
"github.com/mtgban/go-mtgban/mtgmatcher"
)
type MoxfieldDeck struct {
Boards map[string]MoxBoard `json:"boards"`
}
type MoxBoard struct {
Count int `json:"count"`
Cards map[string]MoxCard `json:"cards"`
}
type MoxCard struct {
Quantity int `json:"quantity"`
Finish string `json:"finish"`
Card struct {
ScryfallID string `json:"scryfall_id"`
} `json:"card"`
}
func extractDeckID(gdocURL string) (string, error) {
// Parse the provided URL.
parsedURL, err := url.Parse(gdocURL)
if err != nil {
return "", err
}
// Extract the deck ID from the URL.
pathSegments := strings.Split(parsedURL.Path, "/")
if len(pathSegments) < 2 || pathSegments[1] != "decks" {
return "", errors.New("invalid Moxfield deck URL")
}
deckID := pathSegments[2]
if deckID == "" {
return "", errors.New("no deck ID found in URL")
}
return fmt.Sprintf("%s/%s", Config.Uploader.Moxfield, deckID), nil
}
func getMoxDeck(url string) (*MoxfieldDeck, error) {
// Create request to fetch Moxfield deck data.
client := cleanhttp.DefaultClient()
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer undefined")
req.Header.Set("User-Agent", "curl/8.4.0")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Check response status code.
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status code: %d", resp.StatusCode)
}
// Read response body into slice.
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Parse deck data from response body.
var data MoxfieldDeck
err = json.Unmarshal(body, &data)
if err != nil {
return nil, err
}
// Return deck data.
return &data, nil
}
// Extract card details, flattening deck data into a single slice
func extractDecklist(data *MoxfieldDeck) []MoxCard {
var cardInfoList []MoxCard
for _, board := range data.Boards {
for _, card := range board.Cards {
cardInfoList = append(cardInfoList, card)
}
}
return cardInfoList
}
// Prepare upload entries in the expected format
func prepareUploadEntries(MoxCards []MoxCard, maxRows int) ([]UploadEntry, error) {
var uploadEntries []UploadEntry
for i, detail := range MoxCards {
if i >= maxRows {
break
}
entry := UploadEntry{
HasQuantity: true,
Quantity: detail.Quantity,
}
isfoil := detail.Finish == "foil"
isetched := detail.Finish == "etched"
cardId, err := mtgmatcher.MatchId(detail.Card.ScryfallID, isfoil, isetched)
entry.CardId = cardId
entry.MismatchError = err
uploadEntries = append(uploadEntries, entry)
}
return uploadEntries, nil
}
func loadMoxfieldDeck(gdocURL string, maxRows int) ([]UploadEntry, error) {
moxURL, err := extractDeckID(gdocURL)
if err != nil {
log.Println(err)
return nil, errors.New("invalid Moxfield deck URL")
}
log.Println("Querying:", moxURL)
moxDeck, err := getMoxDeck(moxURL)
if err != nil {
log.Println(err)
return nil, errors.New("failed to fetch Moxfield deck")
}
moxCards := extractDecklist(moxDeck)
// Preserve some sort of ordering
sort.Slice(moxCards, func(i, j int) bool {
uuidI := mtgmatcher.Scryfall2UUID(moxCards[i].Card.ScryfallID)
uuidJ := mtgmatcher.Scryfall2UUID(moxCards[j].Card.ScryfallID)
return !sortSetsAlphabeticalSet(uuidI, uuidJ)
})
return prepareUploadEntries(moxCards, maxRows)
}