-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
278 lines (240 loc) · 6.06 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
import (
"encoding/csv"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
)
// Minimum difference between market and buylist price
const Tolerance = 0.75
// Minimum buylist price to consider
const Threshold = 0.1
// Maximum number of parallel requests
const MaxConcurrency = 8
const ConfigFile = "cfg.json"
var Config struct {
ApiKey string `json:"api_key"`
UserName string `json:"user_name"`
}
type result struct {
err error
cardName string
cardSet string
price float64
buylistPrice float64
isFoil bool
url string
}
func processEntry(record []string) (ret result) {
var err error
cardName := strings.TrimSpace(record[1])
cardSet := strings.TrimSpace(record[2])
isFoil := strings.TrimSpace(record[5]) != ""
buylistPrice, _ := strconv.ParseFloat(record[7], 64)
if strings.HasPrefix(record[7], "$") {
buylistPrice, _ = strconv.ParseFloat(record[7][1:], 64)
}
// skip small BL under this
if buylistPrice < Threshold {
return
}
// convert the CK name and set to CS versions
cardName, cardSet, err = processRecord(cardName, cardSet)
if err != nil {
ret.err = fmt.Errorf("Error parsing %q - %q\n", record, err)
return
} else if cardName == "" || cardSet == "" {
return
}
u, err := url.Parse(fmt.Sprintf("http://www.cardshark.com/API/%s/Get-Price.aspx", Config.UserName))
if err != nil {
ret.err = err
return
}
q := u.Query()
q.Set("apiKey", Config.ApiKey)
q.Set("CardName", cardName)
q.Set("CardSet", cardSet)
u.RawQuery = q.Encode()
resp, err := http.Get(u.String())
if err != nil {
ret.err = fmt.Errorf("Error retrieving %q - %q\n", record, err)
return
}
data, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
ret.err = fmt.Errorf("Error reading %q - %q\n", record, err)
return
}
if resp.StatusCode != 200 {
ret.err = fmt.Errorf("Error requesting %q - %q\n", record, string(data))
return
}
var response struct {
Status string `xml:"status"`
Price string `xml:"price"`
FoilPrice string `xml:"foilprice"`
Url string `xml:"url"`
}
err = xml.Unmarshal(data, &response)
if err != nil {
ret.err = fmt.Errorf("Error decoding %q - %q\n", record, err)
return
}
// check for missing prerelease cards and wrong foil prices
isPrerelease := cardSet == "Prerelease Stamped"
if response.Status != "valid card" {
isConspiracy := cardSet == "Conspiracy Take the Crown"
isSunCe := cardName == "Sun Ce, Young Conquerer"
// skip errors for missing prerelease cards, CSP2-only
// conspiracies, and a single p3k card
if !isPrerelease && !isConspiracy && !isSunCe {
ret.err = fmt.Errorf("Invalid record: (%s/%s) %q\n", cardName, cardSet, record)
}
return
}
// handle foil pricing differently for promos
isPromo := strings.HasPrefix(cardSet, "Promotional")
// this roundabout way is because some extremenly high prices have a ","
// which prevents correct unmarshaling
marketPrice, _ := strconv.ParseFloat(strings.Replace(response.Price, ",", "", -1), 64)
foilPrice, _ := strconv.ParseFloat(strings.Replace(response.FoilPrice, ",", "", -1), 64)
price := marketPrice
if isFoil {
price = foilPrice
}
// can't trust the promos, pick the lowest among the two prices
if isPromo || isPrerelease {
price = foilPrice
if price-foilPrice > 0 {
price = marketPrice
}
}
ret.cardName = cardName
ret.cardSet = cardSet
ret.price = price
ret.buylistPrice = buylistPrice
ret.isFoil = isFoil
ret.url = response.Url
return
}
func run() int {
l := log.New(os.Stderr, "", 0)
if len(os.Args) < 2 {
log.Fatal(fmt.Errorf("usage: <exe> <csv>"))
}
data, err := ioutil.ReadFile(ConfigFile)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(data, &Config)
if err != nil {
log.Fatal(err)
}
file, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
r := csv.NewReader(file)
first, err := r.Read()
if err == io.EOF {
log.Fatal("Empty input file")
}
if err != nil {
log.Fatal("Error reading record: " + err.Error())
}
if len(first) < 8 || (first[1] != "Card Name" &&
first[2] != "CK_Modif_Set" && first[5] != "NF/F" &&
first[7] != "BL_Value") {
log.Fatal("Malformed input file")
}
w := csv.NewWriter(os.Stdout)
defer w.Flush()
entries := 0
records := make(chan []string)
results := make(chan result)
var wg sync.WaitGroup
// Read from the records channel and block the subroutine until done
// In this way you process entry up to MaxConcurrency at the same time
for i := 0; i < MaxConcurrency; i++ {
wg.Add(1)
go func() {
for record := range records {
results <- processEntry(record)
}
wg.Done()
}()
}
// Read from input file and queue records to be processed
// Close channels and wait group when done
// In case of error, wait for any remaining background routines
go func() {
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
l.Printf("Error reading record: %s", err.Error())
break
}
records <- record
}
close(records)
wg.Wait()
close(results)
}()
// Read from the result and apply any further logic
for result := range results {
if result.err != nil {
l.Println(result.err)
continue
}
if result.price > 0 && result.price <= Tolerance*result.buylistPrice {
if entries == 0 {
header := []string{
"URL", "Name", "Set", "Foil", "Buylist Price", "CS Price", "Arb", "Spread",
}
w.Write(header)
}
foil := ""
if result.isFoil {
foil = "X"
}
buylistPriceStr := fmt.Sprintf("%0.2f", result.buylistPrice)
priceStr := fmt.Sprintf("%0.2f", result.price)
diff := fmt.Sprintf("%0.2f", result.buylistPrice-result.price)
spread := fmt.Sprintf("%0.2f%%", 100*(result.buylistPrice-result.price)/result.price)
record := []string{
result.url,
result.cardName,
result.cardSet,
foil,
buylistPriceStr,
priceStr,
diff,
spread,
}
err := w.Write(record)
if err != nil {
log.Fatalln("Error writing record to csv: ", err)
}
w.Flush()
entries++
}
}
return 0
}
func main() {
os.Exit(run())
}