forked from hnrss/hnrss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecial.go
46 lines (39 loc) · 866 Bytes
/
special.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"astuart.co/goq"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
"strings"
)
func Special(url, title string) gin.HandlerFunc {
return func(c *gin.Context) {
var sp SearchParams
var op OutputParams
ParseRequest(c, &sp, &op)
resp, err := algoliaClient.Get(url)
if err != nil {
c.Error(err)
c.String(http.StatusBadGateway, err.Error())
return
}
defer resp.Body.Close()
var parsed ItemList
err = goq.NewDecoder(resp.Body).Decode(&parsed)
if err != nil {
c.Error(err)
c.String(http.StatusBadGateway, err.Error())
return
}
var sids []string
for _, id := range parsed.Thing {
sids = append(sids, "story_"+id)
}
sp.Tags = fmt.Sprintf("(story,poll),(%s)", strings.Join(sids, ","))
sp.Count = strconv.Itoa(len(sids))
op.Title = title
op.Link = url
Generate(c, &sp, &op)
}
}