promodj/handlers.go

98 lines
1.9 KiB
Go
Raw Normal View History

2021-08-14 09:59:41 +05:00
package main
import (
"fmt"
2021-11-02 16:13:23 +05:00
"log"
2021-08-14 09:59:41 +05:00
"net/http"
2021-11-02 16:13:23 +05:00
"net/url"
2021-08-14 09:59:41 +05:00
)
func handleGenres(w http.ResponseWriter, r *http.Request) {
if r.RequestURI != "/" {
2021-09-25 12:38:27 +05:00
http.FileServer(http.FS(stripSiteFS)).ServeHTTP(w, r)
2021-08-14 09:59:41 +05:00
return
}
data := struct {
Domain string
Genres []Genre
}{
Domain: r.Host,
Genres: Genres}
2021-09-25 12:38:27 +05:00
err := templates.Lookup("index.htm").Execute(w, data)
2021-08-14 09:59:41 +05:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
2022-07-02 16:20:06 +05:00
func handlePlayer(w http.ResponseWriter, r *http.Request) {
genreCode := r.FormValue("genre")
params := url.Values{}
if r.FormValue("top100") != "" {
params.Set("top100", "1")
}
tracks, err := tracksByGenre(genreCode, params)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
type J struct {
Title string
File string
}
var data []J
for _, track := range tracks {
2022-11-27 14:31:06 +05:00
u, _ := url.Parse("stream")
2022-07-02 16:20:06 +05:00
q := make(url.Values)
q.Add("url", track.Url)
u.RawQuery = q.Encode()
data = append(data, J{track.Title, u.String()})
}
err = templates.Lookup("player.htm").Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
2021-08-14 09:59:41 +05:00
func handleGetM3u(w http.ResponseWriter, r *http.Request) {
genreCode := r.FormValue("genre")
2021-11-02 16:13:23 +05:00
params := url.Values{}
if r.FormValue("top100") != "" {
params.Set("top100", "1")
2021-08-14 09:59:41 +05:00
}
2021-11-02 16:13:23 +05:00
tracks, err := tracksByGenre(genreCode, params)
2021-08-14 09:59:41 +05:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
b := tracksToM3u(r.Host, tracks)
2021-11-02 16:13:23 +05:00
if genreCode == "" {
genreCode = "music"
}
2021-08-14 09:59:41 +05:00
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.m3u8"`, genreCode))
w.Header().Set("Content-Type", "audio/x-mpegurl")
w.Header().Set("Accept-Ranges", "none")
2022-11-27 14:33:56 +05:00
_, _ = w.Write(b)
2021-08-14 09:59:41 +05:00
}
func handleStream(w http.ResponseWriter, r *http.Request) {
2021-11-02 16:13:23 +05:00
err := stream(r.FormValue("url"), w)
if err != nil {
log.Println(err)
}
2021-08-14 09:59:41 +05:00
}