-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
341 lines (301 loc) · 9 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
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
package main
import (
"bufio"
"encoding/json"
"fmt"
"github.com/oemel09/waytoofast/apis/freesound"
"github.com/oemel09/waytoofast/apis/pexels"
"github.com/oemel09/waytoofast/apis/twitter"
"html/template"
"io"
"io/ioutil"
"log"
"math"
"math/rand"
"net/http"
"os"
"strings"
"time"
)
const TemplatesDir = "TEMPLATES_DIR"
const StaticAssetsDir = "STATIC_ASSETS_DIR"
const ConfigFile = "CONFIG_FILE"
const Port = "PORT"
const PathPrefix = "PATH_PREFIX"
const QualityHigh = "xhd"
const QualityStandard = "sd"
const QualityLarge = "l"
const QualitySmall = "s"
const QualityTiny = "t"
var tpl *template.Template
var config Configuration
var port string
var pathPrefix string
var templatesDir = os.Getenv(TemplatesDir)
var staticAssetsDir = os.Getenv(StaticAssetsDir)
var configFile = os.Getenv(ConfigFile)
func init() {
checkMandatoryEnvVars()
checkOptionalEnvVars()
}
func checkMandatoryEnvVars() {
if templatesDir == "" {
log.Fatalln(TemplatesDir, "not set")
} else if staticAssetsDir == "" {
log.Fatalln(StaticAssetsDir, "not set")
} else if configFile == "" {
log.Fatalln(ConfigFile, "not set")
}
}
func checkOptionalEnvVars() {
var ok bool
pathPrefix, ok = os.LookupEnv(PathPrefix)
if ok {
pathPrefix = "/" + pathPrefix
}
port, ok = os.LookupEnv(Port)
if !ok {
port = "61113"
}
}
func main() {
tpl = template.Must(template.ParseGlob(templatesDir + "/*"))
config = loadConfiguration(configFile)
http.HandleFunc("/", index)
http.HandleFunc(pathPrefix+"/twitter", twitterAPI)
http.HandleFunc(pathPrefix+"/pexels-videos", pexelsVideoAPI)
http.HandleFunc(pathPrefix+"/pexels-images", pexelsImageAPI)
http.HandleFunc(pathPrefix+"/free-sounds", freeSoundAPI)
http.Handle(pathPrefix+"/assets/", disableDirectoryListing(
changeHeaderThenServe(
http.StripPrefix(pathPrefix+"/assets", http.FileServer(http.Dir(staticAssetsDir))))))
log.Println("Started application on port", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func disableDirectoryListing(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/assets/") {
io.WriteString(w, "404")
} else {
h.ServeHTTP(w, r)
}
}
}
func changeHeaderThenServe(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var ct string
if strings.HasSuffix(r.URL.Path, ".js") {
ct = "text/javascript"
} else if strings.HasSuffix(r.URL.Path, ".css") {
ct = "text/css"
}
w.Header().Add("Content-Type", ct)
h.ServeHTTP(w, r)
}
}
func index(w http.ResponseWriter, r *http.Request) {
err := tpl.ExecuteTemplate(w, "root.gohtml", pathPrefix)
if err != nil {
fmt.Println("Failed to render index template:", err)
}
}
func twitterAPI(w http.ResponseWriter, r *http.Request) {
twitterUrl := twitter.Url
req, err := http.NewRequest(http.MethodGet, twitterUrl, nil)
if err != nil {
fmt.Println("Failed to initialize twitter request:", err)
}
req.Header.Add("Authorization", "Bearer "+config.Apis.Twitter.BearerToken)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
fmt.Println("Failed to perform twitter request:", err)
}
reader := bufio.NewReader(res.Body)
var fastTweets []twitter.FastTweet
for len(fastTweets) < twitter.MinTweets {
line, err := reader.ReadBytes('\n')
var tweet twitter.Tweet
err = json.Unmarshal(line, &tweet)
if err != nil {
fmt.Println("Failed to unmarshal tweets:", err)
}
fastTweet := twitter.FastTweet{
Id: tweet.Data.Id,
Text: tweet.Data.Text,
Username: getTwitterHandle(tweet.Data.AuthorId, tweet.Includes.Users),
MediaUrl: getTwitterMedia(tweet),
}
fastTweets = append(fastTweets, fastTweet)
}
res.Body.Close()
data, err := json.Marshal(fastTweets)
if err != nil {
fmt.Println("Failed to marshall tweet response:", err)
}
writeResponse(w, data)
}
func pexelsVideoAPI(w http.ResponseWriter, r *http.Request) {
rand.Seed(time.Now().UnixNano())
page := rand.Intn(pexels.TotalVideos/pexels.VideosPerPage) + 1
videosUrl := fmt.Sprintf(pexels.VideoUrl, page, pexels.VideosPerPage, pexels.MaxDuration)
pexelsVideosResponse := performRequest(config.Apis.Pexels.Name, videosUrl, config.Apis.Pexels.APIKey)
var pexelsVideos pexels.PexelsVideos
err := json.Unmarshal(pexelsVideosResponse, &pexelsVideos)
if err != nil {
fmt.Println("Failed to unmarshal pexels videos:", err)
}
var fastVideos []pexels.FastVideo
quality := r.URL.Query().Get("q")
for _, v := range pexelsVideos.Videos {
videoFiles := filterVideoQuality(v.VideoFiles, func(vf pexels.VideoFile) bool {
// quality may be one of: xsd, sd, hd, xhd
// vf.Quality may be on of sd, hd
return vf.FileType == "video/mp4" && strings.Contains(quality, vf.Quality)
})
vf := getBestVideoLink(videoFiles, quality)
fast := pexels.FastVideo{
Duration: v.Duration,
Link: vf.Link,
Width: vf.Width,
Height: vf.Height,
PexelsUrl: v.URL,
}
fastVideos = append(fastVideos, fast)
}
data, err := json.Marshal(fastVideos)
if err != nil {
fmt.Println("Failed to marshall pexels video response:", err)
}
writeResponse(w, data)
}
func pexelsImageAPI(w http.ResponseWriter, r *http.Request) {
rand.Seed(time.Now().UnixNano())
page := rand.Intn(pexels.ImageTotal/pexels.ImagesPerPage) + 1
imagesUrl := fmt.Sprintf(pexels.ImageUrl, page, pexels.ImagesPerPage)
pexelsImagesResponse := performRequest(config.Apis.Pexels.Name, imagesUrl, config.Apis.Pexels.APIKey)
var pexelsImages pexels.PexelsImages
err := json.Unmarshal(pexelsImagesResponse, &pexelsImages)
if err != nil {
fmt.Println("Failed to unmarshal pexels images:", err)
}
var fastImages []pexels.FastImage
quality := r.URL.Query().Get("q")
for _, v := range pexelsImages.Photos {
link := getBestImageLink(v.Src, quality)
fast := pexels.FastImage{
Link: link,
Photographer: v.Photographer,
PexelsUrl: v.URL,
}
fastImages = append(fastImages, fast)
}
data, err := json.Marshal(fastImages)
if err != nil {
fmt.Println("Failed to marshall pexels image response:", err)
}
writeResponse(w, data)
}
func freeSoundAPI(w http.ResponseWriter, r *http.Request) {
rand.Seed(time.Now().UnixNano())
page := rand.Intn(freesound.SoundTotal/freesound.SoundsPerPage) + 1
freeSoundUrl := freesound.SoundUrl + fmt.Sprintf(freesound.SoundParams, page, freesound.SoundsPerPage)
freeSoundResponse := performRequest(config.Apis.FreeSound.Name, freeSoundUrl, "Token "+config.Apis.FreeSound.APIKey)
var freeSounds freesound.FreeSounds
err := json.Unmarshal(freeSoundResponse, &freeSounds)
if err != nil {
fmt.Println("Failed to unmarshal free sounds:", err)
}
var fastSounds []freesound.FastSounds
for _, v := range freeSounds.Results {
sound := freesound.FastSounds{
Link: v.Previews.PreviewLqMp3,
Duration: int(v.Duration),
}
fastSounds = append(fastSounds, sound)
}
data, err := json.Marshal(fastSounds)
if err != nil {
fmt.Println("Failed to marshall fast sound response:", err)
}
writeResponse(w, data)
}
func performRequest(name string, uri string, authorization string) []byte {
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
fmt.Printf("Failed to initialize %s request: %s\n", name, err)
}
req.Header.Add("Authorization", authorization)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to perform %s request: %s\n", name, err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Printf("Failed to read %s response: %s\n", name, err)
}
return body
}
func writeResponse(w http.ResponseWriter, data []byte) {
w.Header().Set("Content-Type", "application/json")
_, err := io.WriteString(w, string(data))
if err != nil {
fmt.Println("Failed to write pexels videos response:", err)
}
}
type videoQualityFilter func(pexels.VideoFile) bool
func filterVideoQuality(videos []pexels.VideoFile, f videoQualityFilter) (filtered []pexels.VideoFile) {
for _, v := range videos {
if f(v) {
filtered = append(filtered, v)
}
}
return
}
func getBestVideoLink(files []pexels.VideoFile, quality string) (bestLink pexels.VideoFile) {
var bestQuality int
var highest bool
if quality == QualityHigh || quality == QualityStandard {
bestQuality = 0
highest = true
} else {
bestQuality = math.MaxInt64
highest = false
}
for _, vf := range files {
if highest && vf.Width > bestQuality || !highest && vf.Width < bestQuality {
bestQuality = vf.Width
bestLink = vf
}
}
return
}
func getBestImageLink(src pexels.Source, quality string) string {
if quality == QualityLarge {
return src.Large
} else if quality == QualitySmall {
return src.Small
} else if quality == QualityTiny {
return src.Tiny
} else {
return src.Medium
}
}
func getTwitterHandle(userId string, users []twitter.User) (handle string) {
for _, u := range users {
if userId == u.Id {
handle = u.Username
return
}
}
return "404HandleNotFound"
}
func getTwitterMedia(tweet twitter.Tweet) (mediaUrl *string) {
if len(tweet.Includes.Media) > 0 {
urlString := tweet.Includes.Media[0].Url
mediaUrl = &urlString
}
return
}