This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.go
91 lines (76 loc) · 1.72 KB
/
index.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package xkcdslack
import (
"net/http"
"strconv"
"github.com/julienschmidt/httprouter"
"appengine"
"appengine/search"
"appengine/taskqueue"
)
const xkcdIndex = "xkcd"
type ComicSearch struct {
Num string
Title string
Img string
Alt string
Transcript string
}
func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
c := appengine.NewContext(r)
index, err := search.Open(xkcdIndex)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var x *Comic
comicNum := r.FormValue("id")
if comicNum != "" {
iComicNum, _ := strconv.Atoi(comicNum)
x, _ = Get(c, iComicNum)
} else {
x, _ = GetCurrent(c)
}
xSearch := &ComicSearch{
strconv.Itoa(x.Num),
x.Title,
x.Img,
x.Alt,
x.Transcript,
}
id := strconv.Itoa(x.Num)
_, err = index.Put(c, id, xSearch)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
func backfill(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
c := appengine.NewContext(r)
index, err := search.Open(xkcdIndex)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
current, _ := GetCurrent(c)
for i := 1; i <= current.Num; i++ {
// xcdc returns 404 with issue 404
if i == 404 {
continue
}
comicNum := strconv.Itoa(i)
force := r.FormValue("force")
if force != "yes" {
var s ComicSearch
err := index.Get(c, comicNum, &s)
if err == nil {
continue
}
}
t := taskqueue.NewPOSTTask("/index", map[string][]string{"id": {comicNum}})
if _, err := taskqueue.Add(c, t, ""); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}