/
globe.go
187 lines (164 loc) · 3.88 KB
/
globe.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
package main
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"time"
)
type AllApiData struct {
Images []string
Weather *WeatherData
}
type WeatherData struct {
Temp string
City string
Icon string
}
var dispdata AllApiData
var celsiusNum string
var imagesArray []string
var rainOrShine string
var RANDi int
var cityLibrary = []string{
"Tokyo",
"Paris",
"Singapore",
"London",
"Shanghai",
"Beijing",
"Seoul",
"Mumbai",
"Washington",
"Bangkok",
"Hanoi",
"Toronto",
"Atlanta",
"Rome",
"Milan",
"Edinburgh",
"Vienna",
"Prague",
"Stockholm",
"Vancouver",
"Barcelona",
"Sydney",
"Istanbul",
"Hokkaido",
"Santiago",
"Valencia",
"Peru",
"Moscow",
"Florence",
"Berlin",
"Auckland",
"Kyoto",
}
//API funcs
//doc for Flickr API: https://www.flickr.com/services/api/flickr.photos.search.html
func ImageDisplay() {
reqUrl := fmt.Sprintf("https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=%s&tags=%s&extras=url_m&format=json&nojsoncallback=1&sort=relevance",
os.Getenv("FLICKR_APIKEY"),
cityLibrary[RANDi])
client := &http.Client{}
req, err := http.NewRequest("GET", reqUrl, nil)
if err != nil {
log.Fatal("NewRequest: ", err)
return
}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Do: ", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("ReadAll: ", err)
return
}
type FlickrResponse struct {
Photos struct {
Photo []struct {
Id, Secret, Server string
Farm int
}
}
}
var f FlickrResponse
if err := json.Unmarshal(body, &f); err != nil {
log.Fatalf("unmarshal err: %v", err)
}
imagesArray = []string{} //resets previous array on click
v := rand.Perm(100)[:27] //get different numbers
for i := 0; i < 27; i++ {
b := v[i]
respUrl := "https://farm" + strconv.Itoa(f.Photos.Photo[b].Farm) + ".staticflickr.com/" + f.Photos.Photo[b].Server + "/" + f.Photos.Photo[b].Id + "_" + f.Photos.Photo[b].Secret + "_q.jpg"
imagesArray = append(imagesArray, respUrl)
}
}
//doc for weather API: http://openweathermap.org/weather-data#current
func WeatherDisplay() {
reqUrl := fmt.Sprintf("http://api.openweathermap.org/data/2.5/weather?q=%s&APPID=%s",
cityLibrary[RANDi],
os.Getenv("WEATHER_APIKEY"),
)
client := &http.Client{}
req, err := http.NewRequest("GET", reqUrl, nil)
if err != nil {
log.Fatal("NewRequest: ", err)
return
}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Do: ", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("ReadAll: ", err)
return
}
type WeatherResponse struct {
Main struct {
Temp float64
}
Weather []struct {
Icon string
}
}
var f WeatherResponse
if err := json.Unmarshal(body, &f); err != nil {
log.Fatalf("unmarshal weatherdisplay %v", err)
}
celsiusNum = fmt.Sprintf("%.1f", f.Main.Temp-273.15) //formula to get celsius
rainOrShine = fmt.Sprintf("http://openweathermap.org/img/w/%s.png", f.Weather[0].Icon)
}
//handler and template
func homeHandler(w http.ResponseWriter, r *http.Request) {
RANDi = rand.Intn(len(cityLibrary))
ImageDisplay()
WeatherDisplay()
dispdata = AllApiData{Images: imagesArray, Weather: &WeatherData{Temp: celsiusNum, City: cityLibrary[RANDi], Icon: rainOrShine}}
renderTemplate(w, "home", dispdata)
}
func renderTemplate(w http.ResponseWriter, tmpl string, structdata AllApiData) {
t := template.Must(template.New("image").ParseFiles("layout/home.html"))
if err := t.ExecuteTemplate(w, tmpl, structdata); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
func main() {
http.HandleFunc("/", homeHandler)
http.Handle("/layout/", http.StripPrefix("/layout/", http.FileServer(http.Dir("layout"))))
http.ListenAndServe(":"+os.Getenv("PORT"), nil)
}