/
server.go
405 lines (333 loc) · 8.92 KB
/
server.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package main
import (
"encoding/json"
"errors"
"fmt"
"html/template"
"io"
"net/http"
"strconv"
"time"
"log"
"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"
"github.com/patrickmn/go-cache"
"github.com/spf13/viper"
)
type config struct {
CLIENTID string
CLIENTSECRET string
BASEURL string
USERNAME string
PORT string
}
var C config
var InstaCache *cache.Cache
type UserResponse struct {
MetaResponse
User *User `json:"data"`
}
type UsersResponse struct {
MetaResponse
Users []User `json:"data"`
}
type MediaResponse struct {
MetaResponse
Media *Media `json:"data"`
}
type MediasResponse struct {
MetaResponse
Medias []Media `json:"data"`
}
type MetaResponse struct {
Meta *Meta
}
type User struct {
Username string `json:"username"`
FirstName string `json:"first_name"`
ProfilePicture string `json:"profile_picture"`
ID string `json:"id"`
LastName string `json:"last_name"`
}
type Meta struct {
Code int
ErrorType string `json:"error_type"`
ErrorMessage string `json:"error_message"`
}
// Instagram Media object
type Media struct {
Type string
Id string
UsersInPhoto []UserPosition `json:"users_in_photo"`
Filter string
Tags []string
Comments *Comments
Caption *Caption
Likes *Likes
Link string
User *User
CreatedTime StringUnixTime `json:"created_time"`
Images *Images
Videos *Videos
Location *Location
UserHasLiked bool `json:"user_has_liked"`
Attribution *Attribution
}
// A pair of user object and position
type UserPosition struct {
User *User
Position *Position
}
// A position in a media
type Position struct {
X float64
Y float64
}
// Instagram tag
type Tag struct {
MediaCount int64 `json:"media_count"`
Name string
}
type Comments struct {
Count int64
Data []Comment
}
type Comment struct {
CreatedTime StringUnixTime `json:"created_time"`
Text string
From *User
Id string
}
type Caption Comment
type Likes struct {
Count int64
Data []User
}
type Images struct {
LowResolution *Image `json:"low_resolution"`
Thumbnail *Image
StandardResolution *Image `json:"standard_resolution"`
}
type Image struct {
Url string
Width int64
Height int64
}
type Videos struct {
LowResolution *Video `json:"low_resolution"`
StandardResolution *Video `json:"standard_resolution"`
}
type Video Image
type Location struct {
Id LocationId
Name string
Latitude float64
Longitude float64
}
type Relationship struct {
IncomingStatus string `json:"incoming_status"`
OutgoingStatus string `json:"outgoing_status"`
}
// If another app uploaded the media, then this is the place it is given. As of 11/2013, Hipstamic is the only allowed app
type Attribution struct {
Website string
ItunesUrl string
Name string
}
type StringUnixTime string
func (s StringUnixTime) Time() (t time.Time, err error) {
unix, err := strconv.ParseInt(string(s), 10, 64)
if err != nil {
return
}
t = time.Unix(unix, 0)
return
}
// Sometimes location Id is a string and sometimes its an integer
type LocationId interface{}
func ParseLocationId(lid LocationId) string {
if lid == nil {
return ""
}
if slid, ok := lid.(string); ok {
return slid
}
if ilid, ok := lid.(int64); ok {
return fmt.Sprintf("%d", ilid)
}
return ""
}
func getUserID(username string) (string, error) {
usersResponse := new(UsersResponse)
response, err := http.Get(C.BASEURL + "/users/search?q=" + username + "&client_id=" + C.CLIENTID)
if err != nil {
return "", err
}
defer response.Body.Close()
if err = json.NewDecoder(response.Body).Decode(&usersResponse); err != nil {
return "", err
}
if usersResponse.Meta.Code == 200 {
// search for id of given user in array
for _, value := range usersResponse.Users {
if value.Username == username {
return value.ID, nil
}
}
}
return "", errors.New(usersResponse.Meta.ErrorMessage)
}
func getRecentMedia(userID string, count int) (*MediasResponse, error) {
mediasResponse := new(MediasResponse)
response, err := http.Get(C.BASEURL + "/users/" + userID + "/media/recent/?client_id=" + C.CLIENTID + "&count=" + strconv.Itoa(count))
if err != nil {
return mediasResponse, err
}
defer response.Body.Close()
if err = json.NewDecoder(response.Body).Decode(&mediasResponse); err != nil {
return mediasResponse, err
}
if mediasResponse.Meta.Code == 200 {
return mediasResponse, nil
}
return mediasResponse, errors.New(mediasResponse.Meta.ErrorMessage)
}
// Handler
func recentMedia(c *echo.Context) error {
var err error
//log.Println("InstaChache: ", InstaCache)
// Caching UserID
var id string
x, found := InstaCache.Get("userID")
if found {
id = x.(string)
} else {
id, err = getUserID(C.USERNAME)
if err != nil {
log.Println("Error in getUserID func: ", err)
err = c.String(http.StatusBadRequest, "User not found: "+err.Error())
}
if err == nil {
InstaCache.Set("userID", id, cache.DefaultExpiration)
}
}
//log.Println("UserID: ", id)
// Caching Instafeed last 20 photos
var mediasResponse *MediasResponse
if x, found := InstaCache.Get("mediasResponse"); found {
mediasResponse = x.(*MediasResponse)
} else {
mediasResponse, err = getRecentMedia(id, 20)
if err != nil {
log.Println("Error in getRecentMedia: ", err)
err = c.String(http.StatusBadRequest, "Media not found: "+err.Error())
}
if err == nil {
InstaCache.Set("mediasResponse", mediasResponse, cache.DefaultExpiration)
}
}
if mediasResponse.Meta.Code == 200 {
err = c.Render(http.StatusOK, "index", mediasResponse)
}
return err
}
func generateEndingColumns(columnsToGenerate, columns int) string {
var feed string
for i := 1; i <= columnsToGenerate; i++ {
feed += `<div class="col-sm-6 col-md-` + strconv.Itoa(columns) + `"></div>`
if i == columnsToGenerate {
feed += `</div>`
}
}
return feed
}
func buildInstaFeed(medias []Media, itemsPerRow int) template.HTML {
counter := 1
var feed string
columns := 12 / itemsPerRow
itemsCount := len(medias)
rows := itemsCount / itemsPerRow
rowsMod := itemsCount % itemsPerRow
if rowsMod > 0 {
rows++
}
realItemsCount := itemsPerRow * rows
endingEmptyColumns := realItemsCount - itemsCount
for i := 0; i < itemsCount; i++ {
imageURL := medias[i].Images.LowResolution.Url
link := medias[i].Link
if counter == 1 {
feed += `<div class="insta row">`
feed += `<div class="col-sm-6 col-md-` + strconv.Itoa(columns) + `">`
feed += `<a href="` + link + `"><img src="` + imageURL + `" alt="" class="img-responsive"></a>`
feed += `</div>`
counter++
} else if counter == itemsPerRow {
feed += `<div class="col-sm-6 col-md-` + strconv.Itoa(columns) + `">`
feed += `<a href="` + link + `"><img src="` + imageURL + `" alt="" class="img-responsive"></a>`
feed += `</div>`
feed += `</div>`
counter = 1
} else {
feed += `<div class="col-sm-6 col-md-` + strconv.Itoa(columns) + `">`
feed += `<a href="` + link + `"><img src="` + imageURL + `" alt="" class="img-responsive"></a>`
feed += `</div>`
counter++
}
feed += generateEndingColumns(endingEmptyColumns, columns)
}
return template.HTML(feed)
}
type Template struct {
templates *template.Template
}
// Render HTML
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func main() {
// init config
viper.SetConfigName("config") // name of config file (without extension)
viper.AddConfigPath(".")
err := viper.ReadInConfig() // Find and read the config file
if err != nil {
log.Println(err)
// Handle errors reading the config file
viper.AutomaticEnv()
//panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
// err = viper.Marshal(&C)
// if err != nil {
// log.Fatalf("unable to decode into struct, %v", err)
// }
//os.Getenv("PORT")
// log.Println("Mimozaflowers port: ", viper.GetString("port"))
C = config{CLIENTID: viper.GetString("clientid"),
CLIENTSECRET: viper.GetString("clientsecret"),
BASEURL: viper.GetString("baseurl"),
USERNAME: viper.GetString("username"),
PORT: viper.GetString("port")}
// set cache for data from instagram
InstaCache = cache.New(5*time.Hour, 30*time.Minute)
// Gin instance
router := echo.New() //gin.Default()
html := template.Must(template.New("").Funcs(template.FuncMap{"buildInstaFeed": buildInstaFeed}).ParseGlob("templates/*.html"))
t := &Template{templates: html}
router.SetRenderer(t)
//router.SetHTMLTemplate(html)
// Middleware
router.Use(mw.Logger())
router.Use(mw.Recover())
// Routes
router.Static("/js/", "public/js")
router.Static("/css/", "public/css")
router.Get("/", recentMedia)
// Start server
//err = router.Run(":" + C.PORT)
//graceful.Run(":"+C.PORT, 5*time.Second, router)
//graceful.ListenAndServe(router.Server(":"+C.PORT), 5*time.Second)
router.Run(":" + C.PORT)
// if err != nil {
// log.Println("Error: ", err)
// }
}