Better download list parsing

This commit is contained in:
Belac Darkstorm
2017-04-14 03:36:50 -05:00
parent 9e9bb82025
commit a55c82483a
8 changed files with 158 additions and 35 deletions
+4 -2
View File
@@ -1,11 +1,13 @@
package appimg
type appimg struct {
name string
full string
name string
version string
}
func newApp(name string) appimg {
var out appimg
out.name = name
out.full = name
return out
}
+6 -5
View File
@@ -19,7 +19,7 @@ func downloadApp(parent *gtk.Window, ap appimg) {
})
spn, _ := gtk.SpinnerNew()
spn.Start()
lbl, _ := gtk.LabelNew("Downloading " + ap.name + "...")
lbl, _ := gtk.LabelNew("Downloading " + ap.full + "...")
box, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 5)
box.SetMarginStart(10)
box.SetMarginEnd(10)
@@ -39,13 +39,13 @@ func downloadApp(parent *gtk.Window, ap appimg) {
return nil
},
}
resp, err := check.Get(urlBase + ap.name)
resp, err := check.Get(urlBase + ap.full)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
name := strings.Split(ap.name, "-")[0]
name := strings.Split(ap.full, "-")[0]
var foldName string
if _, err = os.Open("PortableApps/" + name + "Portable"); err == nil {
foldName = "PortableApps/" + name + "Portable"
@@ -55,12 +55,13 @@ func downloadApp(parent *gtk.Window, ap appimg) {
os.Mkdir("PortableApps/"+name+"Portable", 0777)
foldName = "PortableApps/" + name
}
fil, err := os.Create(foldName + "/" + ap.name)
os.Remove(foldName + "/" + ap.full)
fil, err := os.Create(foldName + "/" + ap.full)
if err != nil {
fmt.Println(err)
return
}
io.Copy(fil, resp.Body)
_ = fil.Chmod(0777)
fil.Chmod(0777)
}(win, ap)
}
+40 -8
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"sort"
"strings"
"github.com/gotk3/gotk3/glib"
@@ -16,7 +17,7 @@ const (
)
//ShowUI shows the list of possible AppImages to be downloaded in a gtk.Window
func ShowUI(clsFunc func()) {
func ShowUI(newestVersionOnly bool, clsFunc func()) {
win, _ := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
win.Connect("destroy", func() {
clsFunc()
@@ -45,13 +46,44 @@ func ShowUI(clsFunc func()) {
win.Show()
getList(win, apch)
go func(win *gtk.Window, apch chan appimg, list *gtk.ListBox) {
for i := range apch {
glib.IdleAdd(func(list *gtk.ListBox, i appimg) {
lbl, _ := gtk.LabelNew(i.name)
list.Add(lbl)
apps = append(apps, i)
lbl.Show()
}, list, i)
if newestVersionOnly {
imgs := make([]appimg, 0)
a := make(map[string][]appimg)
names := make([]string, 0)
for i := range apch {
imgs = append(imgs, i)
}
for i, v := range imgs {
sp := strings.Split(v.full, "-")
if len(sp) >= 2 {
vers := sp[1]
removeLetters(vers)
imgs[i].version = vers
imgs[i].name = sp[0]
if _, ok := a[imgs[i].name]; !ok {
names = append(names, imgs[i].name)
}
a[imgs[i].name] = append(a[imgs[i].name], imgs[i])
}
}
sort.Strings(names)
for _, name := range names {
glib.IdleAdd(func(name string, list *gtk.ListBox, i appimg) {
lbl, _ := gtk.LabelNew(name)
list.Add(lbl)
apps = append(apps, i)
lbl.Show()
}, name, list, a[name][compareVersions(a[name])])
}
} else {
for i := range apch {
glib.IdleAdd(func(list *gtk.ListBox, i appimg) {
lbl, _ := gtk.LabelNew(i.full)
list.Add(lbl)
apps = append(apps, i)
lbl.Show()
}, list, i)
}
}
}(win, apch, appList)
}
+12
View File
@@ -0,0 +1,12 @@
package appimg
import "strings"
func removeLetters(vers string) string {
vers = strings.ToLower(vers)
letters := []string{"abcdefghijklmnopqrstuvwxyz"}
for _, v := range letters {
vers = strings.Replace(vers, v, "", -1)
}
return vers
}
+33
View File
@@ -0,0 +1,33 @@
package appimg
import (
"strconv"
"strings"
)
func compareVersions(imgs []appimg) int {
for i := range imgs {
imgs[i].version = removeLetters(imgs[i].version)
}
highest := 0
higharr := strings.Split(imgs[0].version, ".")
for i := 0; i < len(imgs); i++ {
if i != highest {
varr := strings.Split(imgs[i].version, ".")
if len(higharr) < len(varr) {
for j := 0; j < len(higharr); j++ {
h, _ := strconv.Atoi(higharr[j])
c, _ := strconv.Atoi(varr[j])
if h > c {
break
} else if c > h {
highest = i
higharr = varr
break
}
}
}
}
}
return highest
}