Skip to content

Commit

Permalink
added a duration to songs (#45)
Browse files Browse the repository at this point in the history
* added a duration to songs

* broadcast who skipped a song. Fixes #41

* gofmt
  • Loading branch information
svenwiltink authored May 30, 2018
1 parent 9f716e8 commit e510ea1
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 32 deletions.
18 changes: 15 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,11 @@
[[constraint]]
branch = "master"
name = "google.golang.org/api"

[[constraint]]
branch = "master"
name = "github.com/svenwiltink/youtube-dl"

[[constraint]]
branch = "master"
name = "github.com/ChannelMeter/iso8601duration"
35 changes: 24 additions & 11 deletions bot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/svenwiltink/go-musicbot/music"
"strconv"
"time"
)

type Command struct {
Expand Down Expand Up @@ -44,9 +45,9 @@ var addCommand = &Command{
bot.ReplyToMessage(message, err.Error())
} else {
if message.IsPrivate {
bot.BroadcastMessage(fmt.Sprintf("%s added by %s", song.Name, message.Sender.NickName))
bot.BroadcastMessage(fmt.Sprintf("%s - %s added by %s", song.Artist, song.Name, message.Sender.NickName))
}
bot.ReplyToMessage(message, fmt.Sprintf("%s added", song.Name))
bot.ReplyToMessage(message, fmt.Sprintf("%s - %s added", song.Artist, song.Name))
}
},
}
Expand All @@ -60,26 +61,30 @@ var searchAddCommand = &Command{
return
}

songs, _ := bot.musicPlayer.Search(words[2])
songs, err := bot.musicPlayer.Search(words[2])

if err != nil {
bot.ReplyToMessage(message, fmt.Sprintf("error: %v", err))
}

if len(songs) == 0 {
bot.ReplyToMessage(message, "No song found")
return
}

song := songs[0]
err := bot.musicPlayer.AddSong(song)
err = bot.musicPlayer.AddSong(song)

if err != nil {
bot.ReplyToMessage(message, fmt.Sprintf("Error: %v", err))
return
}

if message.IsPrivate {
bot.BroadcastMessage(fmt.Sprintf("%s added by %s", song.Name, message.Sender.NickName))
bot.BroadcastMessage(fmt.Sprintf("%s - %s added by %s", song.Artist, song.Name, message.Sender.NickName))
}

bot.ReplyToMessage(message, fmt.Sprintf("%s added", song.Name))
bot.ReplyToMessage(message, fmt.Sprintf("%s - %s added", song.Artist, song.Name))
},
}

Expand All @@ -91,7 +96,7 @@ var nextCommand = &Command{
bot.ReplyToMessage(message, fmt.Sprintf("Could not skip song: %v", err))
} else {
if message.IsPrivate {
bot.BroadcastMessage("Skipping song")
bot.BroadcastMessage(fmt.Sprintf("%s skipped the song", message.Sender.NickName))
}
bot.ReplyToMessage(message, "Skipping song")
}
Expand Down Expand Up @@ -137,12 +142,21 @@ var playCommand = &Command{
var currentCommand = &Command{
Name: "current",
Function: func(bot *MusicBot, message Message) {
song := bot.musicPlayer.GetCurrentSong()
song, durationLeft := bot.musicPlayer.GetCurrentSong()
if song == nil {
bot.ReplyToMessage(message, "Nothing currently playing")
return
}
bot.ReplyToMessage(message, fmt.Sprintf("Current song: %s %s", song.Artist, song.Name))

if song.SongType == music.SongTypeSong {
bot.ReplyToMessage(
message,
fmt.Sprintf("Current song: %s %s. %s remaining (%s)", song.Artist, song.Name, durationLeft.String(), song.Duration.Round(time.Second).String()))
} else {
bot.ReplyToMessage(
message,
fmt.Sprintf("Current song: %s %s. This is a livestream, use the next command to skip", song.Artist, song.Name))
}
},
}

Expand Down Expand Up @@ -240,7 +254,6 @@ var volCommand = &Command{
}
}


if message.IsPrivate {
bot.BroadcastMessage(fmt.Sprintf("Volume set to %d by %s", volume, message.Sender.NickName))
}
Expand All @@ -254,4 +267,4 @@ var aboutCommand = &Command{
Function: func(bot *MusicBot, message Message) {
bot.ReplyToMessage(message, "go-MusicBot by Sven Wiltink: https://github.com/svenwiltink/go-MusicBot")
},
}
}
3 changes: 2 additions & 1 deletion music/dataprovider/nts/nts.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func (DataProvider) ProvideData(song *music.Song) error {
link = link + `2`
}

song.Artist = song.Path
song.Name = song.Path
song.Artist = "nts"
song.Path = link
song.SongType = music.SongTypeStream

Expand Down
14 changes: 10 additions & 4 deletions music/dataprovider/soundcloud/soundcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"regexp"

"github.com/svenwiltink/go-musicbot/music"
"github.com/svenwiltink/youtube-dl"
"time"
)

var soundCloudRegex = regexp.MustCompile(`^https://soundcloud.com/([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)`)
Expand All @@ -15,10 +17,14 @@ func (DataProvider) CanProvideData(song *music.Song) bool {
}

func (DataProvider) ProvideData(song *music.Song) error {
matches := soundCloudRegex.FindStringSubmatch(song.Path)

song.Artist = matches[1]
song.Name = matches[2]
data, err := youtubedl.GetMetaData(song.Path)
if err != nil {
return err
}

song.Artist = data.Uploader
song.Name = data.Title
song.Duration = time.Second * time.Duration(data.Duration)

return nil
}
Expand Down
13 changes: 12 additions & 1 deletion music/dataprovider/youtube/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"regexp"
"strings"

isoduration "github.com/ChannelMeter/iso8601duration"
"github.com/svenwiltink/go-musicbot/music"
"google.golang.org/api/googleapi/transport"
youtube "google.golang.org/api/youtube/v3"
"google.golang.org/api/youtube/v3"
)

const (
Expand Down Expand Up @@ -83,6 +84,16 @@ func (provider *DataProvider) provideDataForIdentifier(identifier string, song *
}

song.Name = item.Snippet.Title
song.Artist = item.Snippet.ChannelTitle

duration, err := isoduration.FromString(item.ContentDetails.Duration)

if err != nil {
return err
}

song.Duration = duration.ToDuration()

song.Path = fmt.Sprintf(youTubeVideoURL, identifier)
return nil
}
Expand Down
7 changes: 5 additions & 2 deletions music/player.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package music

import "github.com/vansante/go-event-emitter"
import (
"github.com/vansante/go-event-emitter"
"time"
)

const (
EventSongStarted = "song-started"
Expand All @@ -23,7 +26,7 @@ type Player interface {
Play() error
Stop()
GetStatus() PlayerStatus
GetCurrentSong() *Song
GetCurrentSong() (*Song, time.Duration)
}

type PlayerStatus string
Expand Down
30 changes: 20 additions & 10 deletions music/player/musicplayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import (
"errors"
"github.com/svenwiltink/go-musicbot/music"
"github.com/vansante/go-event-emitter"
"time"
)

// MusicPlayer is responsible for playing music
type MusicPlayer struct {
*eventemitter.Emitter
Queue *Queue
Status music.PlayerStatus
dataProviders []music.DataProvider
musicProviders []music.Provider
activeProvider music.Provider
currentSong *music.Song
shouldStop bool
Queue *Queue
Status music.PlayerStatus
dataProviders []music.DataProvider
musicProviders []music.Provider
activeProvider music.Provider
currentSong *music.Song
shouldStop bool
currentSongEnds time.Time
}

func (player *MusicPlayer) Pause() error {
Expand Down Expand Up @@ -54,8 +56,12 @@ func (player *MusicPlayer) GetStatus() music.PlayerStatus {
return player.Status
}

func (player *MusicPlayer) GetCurrentSong() *music.Song {
return player.currentSong
func (player *MusicPlayer) GetCurrentSong() (*music.Song, time.Duration) {
if player.currentSong != nil {
return player.currentSong, player.currentSongEnds.Sub(time.Now()).Round(time.Second)
}

return nil, time.Duration(0)
}

func (player *MusicPlayer) SetVolume(percentage int) error {
Expand Down Expand Up @@ -114,7 +120,10 @@ func (player *MusicPlayer) Search(searchString string) ([]*music.Song, error) {
songs := make([]*music.Song, 0)

for _, provider := range player.dataProviders {
results, _ := provider.Search(searchString)
results, err := provider.Search(searchString)
if err != nil {
return nil, err
}

if results != nil {
songs = append(songs, results...)
Expand Down Expand Up @@ -198,6 +207,7 @@ func (player *MusicPlayer) playLoop() {
continue
}

player.currentSongEnds = time.Now().Add(song.Duration)
player.EmitEvent(music.EventSongStarted, song)
player.Status = music.PlayerStatusPlaying
provider.Wait()
Expand Down
3 changes: 3 additions & 0 deletions music/song.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package music

import "time"

type SongType string

const (
Expand All @@ -12,4 +14,5 @@ type Song struct {
Artist string
Path string
SongType SongType
Duration time.Duration
}

0 comments on commit e510ea1

Please sign in to comment.