Parses graphs and other data from camelcamelcamel.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 

89 lines
2.4 KiB

package main
import (
"fmt"
"image/png"
"os"
"time"
"git.geek1011.net/geek1011/camelparse/camelcamelcamel"
"git.geek1011.net/geek1011/camelparse/graph"
"github.com/spf13/pflag"
)
func main() {
locale := pflag.StringP("locale", "l", "us", "Amazon store location (gb, us, ca)")
writeGraph := pflag.StringP("write-graph", "g", "", "Write the PNG graph to a file (overwrites if exists)")
precision := pflag.BoolP("precision", "p", false, "Write precision information")
highQuality := pflag.BoolP("high-quality", "h", false, "Double graph size (warning: slow, drastically increases memory usage)")
csv := pflag.BoolP("csv", "c", false, "CSV output (date,price,outofstock)")
help := pflag.Bool("help", false, "Show this help text")
pflag.Parse()
if *help || pflag.NArg() != 1 {
fmt.Fprintf(os.Stderr, "Usage: %s ASIN\n\nOptions:\n", os.Args[0])
pflag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nNote: Only items sold by Amazon are currently supported\n")
os.Exit(2)
}
if *locale != "gb" && *locale != "us" && *locale != "ca" {
fmt.Fprintf(os.Stderr, "Error: Unsupported locale %#v, see --help for supported options.", locale)
os.Exit(1)
}
asin := pflag.Arg(0)
cr, err := camelcamelcamel.Camelizer(asin, *locale)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Could not camelize: %v\n", err)
os.Exit(1)
}
width, height := 10000, 6000
if *highQuality {
width, height = 16000, 8000
}
cg, err := camelcamelcamel.Graph(asin, *locale, width, height)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Could not get graph: %v\n", err)
os.Exit(1)
}
if *writeGraph != "" {
f, err := os.Create(*writeGraph)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Could not create graph file: %v\n", err)
os.Exit(1)
}
if err := png.Encode(f, cg); err != nil {
fmt.Fprintf(os.Stderr, "Error: Could not write graph file: %v\n", err)
f.Close()
os.Exit(1)
}
f.Close()
}
pc, pp, dp, err := graph.Parse(cg, cr.HighestPricing.PriceAmazon.Price.Price(), cr.LowestPricing.PriceAmazon.Price.Price(), *cr.CreatedAt, time.Now())
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Could not parse graph file: %v\n", err)
os.Exit(1)
}
if *precision {
if *csv {
fmt.Printf("date(+-%.2f days),price(+-$%.4f),outofstock\n", dp, pp)
} else {
fmt.Printf("date +-%.2f days\nprice +-$%.4f\n", dp, pp)
}
}
for _, p := range pc {
if *csv {
fmt.Println(p.CSV())
} else {
fmt.Println(p)
}
}
}