2015-08-03 22:40:19 +02:00
|
|
|
package main
|
|
|
|
|
2015-08-03 23:51:38 +02:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
|
|
|
|
2015-08-13 14:46:28 +02:00
|
|
|
"github.com/layeh/gumble/gumble"
|
2015-08-03 23:51:38 +02:00
|
|
|
"github.com/layeh/gumble/gumble_ffmpeg"
|
|
|
|
)
|
2015-08-03 22:40:19 +02:00
|
|
|
|
2015-08-15 20:56:00 +02:00
|
|
|
// YouTubeSong implements the Song interface
|
|
|
|
type YouTubeSong struct {
|
2015-08-03 22:40:19 +02:00
|
|
|
id string
|
|
|
|
title string
|
|
|
|
thumbnail string
|
2015-08-13 14:44:38 +02:00
|
|
|
submitter *gumble.User
|
2015-08-13 14:57:45 +02:00
|
|
|
duration string
|
2015-08-04 00:02:16 +02:00
|
|
|
url string
|
2015-08-04 00:04:41 +02:00
|
|
|
offset int
|
2015-08-13 15:33:37 +02:00
|
|
|
format string
|
2015-08-03 22:40:19 +02:00
|
|
|
playlist Playlist
|
|
|
|
skippers []string
|
|
|
|
dontSkip bool
|
|
|
|
}
|
|
|
|
|
2015-08-15 20:56:00 +02:00
|
|
|
// YouTubePlaylist implements the Playlist interface
|
|
|
|
type YouTubePlaylist struct {
|
2015-08-10 17:00:47 +02:00
|
|
|
id string
|
|
|
|
title string
|
|
|
|
}
|
|
|
|
|
2015-08-15 20:56:00 +02:00
|
|
|
// ------------
|
|
|
|
// YOUTUBE SONG
|
|
|
|
// ------------
|
2015-08-10 17:00:47 +02:00
|
|
|
|
2015-08-03 22:40:19 +02:00
|
|
|
// Download downloads the song via youtube-dl if it does not already exist on disk.
|
|
|
|
// All downloaded songs are stored in ~/.mumbledj/songs and should be automatically cleaned.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (dl *YouTubeSong) Download() error {
|
2015-08-03 22:40:19 +02:00
|
|
|
|
|
|
|
// Checks to see if song is already downloaded
|
2015-08-12 20:49:03 +02:00
|
|
|
if _, err := os.Stat(fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, dl.Filename())); os.IsNotExist(err) {
|
2015-08-13 15:33:37 +02:00
|
|
|
cmd := exec.Command("youtube-dl", "--no-mtime", "--output", fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, dl.Filename()), "--format", dl.format, "--prefer-ffmpeg", dl.url)
|
2015-08-13 15:12:28 +02:00
|
|
|
output, err := cmd.CombinedOutput()
|
2015-08-12 23:18:22 +02:00
|
|
|
if err == nil {
|
2015-08-03 22:40:19 +02:00
|
|
|
if dj.conf.Cache.Enabled {
|
|
|
|
dj.cache.CheckMaximumDirectorySize()
|
|
|
|
}
|
|
|
|
return nil
|
2015-08-12 23:18:22 +02:00
|
|
|
} else {
|
2015-08-13 15:33:37 +02:00
|
|
|
args := ""
|
2015-08-12 23:51:18 +02:00
|
|
|
for s := range cmd.Args {
|
2015-08-13 15:33:37 +02:00
|
|
|
args += cmd.Args[s] + " "
|
2015-08-12 23:51:18 +02:00
|
|
|
}
|
2015-08-16 02:10:35 +02:00
|
|
|
fmt.Printf(args + "\n" + string(output) + "\n" + "youtube-dl: " + err.Error() + "\n")
|
2015-08-12 23:18:22 +02:00
|
|
|
return errors.New("Song download failed.")
|
2015-08-03 22:40:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Play plays the song. Once the song is playing, a notification is displayed in a text message that features the song
|
|
|
|
// thumbnail, URL, title, duration, and submitter.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (dl *YouTubeSong) Play() {
|
2015-08-03 23:59:37 +02:00
|
|
|
if dl.offset != 0 {
|
2015-08-03 22:40:19 +02:00
|
|
|
offsetDuration, _ := time.ParseDuration(fmt.Sprintf("%ds", dl.offset))
|
|
|
|
dj.audioStream.Offset = offsetDuration
|
|
|
|
}
|
2015-08-12 20:49:03 +02:00
|
|
|
dj.audioStream.Source = gumble_ffmpeg.SourceFile(fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, dl.Filename()))
|
2015-08-03 22:40:19 +02:00
|
|
|
if err := dj.audioStream.Play(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
2015-08-12 20:49:03 +02:00
|
|
|
message := `<table><tr><td align="center"><img src="%s" width=150 /></td></tr><tr><td align="center"><b><a href="%s">%s</a> (%s)</b></td></tr><tr><td align="center">Added by %s</td></tr>`
|
2015-08-13 15:57:12 +02:00
|
|
|
message = fmt.Sprintf(message, dl.thumbnail, dl.url, dl.title, dl.duration, dl.submitter.Name)
|
2015-08-12 20:49:03 +02:00
|
|
|
if !isNil(dl.playlist) {
|
|
|
|
message = fmt.Sprintf(message+`<tr><td align="center">From playlist "%s"</td></tr>`, dl.playlist.Title())
|
2015-08-03 22:40:19 +02:00
|
|
|
}
|
2015-08-12 20:49:03 +02:00
|
|
|
dj.client.Self.Channel.Send(message+`</table>`, false)
|
2015-08-03 22:40:19 +02:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
dj.audioStream.Wait()
|
|
|
|
dj.queue.OnSongFinished()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes the song from ~/.mumbledj/songs if the cache is disabled.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (dl *YouTubeSong) Delete() error {
|
2015-08-03 22:40:19 +02:00
|
|
|
if dj.conf.Cache.Enabled == false {
|
2015-08-12 20:49:03 +02:00
|
|
|
filePath := fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, dl.Filename())
|
2015-08-03 22:40:19 +02:00
|
|
|
if _, err := os.Stat(filePath); err == nil {
|
|
|
|
if err := os.Remove(filePath); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("Error occurred while deleting audio file.")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddSkip adds a skip to the skippers slice. If the user is already in the slice, AddSkip
|
|
|
|
// returns an error and does not add a duplicate skip.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (dl *YouTubeSong) AddSkip(username string) error {
|
2015-08-03 22:40:19 +02:00
|
|
|
for _, user := range dl.skippers {
|
|
|
|
if username == user {
|
|
|
|
return errors.New("This user has already skipped the current song.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dl.skippers = append(dl.skippers, username)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveSkip removes a skip from the skippers slice. If username is not in slice, an error is
|
|
|
|
// returned.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (dl *YouTubeSong) RemoveSkip(username string) error {
|
2015-08-03 22:40:19 +02:00
|
|
|
for i, user := range dl.skippers {
|
|
|
|
if username == user {
|
2015-08-04 00:02:16 +02:00
|
|
|
dl.skippers = append(dl.skippers[:i], dl.skippers[i+1:]...)
|
2015-08-03 22:40:19 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New("This user has not skipped the song.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SkipReached calculates the current skip ratio based on the number of users within MumbleDJ's
|
|
|
|
// channel and the number of usernames in the skippers slice. If the value is greater than or equal
|
|
|
|
// to the skip ratio defined in the config, the function returns true, and returns false otherwise.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (dl *YouTubeSong) SkipReached(channelUsers int) bool {
|
2015-08-03 22:40:19 +02:00
|
|
|
if float32(len(dl.skippers))/float32(channelUsers) >= dj.conf.General.SkipRatio {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Submitter returns the name of the submitter of the Song.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (dl *YouTubeSong) Submitter() string {
|
2015-08-13 14:51:51 +02:00
|
|
|
return dl.submitter.Name
|
2015-08-03 22:40:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Title returns the title of the Song.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (dl *YouTubeSong) Title() string {
|
2015-08-03 22:40:19 +02:00
|
|
|
return dl.title
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the id of the Song.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (dl *YouTubeSong) ID() string {
|
2015-08-03 22:40:19 +02:00
|
|
|
return dl.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filename returns the filename of the Song.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (dl *YouTubeSong) Filename() string {
|
2015-08-13 15:33:37 +02:00
|
|
|
return dl.id + dl.format
|
2015-08-03 22:40:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Duration returns the duration of the Song.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (dl *YouTubeSong) Duration() string {
|
2015-08-03 22:40:19 +02:00
|
|
|
return dl.duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// Thumbnail returns the thumbnail URL for the Song.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (dl *YouTubeSong) Thumbnail() string {
|
2015-08-03 22:40:19 +02:00
|
|
|
return dl.thumbnail
|
|
|
|
}
|
|
|
|
|
|
|
|
// Playlist returns the playlist type for the Song (may be nil).
|
2015-08-15 20:56:00 +02:00
|
|
|
func (dl *YouTubeSong) Playlist() Playlist {
|
2015-08-03 22:40:19 +02:00
|
|
|
return dl.playlist
|
|
|
|
}
|
|
|
|
|
|
|
|
// DontSkip returns the DontSkip boolean value for the Song.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (dl *YouTubeSong) DontSkip() bool {
|
2015-08-03 22:40:19 +02:00
|
|
|
return dl.dontSkip
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDontSkip sets the DontSkip boolean value for the Song.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (dl *YouTubeSong) SetDontSkip(value bool) {
|
2015-08-03 22:40:19 +02:00
|
|
|
dl.dontSkip = value
|
|
|
|
}
|
2015-08-10 17:00:47 +02:00
|
|
|
|
|
|
|
// ----------------
|
|
|
|
// YOUTUBE PLAYLIST
|
|
|
|
// ----------------
|
|
|
|
|
|
|
|
// AddSkip adds a skip to the playlist's skippers slice.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (p *YouTubePlaylist) AddSkip(username string) error {
|
2015-08-10 17:00:47 +02:00
|
|
|
for _, user := range dj.playlistSkips[p.ID()] {
|
|
|
|
if username == user {
|
|
|
|
return errors.New("This user has already skipped the current song.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dj.playlistSkips[p.ID()] = append(dj.playlistSkips[p.ID()], username)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveSkip removes a skip from the playlist's skippers slice. If username is not in the slice
|
|
|
|
// an error is returned.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (p *YouTubePlaylist) RemoveSkip(username string) error {
|
2015-08-10 17:00:47 +02:00
|
|
|
for i, user := range dj.playlistSkips[p.ID()] {
|
|
|
|
if username == user {
|
|
|
|
dj.playlistSkips[p.ID()] = append(dj.playlistSkips[p.ID()][:i], dj.playlistSkips[p.ID()][i+1:]...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New("This user has not skipped the song.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSkippers removes the skippers entry in dj.playlistSkips.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (p *YouTubePlaylist) DeleteSkippers() {
|
2015-08-10 17:00:47 +02:00
|
|
|
delete(dj.playlistSkips, p.ID())
|
|
|
|
}
|
|
|
|
|
|
|
|
// SkipReached calculates the current skip ratio based on the number of users within MumbleDJ's
|
|
|
|
// channel and the number of usernames in the skippers slice. If the value is greater than or equal
|
|
|
|
// to the skip ratio defined in the config, the function returns true, and returns false otherwise.
|
2015-08-15 20:56:00 +02:00
|
|
|
func (p *YouTubePlaylist) SkipReached(channelUsers int) bool {
|
2015-08-10 17:00:47 +02:00
|
|
|
if float32(len(dj.playlistSkips[p.ID()]))/float32(channelUsers) >= dj.conf.General.PlaylistSkipRatio {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-08-15 20:56:00 +02:00
|
|
|
// ID returns the id of the YouTubePlaylist.
|
|
|
|
func (p *YouTubePlaylist) ID() string {
|
2015-08-10 17:00:47 +02:00
|
|
|
return p.id
|
|
|
|
}
|
|
|
|
|
2015-08-15 20:56:00 +02:00
|
|
|
// Title returns the title of the YouTubePlaylist.
|
|
|
|
func (p *YouTubePlaylist) Title() string {
|
2015-08-10 17:00:47 +02:00
|
|
|
return p.title
|
|
|
|
}
|