Skip to content

Commit

Permalink
Merge pull request #159 from jakopako:jakopako/issue154
Browse files Browse the repository at this point in the history
Jakopako/issue154
  • Loading branch information
jakopako authored Jan 2, 2023
2 parents ea179da + 62b8942 commit 05289e0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
37 changes: 31 additions & 6 deletions automate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/PuerkitoBio/goquery"
"github.com/agnivade/levenshtein"
"github.com/gdamore/tcell/v2"
"github.com/jakopako/goskyr/fetch"
"github.com/jakopako/goskyr/scraper"
Expand All @@ -20,10 +21,37 @@ type locationProps struct {
count int
examples []string
selected bool
color tcell.Color
distance float64
}

type locationManager []*locationProps

func (l locationManager) setColors() {
if len(l) == 0 {
return
}
for i, e := range l {
if i != 0 {
e.distance = l[i-1].distance + distance(l[i-1].loc, e.loc)
}
}
// scale to 1 and map to rgb
maxDist := l[len(l)-1].distance * 1.2
s := 0.73
v := 0.96
for _, e := range l {
h := e.distance / maxDist
r, g, b := utils.HSVToRGB(h, s, v)
e.color = tcell.NewRGBColor(r, g, b)
}
}

func distance(loc1, loc2 scraper.ElementLocation) float64 {
// calculate differently? eg with nodes of html tree. eg nodes to walk to get from loc1 to loc2
return float64(levenshtein.ComputeDistance(loc1.Selector, loc2.Selector))
}

func update(l locationManager, e scraper.ElementLocation, s string) locationManager {
for _, lp := range l {
if checkAndUpdatePath(&lp.loc, &e) {
Expand Down Expand Up @@ -326,12 +354,9 @@ parse:
}

locMan = filter(locMan, minOcc, removeStaticFields)
locMan.setColors()

if len(locMan) > 0 {
sort.Slice(locMan, func(p, q int) bool {
return locMan[p].loc.Selector > locMan[q].loc.Selector
})

selectFieldsTable(locMan)

var fs []scraper.ElementLocation
Expand Down Expand Up @@ -381,7 +406,7 @@ func selectFieldsTable(locMan locationManager) {
}
table.SetCell(r, c,
tview.NewTableCell(ss).
SetTextColor(color).
SetTextColor(locMan[r-1].color).
SetAlign(tview.AlignCenter))
}
}
Expand All @@ -404,7 +429,7 @@ func selectFieldsTable(locMan locationManager) {
} else {
table.GetCell(row, 0).SetTextColor(tcell.ColorGreen)
for i := 1; i < 5; i++ {
table.GetCell(row, i).SetTextColor(tcell.ColorWhite)
table.GetCell(row, i).SetTextColor(locMan[row-1].color)
}
}
})
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (

require (
github.com/BurntSushi/toml v1.2.0 // indirect
github.com/agnivade/levenshtein v1.1.1
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/chromedp/sysutil v1.0.0 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U=
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRBij0P8=
github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
github.com/chromedp/cdproto v0.0.0-20220930195933-357956813629 h1:idJfU8ZgBwWWIJwlybyyrlVenTdHYEvlnaQvoagh6eE=
github.com/chromedp/cdproto v0.0.0-20220930195933-357956813629/go.mod h1:5Y4sD/eXpwrChIuxhSr/G20n9CdbCmoerOHnuAf0Zr0=
github.com/chromedp/chromedp v0.8.6 h1:KobeeqR2dpfKSG1prS3Y6+FbffMmGC6xmAobRXA9QEQ=
github.com/chromedp/chromedp v0.8.6/go.mod h1:nBYHoD6YSNzrr82cIeuOzhw1Jo/s2o0QQ+ifTeoCZ+c=
github.com/chromedp/sysutil v1.0.0 h1:+ZxhTpfpZlmchB58ih/LBHX52ky7w2VhQVKQMucy3Ic=
github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moAV0xufSww=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.5.3 h1:b9XQrT6QGbgI7JvZOJXFNczOQeIYbo8BfeSMzt2sAV0=
Expand Down
42 changes: 42 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"fmt"
"math"
)

func ShortenString(s string, l int) string {
Expand All @@ -10,3 +11,44 @@ func ShortenString(s string, l int) string {
}
return s
}

func HSVToRGB(h, s, v float64) (int32, int32, int32) {
// from https://go.dev/play/p/9q5yBNDh3W
var r, g, b float64
h = h * 6
i := math.Floor(h)
v1 := v * (1 - s)
v2 := v * (1 - s*(h-i))
v3 := v * (1 - s*(1-(h-i)))

if i == 0 {
r = v
g = v3
b = v1
} else if i == 1 {
r = v2
g = v
b = v1
} else if i == 2 {
r = v1
g = v
b = v3
} else if i == 3 {
r = v1
g = v2
b = v
} else if i == 4 {
r = v3
g = v1
b = v
} else {
r = v
g = v1
b = v2
}

r = r * 255 //RGB results from 0 to 255
g = g * 255
b = b * 255
return int32(r), int32(g), int32(b)
}

0 comments on commit 05289e0

Please sign in to comment.