Item price support
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/runescape-wiki/rsw/internal/prices"
|
||||
"github.com/runescape-wiki/rsw/internal/render"
|
||||
"github.com/runescape-wiki/rsw/internal/wiki"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -25,8 +26,13 @@ Examples:
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
name := args[0]
|
||||
|
||||
if Game() == "rs3" {
|
||||
return runRS3Price(name)
|
||||
}
|
||||
|
||||
priceClient := prices.NewClient(GamePriceBaseURL())
|
||||
mc := prices.NewMappingCache(priceClient)
|
||||
mc := prices.NewMappingCache(priceClient, Game())
|
||||
|
||||
if err := mc.Load(); err != nil {
|
||||
return fmt.Errorf("failed to load item mapping: %w", err)
|
||||
@@ -104,17 +110,16 @@ Examples:
|
||||
}
|
||||
|
||||
hourly, err := priceClient.Get1Hour(item.ID)
|
||||
if err == nil && len(hourly.Data) > 0 {
|
||||
recent := hourly.Data[len(hourly.Data)-1]
|
||||
if err == nil {
|
||||
md.H2("Recent Activity (1h)")
|
||||
if recent.AvgHighPrice != nil {
|
||||
md.KV("Avg buy price", render.FormatGP(*recent.AvgHighPrice))
|
||||
if hourly.AvgHighPrice != nil {
|
||||
md.KV("Avg buy price", render.FormatGP(*hourly.AvgHighPrice))
|
||||
}
|
||||
md.KV("Buy volume", render.FormatNumber(recent.HighVolume))
|
||||
if recent.AvgLowPrice != nil {
|
||||
md.KV("Avg sell price", render.FormatGP(*recent.AvgLowPrice))
|
||||
md.KV("Buy volume", render.FormatNumber(hourly.HighVolume))
|
||||
if hourly.AvgLowPrice != nil {
|
||||
md.KV("Avg sell price", render.FormatGP(*hourly.AvgLowPrice))
|
||||
}
|
||||
md.KV("Sell volume", render.FormatNumber(recent.LowVolume))
|
||||
md.KV("Sell volume", render.FormatNumber(hourly.LowVolume))
|
||||
md.Newline()
|
||||
}
|
||||
}
|
||||
@@ -125,6 +130,58 @@ Examples:
|
||||
}
|
||||
}
|
||||
|
||||
func runRS3Price(name string) error {
|
||||
wikiClient := wiki.NewClient(GameBaseURL())
|
||||
|
||||
results, err := wikiClient.Search(name, 5)
|
||||
if err != nil {
|
||||
return fmt.Errorf("search failed: %w", err)
|
||||
}
|
||||
if len(results) == 0 {
|
||||
return fmt.Errorf("no wiki page found for %q", name)
|
||||
}
|
||||
|
||||
pageTitle := results[0].Title
|
||||
exchangeItem, err := wikiClient.GetExchangeModule(pageTitle)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to look up exchange data: %w", err)
|
||||
}
|
||||
if exchangeItem == nil {
|
||||
return fmt.Errorf("%q is not tradeable on the Grand Exchange", pageTitle)
|
||||
}
|
||||
|
||||
rs3Client := prices.NewRS3Client()
|
||||
detail, err := rs3Client.GetDetail(exchangeItem.ItemID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch RS3 price data: %w", err)
|
||||
}
|
||||
|
||||
md := render.New()
|
||||
md.H1(detail.Name)
|
||||
md.KV("Examine", exchangeItem.Examine)
|
||||
md.KV("Members", fmt.Sprintf("%v", exchangeItem.Members))
|
||||
if exchangeItem.Limit > 0 {
|
||||
md.KV("Buy limit", render.FormatNumber(exchangeItem.Limit))
|
||||
}
|
||||
md.KV("Store value", render.FormatGP(exchangeItem.Value))
|
||||
md.Newline()
|
||||
|
||||
md.H2("Grand Exchange Price")
|
||||
md.KV("Current price", detail.CurrentPrice+" gp")
|
||||
if detail.TodayPrice != 0 {
|
||||
md.KV("Today's change", render.FormatGP(detail.TodayPrice))
|
||||
} else {
|
||||
md.KV("Today's change", "0 gp")
|
||||
}
|
||||
md.KV("30-day change", fmt.Sprintf("%s (%s)", detail.Day30Change, detail.Day30Trend))
|
||||
md.KV("90-day change", fmt.Sprintf("%s (%s)", detail.Day90Change, detail.Day90Trend))
|
||||
md.KV("180-day change", fmt.Sprintf("%s (%s)", detail.Day180Change, detail.Day180Trend))
|
||||
md.Newline()
|
||||
|
||||
fmt.Print(md.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
func timeAgo(unixTime int64) string {
|
||||
t := time.Unix(unixTime, 0)
|
||||
d := time.Since(t)
|
||||
|
||||
Reference in New Issue
Block a user