2023-03-09 17:11:37 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-03-13 20:18:43 +01:00
|
|
|
"net/url"
|
2023-03-09 17:11:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
Response struct {
|
|
|
|
Sections sections
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type result struct {
|
|
|
|
ArtistNames string `json:"artist_names"`
|
|
|
|
Title string
|
|
|
|
Path string
|
|
|
|
Thumbnail string `json:"song_art_image_thumbnail_url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type hits []struct {
|
|
|
|
Result result
|
|
|
|
}
|
|
|
|
|
|
|
|
type sections []struct {
|
|
|
|
Type string
|
|
|
|
Hits hits
|
|
|
|
}
|
|
|
|
|
|
|
|
type renderVars struct {
|
|
|
|
Query string
|
|
|
|
Sections sections
|
|
|
|
}
|
|
|
|
|
|
|
|
func searchHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
query := r.URL.Query().Get("q")
|
2023-03-13 20:18:43 +01:00
|
|
|
url := fmt.Sprintf(`https://genius.com/api/search/multi?q=%s`, url.QueryEscape(query))
|
2023-03-09 17:11:37 +01:00
|
|
|
|
2023-03-12 13:29:33 +01:00
|
|
|
res, err := sendRequest(url)
|
2023-03-09 17:11:37 +01:00
|
|
|
if err != nil {
|
|
|
|
logger.Errorln(err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
render("error", w, map[string]string{
|
|
|
|
"Status": "500",
|
|
|
|
"Error": "cannot reach genius servers",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
var data response
|
|
|
|
|
|
|
|
d := json.NewDecoder(res.Body)
|
|
|
|
d.Decode(&data)
|
|
|
|
|
|
|
|
vars := renderVars{query, data.Response.Sections}
|
|
|
|
|
|
|
|
render("search", w, vars)
|
|
|
|
}
|