From f9fe602c1566bfd5618a5e997d7c200d82df0efe Mon Sep 17 00:00:00 2001 From: nxshock Date: Tue, 2 Nov 2021 16:13:23 +0500 Subject: [PATCH] Add TOP-100 playlist --- handlers.go | 20 +++++++++++++++----- parser.go | 31 +++++++++++++++++++++++++++++-- parser_test.go | 23 +++++++++++++++++++++++ templates/index.htm | 1 + 4 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 parser_test.go diff --git a/handlers.go b/handlers.go index 2ea3d64..2787b41 100644 --- a/handlers.go +++ b/handlers.go @@ -2,7 +2,9 @@ package main import ( "fmt" + "log" "net/http" + "net/url" ) func handleGenres(w http.ResponseWriter, r *http.Request) { @@ -26,12 +28,13 @@ func handleGenres(w http.ResponseWriter, r *http.Request) { func handleGetM3u(w http.ResponseWriter, r *http.Request) { genreCode := r.FormValue("genre") - if genreCode == "" { - http.Error(w, `"genre" field is not specified`, http.StatusBadRequest) - return + params := url.Values{} + + if r.FormValue("top100") != "" { + params.Set("top100", "1") } - tracks, err := tracksByGenre(genreCode) + tracks, err := tracksByGenre(genreCode, params) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -39,6 +42,10 @@ func handleGetM3u(w http.ResponseWriter, r *http.Request) { b := tracksToM3u(r.Host, tracks) + if genreCode == "" { + genreCode = "music" + } + 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") @@ -47,5 +54,8 @@ func handleGetM3u(w http.ResponseWriter, r *http.Request) { } func handleStream(w http.ResponseWriter, r *http.Request) { - stream(r.FormValue("url"), w) + err := stream(r.FormValue("url"), w) + if err != nil { + log.Println(err) + } } diff --git a/parser.go b/parser.go index 9d230c6..f1c983d 100644 --- a/parser.go +++ b/parser.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "net/url" + "strconv" "strings" "github.com/PuerkitoBio/goquery" @@ -52,11 +53,19 @@ func updateGenreList() ([]Genre, error) { } // tracksByGenre возвращает список треков по указанному жанру -func tracksByGenre(genre string) ([]TrackInfo, error) { +func tracksByGenre(genre string, params url.Values) ([]TrackInfo, error) { + if params == nil { + params = url.Values{"download": []string{"1"}} + } else { + params.Set("download", "1") // only available tracks + } + var result []TrackInfo for i := 1; i <= 50; i++ { - url := fmt.Sprintf("https://promodj.com/music/%s?download=1&page=%d", genre, i) + params.Set("page", strconv.Itoa(i)) + //url := fmt.Sprintf("https://promodj.com/music/%s?download=1&page=%d", genre, i) + url := constructUrl(genre, params) doc, err := goquery.NewDocument(url) if err != nil { @@ -118,3 +127,21 @@ func removeDuplicate(strSlice []TrackInfo) []TrackInfo { } return list } + +func constructUrl(genre string, params url.Values) string { + urlTemplate := fmt.Sprintf("https://promodj.com/music/%s", genre) + + if genre == "" { + urlTemplate = "https://promodj.com/music" + } + + u, err := url.Parse(urlTemplate) + if err != nil { + panic(err) + } + + u.RawQuery = params.Encode() + + //?download=1&page=%d + return u.String() +} diff --git a/parser_test.go b/parser_test.go new file mode 100644 index 0000000..5228bba --- /dev/null +++ b/parser_test.go @@ -0,0 +1,23 @@ +package main + +import ( + "net/url" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestConstructURL(t *testing.T) { + tests := []struct { + params url.Values + expected string + }{ + {nil, "https://promodj.com/music/testGenre"}, + {url.Values{"download": []string{"1"}}, "https://promodj.com/music/testGenre?download=1"}, + {url.Values{"download": []string{"1"}, "page": []string{"1"}}, "https://promodj.com/music/testGenre?download=1&page=1"}, + } + + for _, test := range tests { + assert.Equal(t, test.expected, constructUrl("testGenre", test.params)) + } +} diff --git a/templates/index.htm b/templates/index.htm index a305507..20b05fd 100644 --- a/templates/index.htm +++ b/templates/index.htm @@ -15,6 +15,7 @@

Список жанров