Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
fix: wrong content-type for streams
Browse files Browse the repository at this point in the history
  • Loading branch information
keshon committed Mar 12, 2024
1 parent 31070d8 commit 5a026a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
29 changes: 16 additions & 13 deletions mod-music/sources/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ import (

"github.com/gookit/slog"

"github.com/keshon/melodix-player/internal/config"
"github.com/keshon/melodix-player/mod-music/player"
)

// Stream is a struct that encapsulates the Stream functionality.
type Stream struct{}

// NewStream creates a new instance of stream.
func NewStream() *Stream {
return &Stream{}
}

// FetchStreamsByURLs fetches stream URLs into Song struct.
func (s *Stream) FetchStreamsByURLs(urls []string) ([]*player.Song, error) {
var songs []*player.Song

Expand All @@ -35,7 +33,6 @@ func (s *Stream) FetchStreamsByURLs(urls []string) ([]*player.Song, error) {
// Use CRC32 to hash name as unique id
hash := crc32.ChecksumIEEE([]byte(u.Host))

// Fetch the stream and check the content type
contentType, err := getContentType(u.String())
if err != nil {
slog.Errorf("Error fetching content type: %v", err)
Expand All @@ -49,7 +46,7 @@ func (s *Stream) FetchStreamsByURLs(urls []string) ([]*player.Song, error) {
DownloadURL: u.String(),
Thumbnail: player.Thumbnail{},
Duration: -1,
ID: fmt.Sprintf("%d", hash), // Convert hash to string
ID: fmt.Sprintf("%d", hash),
Source: player.SourceStream,
}
songs = append(songs, song)
Expand All @@ -61,21 +58,30 @@ func (s *Stream) FetchStreamsByURLs(urls []string) ([]*player.Song, error) {
return songs, nil
}

// getContentType fetches the content type of a given URL.
func getContentType(url string) (string, error) {
resp, err := http.Head(url)
req, err := http.NewRequest(http.MethodHead, url, nil)
if err != nil {
return "", err
return "", fmt.Errorf("error creating request: %v", err)
}

conf, err := config.NewConfig()
if err != nil {
return "", fmt.Errorf("error loading config: %v", err)
}

req.Header.Set("User-Agent", conf.DcaUserAgent)

resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()

return resp.Header.Get("Content-Type"), nil
}

func isValidStream(contentType string) bool {
// List of common audio and video content types for radio streams
validContentTypes := []string{
// Audio content types
"application/flv",
"application/vnd.ms-wpl",
"audio/aac",
Expand All @@ -98,7 +104,6 @@ func isValidStream(contentType string) bool {
"audio/x-pn-realaudio",
"audio/x-scpls",
"audio/x-wav",
// Video content types
"video/3gpp",
"video/mp4",
"video/quicktime",
Expand All @@ -109,13 +114,11 @@ func isValidStream(contentType string) bool {
"video/x-ms-asf",
}

// Check if the content type is in the list of valid content types
for _, validType := range validContentTypes {
if contentType == validType {
return true
}
}

// If the content type is not in the list, consider it not valid stream
return false
}
18 changes: 9 additions & 9 deletions mod-music/sources/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (y *Youtube) getVideoURLFromTitle(title string) (string, error) {
return url, nil
}

return "", fmt.Errorf("No video found for the given title")
return "", fmt.Errorf("no video found for the given title")
}

// FetchSongsByIDs fetches songs by their IDs from the history.
Expand All @@ -170,12 +170,12 @@ func (y *Youtube) FetchSongsByIDs(guildID string, ids []int) ([]*player.Song, er
for _, id := range ids {
track, err := h.GetTrackFromHistory(guildID, uint(id))
if err != nil {
return nil, fmt.Errorf("Error getting track from history with ID %v", id)
return nil, fmt.Errorf("error getting track from history with ID %v", id)
}

song, err := y.getAllSongsFromURL(track.URL)
if err != nil {
return nil, fmt.Errorf("Error fetching new songs from URL: %v", err)
return nil, fmt.Errorf("error fetching new songs from URL: %v", err)
}

songs = append(songs, song...)
Expand All @@ -191,12 +191,12 @@ func (y *Youtube) FetchSongsByTitles(titles []string) ([]*player.Song, error) {
for _, title := range titles {
url, err := y.getVideoURLFromTitle(title)
if err != nil {
return nil, fmt.Errorf("Error getting YouTube video URL by title: %v", err)
return nil, fmt.Errorf("error getting YouTube video URL by title: %v", err)
}

songs, err = y.getAllSongsFromURL(url)
if err != nil {
return nil, fmt.Errorf("Error fetching new songs from URL: %v", err)
return nil, fmt.Errorf("error fetching new songs from URL: %v", err)
}
}

Expand All @@ -209,12 +209,12 @@ func (y *Youtube) FetchSongsByTitle(title string) ([]*player.Song, error) {

url, err := y.getVideoURLFromTitle(title)
if err != nil {
return nil, fmt.Errorf("Error getting YouTube video URL by title: %v", err)
return nil, fmt.Errorf("error getting YouTube video URL by title: %v", err)
}

songs, err = y.getAllSongsFromURL(url)
if err != nil {
return nil, fmt.Errorf("Error fetching new songs from URL: %v", err)
return nil, fmt.Errorf("error fetching new songs from URL: %v", err)
}

return songs, nil
Expand All @@ -227,7 +227,7 @@ func (y *Youtube) FetchSongsByURLs(urls []string) ([]*player.Song, error) {
for _, url := range urls {
song, err := y.getAllSongsFromURL(url)
if err != nil {
return nil, fmt.Errorf("Error fetching new songs from URL: %v", err)
return nil, fmt.Errorf("error fetching new songs from URL: %v", err)
}

songs = append(songs, song...)
Expand All @@ -242,7 +242,7 @@ func (y *Youtube) FetchSongByURLs(url string) ([]*player.Song, error) {

song, err := y.getAllSongsFromURL(url)
if err != nil {
return nil, fmt.Errorf("Error fetching new songs from URL: %v", err)
return nil, fmt.Errorf("error fetching new songs from URL: %v", err)
}

songs = append(songs, song...)
Expand Down

0 comments on commit 5a026a9

Please sign in to comment.