-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
216 lines (177 loc) · 4.84 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/gorilla/mux"
"github.com/octplane/flickgo"
"log"
"math"
"net/http"
"os"
"strconv"
"strings"
)
var (
apiKey string
secret string
client *flickgo.Client
)
// Define and parse flags
func init() {
// https://www.flickr.com/services/apps/by/oct
flag.StringVar(&apiKey, "api_key", "", "Api Key")
flag.StringVar(&secret, "secret", "", "Secret Key")
}
type Photo struct {
Id string `json:"id"`
Title string `json:"title"`
Thumb string `json:"thumb"` // n
Large string `json:"large"` // z
Width int `json:"width"`
Height int `json:"height"`
}
type PhotoInfo struct {
ID string `json:"id"`
Rotation string `json:"rotation,attr"`
License string `json:"license,attr"`
Title string `json:"title"`
Description string `json:"description"`
Dates flickgo.Dates `xml:"dates"`
Urls []flickgo.Url `xml:"urls>url"`
Thumb string `json:"thumb"`
Large string `json:"large"`
TagsRef []string `json:"tags"`
}
type Tag struct {
ID string `json:"id"`
Text string `json:"text"`
Photoinfo string `json:"photoinfo_id"`
}
func TagsToTags(tags []flickgo.Tag, photoinfo string) []Tag {
ret := make([]Tag, 0, 20)
for _, tag := range tags {
ids := strings.Split(tag.ID, "-")
ret = append(ret, Tag{
ID: ids[len(ids)-1],
Text: tag.Text,
Photoinfo: photoinfo})
}
return ret
}
func TagsToId(tags []Tag) []string {
ret := make([]string, 0, 20)
for _, tag := range tags {
ret = append(ret, tag.ID)
}
return ret
}
func photoHandler(w http.ResponseWriter, r *http.Request) {
client := flickgo.New(apiKey, secret, http.DefaultClient)
photo_id := mux.Vars(r)["photo_id"]
finfo, err := client.GetInfo(photo_id)
if err != nil {
panic(err)
}
tags := TagsToTags(finfo.Tags, finfo.ID)
info := PhotoInfo{
ID: finfo.ID,
Rotation: finfo.Rotation,
License: finfo.License,
Title: finfo.Title,
Description: finfo.Description,
Dates: finfo.Dates,
Urls: finfo.Urls,
Thumb: finfo.Photo.URL("n"),
Large: finfo.Photo.URL("z"),
TagsRef: TagsToId(tags),
}
// Serialize the modified kitten to JSON
j, err := json.MarshalIndent(map[string]interface{}{
"tags": tags,
"photoinfo": &info,
}, "", " ")
if err != nil {
panic(err)
}
w.Write(j)
}
func searchHandler(w http.ResponseWriter, r *http.Request) {
photo_id := mux.Vars(r)["photo_id"]
query := r.FormValue("query")
fmt.Printf("Searching for %s\n", query)
client = flickgo.New(apiKey, secret, http.DefaultClient)
max := 50
photos := make([]Photo, 0, max)
if photo_id != "" {
finfo, err := client.GetInfo(photo_id)
if err != nil {
panic(err)
}
info := Photo{
Id: finfo.ID,
Title: finfo.Title,
Thumb: finfo.Photo.URL("n"),
Large: finfo.Photo.URL("z"),
}
photos = append(photos, info)
} else {
sparams := make(map[string]string)
sparams["text"] = query
sparams["sort"] = "relevance" //interestingness-desc"
sparams["licence"] = "4,7"
sparams["extras"] = "o_dims"
var err error
var sresponse *flickgo.SearchResponse
sresponse, err = client.Search(sparams)
if err != nil {
panic(err)
}
var resCount int
var tempPhoto Photo
resCount, err = strconv.Atoi(sresponse.Total)
if err != nil {
panic(err)
}
var curPhoto flickgo.SearchPhoto
for i := 0; i < int(math.Min(float64(resCount), float64(max))); i++ {
curPhoto = sresponse.Photos[i]
tempPhoto = Photo{Id: curPhoto.ID,
Thumb: curPhoto.URL("n"),
Large: curPhoto.URL("z"),
Title: curPhoto.Title,
Width: curPhoto.Width,
Height: curPhoto.Height,
}
photos = append(photos, tempPhoto)
}
}
// Serialize the modified kitten to JSON
j, err := json.MarshalIndent(map[string][]Photo{"photos": photos}, "", " ")
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(j)
}
func main() {
flag.Parse()
if apiKey == "" || secret == "" {
fmt.Println("Missing one or more command line options.")
flag.PrintDefaults()
os.Exit(2)
}
log.Println("Starting Server")
r := mux.NewRouter()
r.HandleFunc("/api/v1/photos/{photo_id:[a-z0-9]+}", searchHandler).Methods("GET")
r.HandleFunc("/api/v1/photoinfos/{photo_id:[a-z0-9]+}", photoHandler).Methods("GET")
r.HandleFunc("/api/v1/photos", searchHandler).Methods("GET")
http.Handle("/api/", r)
http.Handle("/", http.FileServer(http.Dir("./public/")))
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("Listening on %s\n", port)
http.ListenAndServe(":"+port, nil)
}