SkunkyArt/user/search.go

57 lines
1.4 KiB
Go

package user
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"math"
"skunkyart/util"
"strconv"
)
func search(user, query *string, page int, output chan string) {
var buf bytes.Buffer
{
var tmp struct {
Total int
Results, Pages, Name string
}
var q struct {
EstTotal int
Results []util.Deviation
}
if *query != "" {
rawjson := util.Puppy("dashared/gallection/search?username=" + *user + "&q=" + *query + "&offset=" + strconv.Itoa(50*page) + "&type=gallery&order=most-recent&init=true&limit=50")
err := json.Unmarshal([]byte(rawjson), &q)
if err != nil {
fmt.Println(err)
} else {
if q.EstTotal != 0 {
tmp.Total = q.EstTotal
}
for _, a := range q.Results {
tmp.Results += util.Images(a, a.Media, false)
}
tmp.Pages = ""
tmp.Name = *user
for x := 0; x < int(math.Round(float64(q.EstTotal/50))); x++ {
tmp.Pages += "<a href=\"/user/" + tmp.Name + "?a=search&p=" + strconv.Itoa(x+1) + "&q=" + *query + "\">" + strconv.Itoa(x+1) + "</a> "
}
}
tmpl, _ := template.ParseFiles("templates/user/search.htm")
tmpl.Execute(&buf, tmp)
tmpl.ExecuteTemplate(&buf, "R", template.HTML(tmp.Results))
tmpl.ExecuteTemplate(&buf, "P", template.HTML(tmp.Pages))
} else {
tmpl, _ := template.ParseFiles("templates/user/search-base.htm")
tmpl.Execute(&buf, *user)
}
}
output <- buf.String()
}