Skip to content

Commit

Permalink
change platform label to hostnames
Browse files Browse the repository at this point in the history
  • Loading branch information
keshon committed Jan 20, 2025
1 parent 8d55baa commit ac7d4c7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 20 deletions.
9 changes: 8 additions & 1 deletion cmd/melodix/melodix.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,14 @@ func (b *Bot) onMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate)
return
}
for index, song := range songs {
hostname, err := extractHostname(song.PublicLink)
if err != nil {
hostname = song.Source.String()
}

emb.Fields = append(emb.Fields, &discordgo.MessageEmbedField{
Name: fmt.Sprintf("%d. %s", index+1, song.Title),
Value: fmt.Sprintf("[%s](%s)", song.Source, song.PublicLink),
Value: fmt.Sprintf("[%s](%s)", hostname, song.PublicLink),
Inline: false,
})
}
Expand Down Expand Up @@ -417,6 +422,8 @@ func (b *Bot) onPlayback(s *discordgo.Session, m *discordgo.MessageCreate) {
}

go func() {
time.Sleep(250 * time.Millisecond)

var currentChannelID string
if b.playChannelID[m.GuildID] != "" {
currentChannelID = b.playChannelID[m.GuildID]
Expand Down
8 changes: 5 additions & 3 deletions parsers/ytdlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,23 @@ func (y *YtdlpWrapper) GetMetaInfo(url string) (Meta, error) {
return meta, nil
}

func (y *YtdlpWrapper) GetStream(url string) (*ytdlp.Result, string, error) {
func (y *YtdlpWrapper) DownloadStream(url string) (*ytdlp.Result, string, error) {
cacheDir := "./cache"

if err := os.MkdirAll(cacheDir, 0755); err != nil {
return nil, "", fmt.Errorf("failed to create cache directory: %w", err)
}

timestamp := time.Now().Format("20060102_150405")
outputFile := filepath.Join(cacheDir, timestamp+".webm")
outputFile := filepath.Join(cacheDir, timestamp)

dl := ytdlp.New().
FormatSort("res,ext:webm:webm").
// FormatSort("acodec:opus").
NoPart().
NoPlaylist().
NoOverwrites().
NoKeepVideo().
Format("bestaudio").
Output(outputFile)

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
Expand Down
55 changes: 43 additions & 12 deletions player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"fmt"
"io"
"net/url"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"

"github.com/bwmarrin/discordgo"
Expand Down Expand Up @@ -143,7 +145,7 @@ PLAYBACK_LOOP:
return
}

output, path, err := parsers.NewYtdlpWrapper().GetStream(p.Song.PublicLink) // we can use kkdai for better speed
output, path, err := parsers.NewYtdlpWrapper().DownloadStream(p.Song.PublicLink)
if err != nil {
fmt.Printf("Error caching: %v\n", err)
return
Expand All @@ -154,7 +156,7 @@ PLAYBACK_LOOP:
if output.ExitCode == 0 {
time.Sleep(5 * time.Second)
if p.Song != nil {
p.Song.StreamURL = path + ".mp4" //nasty fix
p.Song.StreamURL = path
cacheReady <- true
} else {
cacheReady <- false
Expand All @@ -166,6 +168,7 @@ PLAYBACK_LOOP:
}
}

time.Sleep(250 * time.Millisecond)
encoding, err := dca.EncodeFile(streamPath, options)
if err != nil {
return err
Expand Down Expand Up @@ -195,7 +198,11 @@ PLAYBACK_LOOP:
}

if startAt == 0 {
err := p.Storage.AddTrackCountByOne(p.GuildID, p.Song.SongID, p.Song.Title, p.Song.Source.String(), p.Song.PublicLink)
hostname, err := extractHostname(p.Song.PublicLink)
if err != nil {
hostname = p.Song.Source.String()
}
err = p.Storage.AddTrackCountByOne(p.GuildID, p.Song.SongID, p.Song.Title, hostname, p.Song.PublicLink)
if err != nil {
return err
}
Expand All @@ -214,7 +221,11 @@ PLAYBACK_LOOP:
return
case <-ticker.C:
if p.Song != nil { // or it will crash on stop signal
err := p.Storage.AddTrackDuration(p.GuildID, p.Song.SongID, p.Song.Title, p.Song.Source.String(), p.Song.PublicLink, 2*time.Second)
hostname, err := extractHostname(p.Song.PublicLink)
if err != nil {
hostname = p.Song.Source.String()
}
err = p.Storage.AddTrackDuration(p.GuildID, p.Song.SongID, p.Song.Title, hostname, p.Song.PublicLink, 2*time.Second)
if err != nil {
fmt.Printf("Error saving track duration: %v\n", err)
}
Expand All @@ -225,17 +236,19 @@ PLAYBACK_LOOP:

for {
select {
case err := <-done:
case doneError := <-done:
close(done)

fmt.Println("Playback got done signal")

// stop if there is an error
if err != nil && err != io.EOF {
if doneError != nil && doneError != io.EOF {
p.StatusSignals <- StatusError
return err
return doneError
}
// restart if there is an interrupt
duration, position, errPlaybackDuration := p.getPlaybackDuration(encoding, streaming, p.Song)
if errPlaybackDuration != nil {
duration, position, err := p.getPlaybackDuration(encoding, streaming, p.Song)
if err != nil {
p.StatusSignals <- StatusError
return err
}
Expand All @@ -250,11 +263,13 @@ PLAYBACK_LOOP:
startAt = position
continue PLAYBACK_LOOP
}

if position.Seconds() == 0.0 {
fmt.Printf("Playback could not be started: \"%v\"\n", p.Song.Title)
p.StatusSignals <- StatusError
return fmt.Errorf("playback could not be started: %v", err)
return fmt.Errorf("playback could not be started: %v", doneError)
}

// skip to the next song
if len(p.Queue) > 0 {
startAt = 0
Expand All @@ -264,18 +279,20 @@ PLAYBACK_LOOP:
isCached = false
continue PLAYBACK_LOOP
}

// finished
fmt.Printf("Finished playback of \"%v\"", p.Song.Title)
return nil
case <-cacheReady: // Cache is ready
case <-cacheReady:
fmt.Println("Switching to cached playback")

_, position, err := p.getPlaybackDuration(encoding, streaming, p.Song)
if err != nil {
p.StatusSignals <- StatusError
return err
}

isCached = true
//encoding.Stop()
encoding.Cleanup()
startAt = position
continue PLAYBACK_LOOP
Expand Down Expand Up @@ -398,3 +415,17 @@ func (p *Player) fetchMP3Duration(filePath string) (time.Duration, error) {

return totalDuration, nil
}

func extractHostname(rawURL string) (string, error) {
parsedURL, err := url.Parse(rawURL)
if err != nil {
return "", err
}

hostname := parsedURL.Hostname()

// Remove 'www.' prefix if present
hostname = strings.TrimPrefix(hostname, "www.")

return hostname, nil
}
12 changes: 8 additions & 4 deletions song/song.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ func (s *Song) fetchSongsByURLs(urlsInput string) ([]*Song, error) {
var songs []*Song
results := make(chan *Song, len(urls)) // Buffered channel to collect songs
errs := make(chan error, len(urls)) // Buffered channel to collect errors
var wg sync.WaitGroup // WaitGroup for managing concurrency
var wg sync.WaitGroup

ytdlp := parsers.NewYtdlpWrapper()
kkdai := parsers.NewKkdaiWrapper()

Expand Down Expand Up @@ -187,9 +188,12 @@ func (s *Song) fetchPlatformSong(ytdlp *parsers.YtdlpWrapper, kkdai *parsers.Kkd
streamURL, meta, err = kkdai.GetStreamURL(url)
parser = ParserKkdai
if err != nil || streamURL == "" {
fmt.Println("Failed to get stream URL from kkdai, trying yt-dlp...")
fmt.Printf("Error: %s\n", err)
fmt.Printf("Stream URL: %s\n", streamURL)
fmt.Println("Failed to parse URL with kkdai, trying yt-dlp...")
if err != nil {
fmt.Printf("Error: %s\n", err)
} else {
fmt.Println("Couldn't get stream URL")
}

streamURL, err = ytdlp.GetStreamURL(url)
if err != nil {
Expand Down

0 comments on commit ac7d4c7

Please sign in to comment.