-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
347 lines (307 loc) · 8.87 KB
/
main.go
File metadata and controls
347 lines (307 loc) · 8.87 KB
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
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"sort"
"strconv"
"strings"
fetch "./src"
)
type Location struct {
Lat float64 `json:"Lat"`
Lng float64 `json:"Lng"`
Name string `json:"Name"`
Dates string `json:"Dates"`
}
var (
loca []Location
Data []fetch.Artist
loc []string
errr error
)
const port = ":8080"
func main() {
Initiate()
if errr != nil {
log.Fatal("Error fetching artist data:", errr)
}
mux := http.NewServeMux()
mux.Handle("/style/", http.StripPrefix("/style/", http.FileServer(http.Dir("style"))))
mux.HandleFunc("/", Index)
mux.HandleFunc("/artist/", ArtistP)
mux.HandleFunc("/search", Search)
mux.HandleFunc("/filter", Filter)
http.HandleFunc("/getdata", func(w http.ResponseWriter, r *http.Request) {
// Marshal the struct into JSON
jsonData, err := json.Marshal(loca)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Set the response content type and send the JSON
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(jsonData)
})
fmt.Printf("(http://localhost%v): Server started on port\n", port)
if err := http.ListenAndServe(port, mux); err != nil {
log.Fatal(err)
}
}
// func Geo(w http.ResponseWriter, r *http.Request) {
// // Replace "YourApiKeyHere" with your actual Google Maps API key
// apiKey := "AIzaSyAbgRjHe8P_pbCsi7dJkaTJqTll3R01ogo"
// // Simulated location names, replace with your actual location names
// var locationsToGeocode []string = Data[id-1].Locations.Locations
// // Convert location names to coordinates using the Google Maps Geocoding API
// locations := []Location{}
// for _, locationName := range locationsToGeocode {
// lat, lng, err := GeocodeLocation(locationName, apiKey)
// if err != nil {
// log.Printf("Error geocoding location %s: %v", locationName, err)
// continue
// }
// locations = append(locations, Location{Lat: lat, Lng: lng, Name: locationName})
// }
// fmt.Println(locations)
// w.Header().Set("Content-Type", "application/json")
// json.NewEncoder(w).Encode(locations)
// }
func Initiate() {
Data, errr = fetch.GetArtist()
var zebi []string
for _, art := range Data {
for _, memb := range art.Locations.Locations {
zeb := strings.Replace(memb, "_", " ", -1)
klwa := strings.Split(zeb, "-")
zeboklwa := strings.Title(klwa[0]) + ", " + strings.ToUpper(klwa[1])
zebi = append(zebi, zeboklwa)
}
}
for i := range zebi {
duplicate := false
for j := range loc {
if zebi[i] == loc[j] {
duplicate = true
break
}
}
if !duplicate {
loc = append(loc, zebi[i])
}
}
sort.Strings(loc)
}
func Index(w http.ResponseWriter, r *http.Request) {
k := template.Must(template.ParseFiles("./templates/404.html"))
if r.URL.Path != "/" {
k.Execute(w, struct {
Value []fetch.Artist
Zebi []string
}{Data, loc})
return
}
t := template.Must(template.ParseFiles("./templates/index.html"))
if r.Method != http.MethodGet {
http.Redirect(w, r, "/", http.StatusSeeOther)
}
fmt.Println(loca)
t.Execute(w, struct {
Succes bool
Value []fetch.Artist
Zebi []string
}{true, Data, loc})
}
func ArtistP(w http.ResponseWriter, r *http.Request) {
// Extract the dynamic part of the URL
t := template.Must(template.ParseFiles("./templates/htmllll.html"))
k := template.Must(template.ParseFiles("./templates/404.html"))
pathParts := strings.Split(r.URL.Path, "/")
if len(pathParts) == 3 {
id, err := strconv.Atoi(pathParts[2])
if id > len(Data) || err != nil {
k.Execute(w, struct {
Value []fetch.Artist
Zebi []string
}{Data, loc})
return
}
apiKey := "AIzaSyAbgRjHe8P_pbCsi7dJkaTJqTll3R01ogo"
// Simulated location names, replace with your actual location names
var locationsToGeocode []string = Data[id-1].Locations.Locations
// Convert location names to coordinates using the Google Maps Geocoding API
locations := []Location{}
for _, locationName := range locationsToGeocode {
lat, lng, err := GeocodeLocation(locationName, apiKey)
if err != nil {
log.Printf("Error geocoding location %s: %v", locationName, err)
continue
}
locations = append(locations, Location{Lat: lat, Lng: lng, Name: locationName, Dates: strings.Join(Data[id-1].Relations.DatesLocations[locationName], ", ")})
}
stringify, _ := json.Marshal(locations)
// w.Header().Set("Content-Type", "application/json")
// json.NewEncoder(w).Encode(locations)
fmt.Println(string(stringify))
t.Execute(w, struct {
Succes bool
Avalues []fetch.Artist
Value fetch.Artist
Zebi []string
Lase9 string
}{true, Data, Data[id-1], loc, string(stringify)})
}
}
func Search(w http.ResponseWriter, r *http.Request) {
// Extract the search query from the URL query parameters
query := r.URL.Query().Get("q")
if query == "" {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
found := false
// Perform a search operation based on the query
var searchResults []fetch.Artist
for _, artist := range Data {
if strings.Contains(strings.ToLower(artist.Name), strings.ToLower(query)) {
searchResults = append(searchResults, artist)
found = true
continue
}
i, errrr := strconv.Atoi(query)
if i == artist.CreationDate && errrr == nil {
searchResults = append(searchResults, artist)
found = true
continue
}
for _, loc := range artist.Locations.Locations {
if strings.Contains(strings.ToLower(loc), strings.ToLower(query)) {
searchResults = append(searchResults, artist)
found = true
continue
}
}
if strings.Contains(strings.ToLower(artist.FirstAlbum), strings.ToLower(query)) {
searchResults = append(searchResults, artist)
found = true
continue
}
for _, members := range artist.Members {
if strings.Contains(strings.ToLower(members), strings.ToLower(query)) {
searchResults = append(searchResults, artist)
found = true
continue
}
}
}
// Render the search results in your HTML template
t := template.Must(template.ParseFiles("./templates/search_results.html"))
t.Execute(w, struct {
Success bool
Query string
Results []fetch.Artist
Zebi []string
Value []fetch.Artist
}{found, query, searchResults, loc, Data})
}
func Fix(s string) string {
s = strings.Replace(s, ", ", "-", -1)
s = strings.Replace(s, " ", "_", -1)
s = strings.ToLower(s)
return s
}
func Filter(w http.ResponseWriter, r *http.Request) {
start := r.FormValue("first-album-start")
end := r.FormValue("first-album-end")
members := r.Form["Members"]
location := r.FormValue("Country")
creationstart, _ := strconv.Atoi(r.FormValue("minC"))
creationend, _ := strconv.Atoi(r.FormValue("maxC"))
found := false
var filterresults, creationresult, membersresult, locationresult []fetch.Artist
for _, artist := range Data {
if start != "" {
if fetch.Compare(start, end, artist.FirstAlbum) {
filterresults = append(filterresults, artist)
}
} else {
filterresults = Data
}
}
if location == "" {
locationresult = filterresults
} else {
location = Fix(location)
for _, artist := range filterresults {
for _, loccccc := range artist.Locations.Locations {
if loccccc == location {
locationresult = append(locationresult, artist)
break
}
}
}
}
if members == nil {
membersresult = locationresult
} else {
for _, artist := range locationresult {
for _, m := range members {
mm, _ := strconv.Atoi(m)
if mm == len(artist.Members) {
membersresult = append(membersresult, artist)
break
}
}
}
}
for _, artist := range membersresult {
if artist.CreationDate >= creationstart && artist.CreationDate <= creationend {
creationresult = append(creationresult, artist)
}
}
if creationresult != nil {
found = true
}
t := template.Must(template.ParseFiles("./templates/search_results.html"))
t.Execute(w, struct {
Success bool
Query string
Results []fetch.Artist
Zebi []string
Value []fetch.Artist
}{found, "zebi", creationresult, loc, Data})
}
func GeocodeLocation(locationName, apiKey string) (float64, float64, error) {
// Make a request to the Google Maps Geocoding API
resp, err := http.Get("https://maps.googleapis.com/maps/api/geocode/json?address=" + locationName + "&key=" + apiKey)
if err != nil {
return 0, 0, err
}
defer resp.Body.Close()
// Parse the response
var geocodeResponse struct {
Results []struct {
Geometry struct {
Location struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"location"`
} `json:"geometry"`
} `json:"results"`
}
err = json.NewDecoder(resp.Body).Decode(&geocodeResponse)
if err != nil {
return 0, 0, err
}
// Check if any results were found
if len(geocodeResponse.Results) == 0 {
return 0, 0, nil // No results found
}
// Extract the coordinates from the first result
lat := geocodeResponse.Results[0].Geometry.Location.Lat
lng := geocodeResponse.Results[0].Geometry.Location.Lng
fmt.Println(lat, lng)
return lat, lng, nil
}