Repository navigation
Expand file tree
/
Copy pathchart.go
More file actions
318 lines (306 loc) · 9.44 KB
/
Copy pathchart.go
File metadata and controls
318 lines (306 loc) · 9.44 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strings"
)
// Price charts: fetch a historical price series (cards → rarebox-price-history,
// crypto → CoinGecko, stocks → Yahoo Finance, all keyless) and render a
// self-contained INTERACTIVE HTML chart (hover for date+price) as an artifact.
// This is neutral data visualization — no trading, no advice.
type pricePoint struct {
Ms int64
Price float64
}
const priceHistBase = "https://raw.githubusercontent.com/novaoc/rarebox-price-history/main/data"
// fetchJSON GETs a JSON API with a browser UA (CoinGecko/Yahoo gate on UA),
// SSRF-guarded like every other outbound call.
func fetchJSON(u string, v any) error {
req, err := http.NewRequest("GET", u, nil)
if err != nil {
return err
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36")
req.Header.Set("Accept", "application/json")
resp, err := ssrfClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return fmt.Errorf("http %d", resp.StatusCode)
}
b, err := io.ReadAll(io.LimitReader(resp.Body, 8<<20))
if err != nil {
return err
}
return json.Unmarshal(b, v)
}
// lastDays trims an ascending-by-time series to the trailing window.
func lastDays(pts []pricePoint, days int) []pricePoint {
if days <= 0 || len(pts) < 2 {
return pts
}
cutoff := pts[len(pts)-1].Ms - int64(days)*86400000
var out []pricePoint
for _, p := range pts {
if p.Ms >= cutoff {
out = append(out, p)
}
}
if len(out) < 2 {
return pts
}
return out
}
// cardHistory pulls a card's [epochDay,usd] series (best-tracked variant). The
// history file name and the per-card key are game-specific.
func cardHistory(game, set, number string, days int) ([]pricePoint, string, error) {
game = strings.ToLower(strings.TrimSpace(game))
set = strings.TrimSpace(set)
fileSet := strings.ToLower(set)
key := rbNormNum(number)
if game == "one-piece" || game == "one-piece-ja" {
// files are OP-01.json; keys are OP01-003 (set sans dash + 3-digit num)
fileSet = strings.ToUpper(set)
n := rbNormNum(number)
for len(n) < 3 {
n = "0" + n
}
key = strings.ToUpper(strings.ReplaceAll(set, "-", "")) + "-" + n
}
var doc struct {
Cards map[string]map[string][][]float64 `json:"cards"`
}
if game == "riftbound" {
// history keys by TCGplayer productId — resolve it from the catalog by
// collector number, picking the printing that actually has history.
fileSet = strings.ToUpper(set)
if err := rbGet(priceHistBase+"/riftbound/"+fileSet+".json", &doc); err != nil {
return nil, "", fmt.Errorf("no price history for set %q", set)
}
var cat []rbCard
_ = rbGet(rbData+"/catalog/riftbound/sets/"+fileSet+".json", &cat)
n := rbNormNum(number)
bestLen := 0
for _, c := range cat {
if rbNormNum(c.Number) != n || c.TcgplayerID == "" {
continue
}
tot := 0
for _, s := range doc.Cards[c.TcgplayerID] {
tot += len(s)
}
if tot > bestLen {
bestLen, key = tot, c.TcgplayerID
}
}
if bestLen == 0 {
return nil, "", fmt.Errorf("no price history for riftbound #%s", number)
}
} else if err := rbGet(priceHistBase+"/"+game+"/"+fileSet+".json", &doc); err != nil {
return nil, "", fmt.Errorf("no price history for set %q", set)
}
variants := doc.Cards[key]
if len(variants) == 0 {
return nil, "", fmt.Errorf("no price history for #%s in %s", number, set)
}
var best [][]float64
var bestVar string
for name, s := range variants {
if len(s) > len(best) {
best, bestVar = s, name
}
}
var pts []pricePoint
for _, p := range best {
if len(p) >= 2 {
pts = append(pts, pricePoint{Ms: int64(p[0]) * 86400000, Price: p[1]})
}
}
return lastDays(pts, days), bestVar, nil
}
// cryptoHistory resolves a name/symbol to a CoinGecko id and pulls daily USD.
func cryptoHistory(query string, days int) ([]pricePoint, string, error) {
var sr struct {
Coins []struct{ ID, Name string }
}
if err := fetchJSON("https://api.coingecko.com/api/v3/search?query="+url.QueryEscape(query), &sr); err != nil {
return nil, "", err
}
if len(sr.Coins) == 0 {
return nil, "", fmt.Errorf("no token matching %q", query)
}
id, name := sr.Coins[0].ID, sr.Coins[0].Name
var d struct {
Prices [][]float64 `json:"prices"`
}
u := fmt.Sprintf("https://api.coingecko.com/api/v3/coins/%s/market_chart?vs_currency=usd&days=%d&interval=daily", url.PathEscape(id), days)
if err := fetchJSON(u, &d); err != nil {
return nil, "", err
}
var pts []pricePoint
for _, p := range d.Prices {
if len(p) >= 2 {
pts = append(pts, pricePoint{Ms: int64(p[0]), Price: p[1]})
}
}
return pts, name, nil
}
// stockHistory pulls daily closes from Yahoo Finance.
func stockHistory(symbol string, days int) ([]pricePoint, string, error) {
rng := "1mo"
switch {
case days > 186:
rng = "1y"
case days > 93:
rng = "6mo"
case days > 31:
rng = "3mo"
}
sym := strings.ToUpper(strings.TrimSpace(symbol))
u := "https://query1.finance.yahoo.com/v8/finance/chart/" + url.PathEscape(sym) + "?range=" + rng + "&interval=1d"
var y struct {
Chart struct {
Result []struct {
Timestamp []int64
Indicators struct {
Quote []struct{ Close []float64 }
}
}
}
}
if err := fetchJSON(u, &y); err != nil {
return nil, "", err
}
if len(y.Chart.Result) == 0 || len(y.Chart.Result[0].Indicators.Quote) == 0 {
return nil, "", fmt.Errorf("no data for %s", sym)
}
r := y.Chart.Result[0]
cl := r.Indicators.Quote[0].Close
var pts []pricePoint
for i, ts := range r.Timestamp {
if i < len(cl) && cl[i] > 0 {
pts = append(pts, pricePoint{Ms: ts * 1000, Price: cl[i]})
}
}
return lastDays(pts, days), sym, nil
}
var slugRe = regexp.MustCompile(`[^a-zA-Z0-9]+`)
func chartSlug(s string) string {
s = strings.Trim(slugRe.ReplaceAllString(strings.ToLower(s), "-"), "-")
if s == "" {
s = "chart"
}
if len(s) > 40 {
s = s[:40]
}
return s
}
// priceChart is the tool entry point.
func (tc *ToolCtx) priceChart(a toolArgs) string {
kind := strings.ToLower(strings.TrimSpace(a.Kind))
days := a.Days
var pts []pricePoint
var title, sub string
var err error
var fmtVal func(float64) string // nil = money
switch kind {
case "card":
// History for these games is keyed by collector number; others (one-piece
// by card id, riftbound by product id, yugioh by set code) don't align, so
// fail fast with a clear message instead of letting the model retry.
if a.Game == "" || a.Set == "" || a.Number == "" {
return "chart error: for a card I need game, set, and number — look them up with tcg first."
}
chartable := map[string]bool{"pokemon": true, "pokemon-ja": true, "mtg": true, "lorcana": true, "one-piece": true, "one-piece-ja": true, "riftbound": true}
if !chartable[strings.ToLower(strings.TrimSpace(a.Game))] {
return "chart error: price charts aren't available for " + a.Game + " cards yet (its history isn't keyed by collector number). Give the current price from tcg instead — don't retry the chart."
}
if days == 0 {
days = 90
}
var variant string
pts, variant, err = cardHistory(a.Game, a.Set, a.Number, days)
title = strings.TrimSpace(a.Query)
if title == "" {
title = fmt.Sprintf("%s %s #%s", a.Game, strings.ToUpper(a.Set), a.Number)
}
sub = fmt.Sprintf("%s %s #%s · %s", a.Game, strings.ToUpper(a.Set), a.Number, variant)
case "index":
g := strings.ToLower(strings.TrimSpace(a.Game))
if g == "one-piece-ja" {
g = "one-piece" // no separate JP catalog — alias to the shared one
}
if !tcgGames[g] {
return "chart error: index needs a game — pokemon|pokemon-ja|mtg|yugioh|lorcana|one-piece|one-piece-ja|riftbound."
}
if days == 0 {
days = 90
}
if days < 14 {
days = 14
} else if days > 365 {
days = 365
}
var nSets, nCards int
var dated bool
pts, nSets, nCards, dated, err = marketIndex(g, days)
title = gameDisplay[g] + " market index"
basket := fmt.Sprintf("newest %d sets", nSets)
if !dated { // undated catalog → the whole catalog is the basket
basket = fmt.Sprintf("%d tracked sets", nSets)
}
sub = fmt.Sprintf("rarebox · %s · %d cards ≥ $1 · equal-weight · base 100 at window start", basket, nCards)
fmtVal = func(v float64) string { return fmt.Sprintf("%.1f", v) }
case "crypto":
if a.Query == "" {
return "chart error: crypto needs a name or symbol (e.g. bitcoin)."
}
if days == 0 {
days = 30
}
pts, title, err = cryptoHistory(a.Query, days)
sub = "CoinGecko · USD"
case "stock":
sym := a.Symbol
if sym == "" {
sym = a.Query
}
if sym == "" {
return "chart error: stock needs a ticker (e.g. AAPL)."
}
if days == 0 {
days = 30
}
pts, title, err = stockHistory(sym, days)
sub = "Yahoo Finance · USD"
default:
return "chart error: kind must be card, stock, crypto, or index."
}
if err != nil {
return "chart error: " + err.Error()
}
if len(pts) < 2 {
return "chart error: not enough price history to chart that yet."
}
if out := tc.saveArtifact(chartSlug(title)+".png", string(renderChartPNG(title, sub, pts, fmtVal))); strings.HasPrefix(out, "artifact error") {
return out
}
fv := fmtVal
if fv == nil {
fv = chartMoney
}
first, last := pts[0].Price, pts[len(pts)-1].Price
chg := 0.0
if first != 0 {
chg = (last - first) / first * 100
}
span := (pts[len(pts)-1].Ms - pts[0].Ms) / 86400000
return fmt.Sprintf("Charted %s — %d points over ~%dd, %s → %s (%+.1f%%). Chart image is attached to your reply.",
title, len(pts), span, fv(first), fv(last), chg)
}