SkunkyArt/misc/search.go

64 lines
1.5 KiB
Go

package misc
import (
"encoding/json"
"math"
"skunkyart/util"
"strconv"
)
func Search(q, p, scope string, output chan string) {
var tmp struct {
Results, Pages string
Total int
}
if q != "" {
var j struct {
EstTotal int
Deviations []util.Deviation
}
var rawjson string
switch scope {
case "", "all":
rawjson = util.Puppy("dabrowse/search/all?q=" + q + "&page=" + p)
case "tag":
rawjson = util.Puppy("dabrowse/networkbar/tag/deviations?tag=" + q + "&page=" + p)
default:
output <- "Unknown scope."
}
json.Unmarshal([]byte(rawjson), &j)
for _, a := range j.Deviations {
tmp.Results += util.Images(a, a.Media, false)
}
tmp.Total = j.EstTotal
switch scope {
case "all":
for x := 0; x < int(math.Round(float64(tmp.Total/25))); x++ {
if x == 417 {
break
}
tmp.Pages += "<a href=\"" + util.Conf.Base_uri + "search?p=" + strconv.Itoa(x+1) + "&scope=all&q=" + q + "\">" + strconv.Itoa(x+1) + "</a> "
}
case "tag":
if p == "" {
p = "1"
}
next, _ := strconv.ParseInt(p, 10, 32)
if int(next) > 1 {
tmp.Pages += "<a href=\"" + util.Conf.Base_uri + "search?p=" + strconv.Itoa(int(next)-1) + "&scope=tag&q=" + q + "\"><-- Back</a> "
}
tmp.Pages += "<a href=\"" + util.Conf.Base_uri + "search?p=" + strconv.Itoa(int(next)+1) + "&scope=tag&q=" + q + "\">Next --></a>"
default:
output <- "Missing or invalid scope."
}
} else {
output <- "Missing query."
}
output <- util.TmpExec("templates/search.htm", &tmp)
}