Browse Source

Reorganized code

master
Patrick Gaskin 2 years ago
parent
commit
95425218a7
Signed by: geek1011 GPG Key ID: A2FD79F68A2AB707
  1. 2
      go.mod
  2. 40
      graph/demo/main.go
  3. 6
      graph/graph.go
  4. 0
      graph/testdata/graph.png
  5. 14
      graph/util.go
  6. 25
      main.go

2
go.mod

@ -1,3 +1,3 @@
module dataextract2
module git.geek1011.net/geek1011/camelparse
go 1.12

40
graph/demo/main.go

@ -0,0 +1,40 @@
package main
import (
"fmt"
"image"
"image/png"
"os"
"time"
"git.geek1011.net/geek1011/camelparse/graph"
)
func main() {
maxPrice, minPrice := 59.99, 9.99
startDate, endDate := time.Date(2015, 10, 29, 0, 0, 0, 0, time.Local), time.Date(2019, 05, 15, 0, 0, 0, 0, time.Local)
img, err := loadPNG("../testdata/graph.png")
if err != nil {
panic(err)
}
parsed, err := graph.Parse(img, maxPrice, minPrice, startDate, endDate)
if err != nil {
panic(err)
}
for _, p := range parsed {
fmt.Println(p.String())
}
}
// loadPNG loads a PNG image.
func loadPNG(path string) (image.Image, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return png.Decode(f)
}

6
graph.go → graph/graph.go

@ -1,4 +1,4 @@
package main
package graph
import (
"errors"
@ -47,9 +47,9 @@ func (p PriceChange) CSV() string {
return fmt.Sprintf("%s,%.2f,%d", p.Date.Format("2006-01-02"), p.Price, os)
}
// ParseGraph parses a camelcamelcamel price graph (only supports single Amazon
// Parse parses a camelcamelcamel price graph (only supports single Amazon
// price line though).
func ParseGraph(img image.Image, maxPrice, minPrice float64, startDate, endDate time.Time) ([]PriceChange, error) {
func Parse(img image.Image, maxPrice, minPrice float64, startDate, endDate time.Time) ([]PriceChange, error) {
graphBounds := getGraphBounds(img, IsGridColor)
if graphBounds == nil {
return nil, errors.New("could not extract graph boundaries")

0
graph.png → graph/testdata/graph.png

Before

Width:  |  Height:  |  Size: 552 KiB

After

Width:  |  Height:  |  Size: 552 KiB

14
util.go → graph/util.go

@ -1,10 +1,8 @@
package main
package graph
import (
"image"
"image/color"
"image/png"
"os"
"time"
)
@ -47,13 +45,3 @@ func colorMatcher(r, g, b uint8) colorMatcherFunc {
func extrapolate(p1, p2 int, v1, v2 float64, p int) float64 {
return float64(p-p1)/float64(p2-p1)*float64(v2-v1) + v1
}
// loadPNG loads a PNG image.
func loadPNG(path string) (image.Image, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return png.Decode(f)
}

25
main.go

@ -1,25 +0,0 @@
package main
import (
"fmt"
"time"
)
func main() {
maxPrice, minPrice := 59.99, 9.99
startDate, endDate := time.Date(2015, 10, 29, 0, 0, 0, 0, time.Local), time.Date(2019, 05, 15, 0, 0, 0, 0, time.Local)
img, err := loadPNG("graph.png")
if err != nil {
panic(err)
}
parsed, err := ParseGraph(img, maxPrice, minPrice, startDate, endDate)
if err != nil {
panic(err)
}
for _, p := range parsed {
fmt.Println(p.String())
}
}