mirror of
https://github.com/nxshock/promodj.git
synced 2024-11-27 03:01:01 +05:00
Add TOP-100 playlist
This commit is contained in:
parent
c6346cadbf
commit
f9fe602c15
20
handlers.go
20
handlers.go
@ -2,7 +2,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleGenres(w http.ResponseWriter, r *http.Request) {
|
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) {
|
func handleGetM3u(w http.ResponseWriter, r *http.Request) {
|
||||||
genreCode := r.FormValue("genre")
|
genreCode := r.FormValue("genre")
|
||||||
if genreCode == "" {
|
params := url.Values{}
|
||||||
http.Error(w, `"genre" field is not specified`, http.StatusBadRequest)
|
|
||||||
return
|
if r.FormValue("top100") != "" {
|
||||||
|
params.Set("top100", "1")
|
||||||
}
|
}
|
||||||
|
|
||||||
tracks, err := tracksByGenre(genreCode)
|
tracks, err := tracksByGenre(genreCode, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@ -39,6 +42,10 @@ func handleGetM3u(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
b := tracksToM3u(r.Host, tracks)
|
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-Disposition", fmt.Sprintf(`attachment; filename="%s.m3u8"`, genreCode))
|
||||||
w.Header().Set("Content-Type", "audio/x-mpegurl")
|
w.Header().Set("Content-Type", "audio/x-mpegurl")
|
||||||
w.Header().Set("Accept-Ranges", "none")
|
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) {
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
31
parser.go
31
parser.go
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/PuerkitoBio/goquery"
|
"github.com/PuerkitoBio/goquery"
|
||||||
@ -52,11 +53,19 @@ func updateGenreList() ([]Genre, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// tracksByGenre возвращает список треков по указанному жанру
|
// 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
|
var result []TrackInfo
|
||||||
|
|
||||||
for i := 1; i <= 50; i++ {
|
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)
|
doc, err := goquery.NewDocument(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -118,3 +127,21 @@ func removeDuplicate(strSlice []TrackInfo) []TrackInfo {
|
|||||||
}
|
}
|
||||||
return list
|
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()
|
||||||
|
}
|
||||||
|
23
parser_test.go
Normal file
23
parser_test.go
Normal file
@ -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))
|
||||||
|
}
|
||||||
|
}
|
@ -15,6 +15,7 @@
|
|||||||
<main>
|
<main>
|
||||||
<h1>Список жанров</h1>
|
<h1>Список жанров</h1>
|
||||||
<ul id="genres-list">
|
<ul id="genres-list">
|
||||||
|
<li><a href="/getm3u?top100=1">TOP 100</a></li>
|
||||||
{{range .Genres}}
|
{{range .Genres}}
|
||||||
<li><a href="/getm3u?genre={{.Code}}">{{.Name}}</a></li>
|
<li><a href="/getm3u?genre={{.Code}}">{{.Name}}</a></li>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
Loading…
Reference in New Issue
Block a user