/
guts.go
64 lines (51 loc) · 1.22 KB
/
guts.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
package main
import (
"fmt"
"github.com/gedex/go-instagram/instagram"
"html/template"
"net/http"
"strings"
)
var pics = [20]string{}
type Pic struct {
Images template.HTML
}
func homeQuery(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
t, _ := template.ParseFiles("main.html")
t.Execute(w, r)
} else {
r.ParseForm()
manipulateStuff(r.Form["food"])
http.Redirect(w,r,"/results", http.StatusFound)
}
}
func resultsHandler(w http.ResponseWriter, r *http.Request) {
html := ""
for _, pic := range pics {
html += fmt.Sprintf("<div><img src='%v' /></div>", pic)
}
result_pics := Pic{template.HTML(html)}
template, _ := template.ParseFiles("results.html")
template.Execute(w, result_pics)
}
func manipulateStuff(food []string) {
s := food
s = strings.Split(s[0], " ")
callInstagram(strings.Join(s, ""))
}
func callInstagram(query string) {
client := instagram.NewClient(nil)
media, _, _ := client.Tags.RecentMedia(query, nil)
index := 0
for index < len(media) {
pics[index] = media[index].Images.LowResolution.URL
index += 1
}
return
}
func main() {
http.HandleFunc("/results", resultsHandler)
http.HandleFunc("/", homeQuery)
http.ListenAndServe("localhost:3000", nil)
}