dumb/search.go

65 lines
1.1 KiB
Go
Raw Normal View History

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
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)
2024-01-20 00:00:53 +03:30
err = d.Decode(&data)
if err != nil {
logger.Errorln(err)
}
2023-03-09 17:11:37 +01:00
vars := renderVars{query, data.Response.Sections}
render("search", w, vars)
}