Seporated Service APIs and youtube-dl
This commit is contained in:
parent
e114268c30
commit
4b24ac56b4
|
@ -52,7 +52,7 @@ type Playlist interface {
|
||||||
Title() string
|
Title() string
|
||||||
}
|
}
|
||||||
|
|
||||||
var services = []Service{YouTube{}}
|
var services = []Service{YouTube{}, SoundCloud{}}
|
||||||
|
|
||||||
func findServiceAndAdd(user *gumble.User, url string) error {
|
func findServiceAndAdd(user *gumble.User, url string) error {
|
||||||
var urlService Service
|
var urlService Service
|
||||||
|
|
|
@ -13,19 +13,9 @@ import (
|
||||||
var soundcloudSongPattern = `https?:\/\/(www)?\.soundcloud\.com\/([\w-]+)\/([\w-]+)`
|
var soundcloudSongPattern = `https?:\/\/(www)?\.soundcloud\.com\/([\w-]+)\/([\w-]+)`
|
||||||
var soundcloudPlaylistPattern = `https?:\/\/(www)?\.soundcloud\.com\/([\w-]+)\/sets\/([\w-]+)`
|
var soundcloudPlaylistPattern = `https?:\/\/(www)?\.soundcloud\.com\/([\w-]+)\/sets\/([\w-]+)`
|
||||||
|
|
||||||
// ------
|
// SoundCloud implements the Service interface
|
||||||
// TYPES
|
|
||||||
// ------
|
|
||||||
|
|
||||||
// YouTube implements the Service interface
|
|
||||||
type SoundCloud struct{}
|
type SoundCloud struct{}
|
||||||
|
|
||||||
// YouTubePlaylist holds the metadata for a YouTube playlist.
|
|
||||||
type SoundCloudPlaylist struct {
|
|
||||||
id string
|
|
||||||
title string
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------
|
// ------------------
|
||||||
// SOUNDCLOUD SERVICE
|
// SOUNDCLOUD SERVICE
|
||||||
// ------------------
|
// ------------------
|
||||||
|
@ -59,7 +49,7 @@ func (sc SoundCloud) NewRequest(user *gumble.User, url string) (string, error) {
|
||||||
// Create playlist
|
// Create playlist
|
||||||
title, _ := apiResponse.String("title")
|
title, _ := apiResponse.String("title")
|
||||||
permalink, _ := apiResponse.String("permalink_url")
|
permalink, _ := apiResponse.String("permalink_url")
|
||||||
playlist := &SoundCloudPlaylist{
|
playlist := &YouTubeDLPlaylist{
|
||||||
id: permalink,
|
id: permalink,
|
||||||
title: title,
|
title: title,
|
||||||
}
|
}
|
||||||
|
@ -73,6 +63,7 @@ func (sc SoundCloud) NewRequest(user *gumble.User, url string) (string, error) {
|
||||||
return "", errors.New("NO_PLAYLIST_PERMISSION")
|
return "", errors.New("NO_PLAYLIST_PERMISSION")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// SONG
|
||||||
return sc.NewSong(user.Name, apiResponse, nil)
|
return sc.NewSong(user.Name, apiResponse, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -96,7 +87,7 @@ func (sc SoundCloud) NewSong(user string, trackData *jsonq.JsonQuery, playlist P
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
song := &YouTubeDL{
|
song := &YouTubeDLSong{
|
||||||
id: id,
|
id: id,
|
||||||
title: title,
|
title: title,
|
||||||
thumbnail: thumbnail,
|
thumbnail: thumbnail,
|
||||||
|
@ -109,55 +100,3 @@ func (sc SoundCloud) NewSong(user string, trackData *jsonq.JsonQuery, playlist P
|
||||||
dj.queue.AddSong(song)
|
dj.queue.AddSong(song)
|
||||||
return title, nil
|
return title, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------
|
|
||||||
// YOUTUBE PLAYLIST
|
|
||||||
// ----------------
|
|
||||||
|
|
||||||
// AddSkip adds a skip to the playlist's skippers slice.
|
|
||||||
func (p *SoundCloudPlaylist) AddSkip(username string) error {
|
|
||||||
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.
|
|
||||||
func (p *SoundCloudPlaylist) RemoveSkip(username string) error {
|
|
||||||
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.
|
|
||||||
func (p *SoundCloudPlaylist) DeleteSkippers() {
|
|
||||||
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.
|
|
||||||
func (p *SoundCloudPlaylist) SkipReached(channelUsers int) bool {
|
|
||||||
if float32(len(dj.playlistSkips[p.ID()]))/float32(channelUsers) >= dj.conf.General.PlaylistSkipRatio {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// ID returns the id of the YouTubePlaylist.
|
|
||||||
func (p *SoundCloudPlaylist) ID() string {
|
|
||||||
return p.id
|
|
||||||
}
|
|
||||||
|
|
||||||
// Title returns the title of the YouTubePlaylist.
|
|
||||||
func (p *SoundCloudPlaylist) Title() string {
|
|
||||||
return p.title
|
|
||||||
}
|
|
||||||
|
|
|
@ -42,26 +42,6 @@ var youtubeVideoPatterns = []string{
|
||||||
// YouTube implements the Service interface
|
// YouTube implements the Service interface
|
||||||
type YouTube struct{}
|
type YouTube struct{}
|
||||||
|
|
||||||
// YouTubeSong holds the metadata for a song extracted from a YouTube video.
|
|
||||||
type YouTubeSong struct {
|
|
||||||
submitter string
|
|
||||||
title string
|
|
||||||
id string
|
|
||||||
offset int
|
|
||||||
filename string
|
|
||||||
duration string
|
|
||||||
thumbnail string
|
|
||||||
skippers []string
|
|
||||||
playlist Playlist
|
|
||||||
dontSkip bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// YouTubePlaylist holds the metadata for a YouTube playlist.
|
|
||||||
type YouTubePlaylist struct {
|
|
||||||
id string
|
|
||||||
title string
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------
|
// ---------------
|
||||||
// YOUTUBE SERVICE
|
// YOUTUBE SERVICE
|
||||||
// ---------------
|
// ---------------
|
||||||
|
@ -105,7 +85,7 @@ func (yt YouTube) NewRequest(user *gumble.User, url string) (string, error) {
|
||||||
|
|
||||||
// NewSong gathers the metadata for a song extracted from a YouTube video, and returns
|
// NewSong gathers the metadata for a song extracted from a YouTube video, and returns
|
||||||
// the song.
|
// the song.
|
||||||
func (yt YouTube) NewSong(user, id, offset string, playlist *YouTubePlaylist) (*YouTubeSong, error) {
|
func (yt YouTube) NewSong(user, id, offset string, playlist *Playlist) (*Song, error) {
|
||||||
var apiResponse *jsonq.JsonQuery
|
var apiResponse *jsonq.JsonQuery
|
||||||
var err error
|
var err error
|
||||||
url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=%s&key=%s",
|
url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=%s&key=%s",
|
||||||
|
@ -179,7 +159,7 @@ func (yt YouTube) NewSong(user, id, offset string, playlist *YouTubePlaylist) (*
|
||||||
}
|
}
|
||||||
|
|
||||||
if dj.conf.General.MaxSongDuration == 0 || totalSeconds <= dj.conf.General.MaxSongDuration {
|
if dj.conf.General.MaxSongDuration == 0 || totalSeconds <= dj.conf.General.MaxSongDuration {
|
||||||
song := &YouTubeSong{
|
song := &YouTubeDLSong{
|
||||||
submitter: user,
|
submitter: user,
|
||||||
title: title,
|
title: title,
|
||||||
id: id,
|
id: id,
|
||||||
|
@ -200,7 +180,7 @@ func (yt YouTube) NewSong(user, id, offset string, playlist *YouTubePlaylist) (*
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPlaylist gathers the metadata for a YouTube playlist and returns it.
|
// NewPlaylist gathers the metadata for a YouTube playlist and returns it.
|
||||||
func (yt YouTube) NewPlaylist(user, id string) (*YouTubePlaylist, error) {
|
func (yt YouTube) NewPlaylist(user, id string) (*Playlist, error) {
|
||||||
var apiResponse *jsonq.JsonQuery
|
var apiResponse *jsonq.JsonQuery
|
||||||
var err error
|
var err error
|
||||||
// Retrieve title of playlist
|
// Retrieve title of playlist
|
||||||
|
@ -211,7 +191,7 @@ func (yt YouTube) NewPlaylist(user, id string) (*YouTubePlaylist, error) {
|
||||||
}
|
}
|
||||||
title, _ := apiResponse.String("items", "0", "snippet", "title")
|
title, _ := apiResponse.String("items", "0", "snippet", "title")
|
||||||
|
|
||||||
playlist := &YouTubePlaylist{
|
playlist := &YouTubeDLPlaylist{
|
||||||
id: id,
|
id: id,
|
||||||
title: title,
|
title: title,
|
||||||
}
|
}
|
||||||
|
@ -235,239 +215,6 @@ func (yt YouTube) NewPlaylist(user, id string) (*YouTubePlaylist, error) {
|
||||||
return playlist, nil
|
return playlist, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------
|
|
||||||
// YOUTUBE SONG
|
|
||||||
// ------------
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
func (s *YouTubeSong) Download() error {
|
|
||||||
|
|
||||||
// Checks to see if song is already downloaded
|
|
||||||
if _, err := os.Stat(fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, s.Filename())); os.IsNotExist(err) {
|
|
||||||
Verbose("Downloading " + s.Title())
|
|
||||||
cmd := exec.Command("youtube-dl", "--output", fmt.Sprintf(`~/.mumbledj/songs/%s`, s.Filename()), "--format", "m4a", "--", s.ID())
|
|
||||||
if err := cmd.Run(); err == nil {
|
|
||||||
if dj.conf.Cache.Enabled {
|
|
||||||
dj.cache.CheckMaximumDirectorySize()
|
|
||||||
}
|
|
||||||
Verbose(s.Title() + " downloaded")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
Verbose(s.Title() + " failed to download")
|
|
||||||
return errors.New("Song download failed.")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Play plays the song. Once the song is playing, a notification is displayed in a text message that features the video
|
|
||||||
// thumbnail, URL, title, duration, and submitter.
|
|
||||||
func (s *YouTubeSong) Play() {
|
|
||||||
if s.offset != 0 {
|
|
||||||
offsetDuration, _ := time.ParseDuration(fmt.Sprintf("%ds", s.offset))
|
|
||||||
dj.audioStream.Offset = offsetDuration
|
|
||||||
}
|
|
||||||
dj.audioStream.Source = gumble_ffmpeg.SourceFile(fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, s.Filename()))
|
|
||||||
if err := dj.audioStream.Play(); err != nil {
|
|
||||||
panic(err)
|
|
||||||
} else {
|
|
||||||
if isNil(s.Playlist()) {
|
|
||||||
message := `
|
|
||||||
<table>
|
|
||||||
<tr>
|
|
||||||
<td align="center"><img src="%s" width=150 /></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td align="center"><b><a href="http://youtu.be/%s">%s</a> (%s)</b></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td align="center">Added by %s</td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
`
|
|
||||||
dj.client.Self.Channel.Send(fmt.Sprintf(message, s.Thumbnail(), s.ID(), s.Title(),
|
|
||||||
s.Duration(), s.Submitter()), false)
|
|
||||||
} else {
|
|
||||||
message := `
|
|
||||||
<table>
|
|
||||||
<tr>
|
|
||||||
<td align="center"><img src="%s" width=150 /></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td align="center"><b><a href="http://youtu.be/%s">%s</a> (%s)</b></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td align="center">Added by %s</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td align="center">From playlist "%s"</td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
`
|
|
||||||
dj.client.Self.Channel.Send(fmt.Sprintf(message, s.Thumbnail(), s.ID(),
|
|
||||||
s.Title(), s.Duration(), s.Submitter(), s.Playlist().Title()), false)
|
|
||||||
}
|
|
||||||
Verbose("Now playing " + s.Title())
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
dj.audioStream.Wait()
|
|
||||||
dj.queue.OnSongFinished()
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete deletes the song from ~/.mumbledj/songs if the cache is disabled.
|
|
||||||
func (s *YouTubeSong) Delete() error {
|
|
||||||
if dj.conf.Cache.Enabled == false {
|
|
||||||
filePath := fmt.Sprintf("%s/.mumbledj/songs/%s.m4a", dj.homeDir, s.ID())
|
|
||||||
if _, err := os.Stat(filePath); err == nil {
|
|
||||||
if err := os.Remove(filePath); err == nil {
|
|
||||||
Verbose("Deleted " + s.Title())
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
Verbose("Failed to delete " + s.Title())
|
|
||||||
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.
|
|
||||||
func (s *YouTubeSong) AddSkip(username string) error {
|
|
||||||
for _, user := range s.skippers {
|
|
||||||
if username == user {
|
|
||||||
return errors.New("This user has already skipped the current song.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s.skippers = append(s.skippers, username)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RemoveSkip removes a skip from the skippers slice. If username is not in slice, an error is
|
|
||||||
// returned.
|
|
||||||
func (s *YouTubeSong) RemoveSkip(username string) error {
|
|
||||||
for i, user := range s.skippers {
|
|
||||||
if username == user {
|
|
||||||
s.skippers = append(s.skippers[:i], s.skippers[i+1:]...)
|
|
||||||
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.
|
|
||||||
func (s *YouTubeSong) SkipReached(channelUsers int) bool {
|
|
||||||
if float32(len(s.skippers))/float32(channelUsers) >= dj.conf.General.SkipRatio {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Submitter returns the name of the submitter of the YouTubeSong.
|
|
||||||
func (s *YouTubeSong) Submitter() string {
|
|
||||||
return s.submitter
|
|
||||||
}
|
|
||||||
|
|
||||||
// Title returns the title of the YouTubeSong.
|
|
||||||
func (s *YouTubeSong) Title() string {
|
|
||||||
return s.title
|
|
||||||
}
|
|
||||||
|
|
||||||
// ID returns the id of the YouTubeSong.
|
|
||||||
func (s *YouTubeSong) ID() string {
|
|
||||||
return s.id
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filename returns the filename of the YouTubeSong.
|
|
||||||
func (s *YouTubeSong) Filename() string {
|
|
||||||
return s.filename
|
|
||||||
}
|
|
||||||
|
|
||||||
// Duration returns the duration of the YouTubeSong.
|
|
||||||
func (s *YouTubeSong) Duration() string {
|
|
||||||
return s.duration
|
|
||||||
}
|
|
||||||
|
|
||||||
// Thumbnail returns the thumbnail URL for the YouTubeSong.
|
|
||||||
func (s *YouTubeSong) Thumbnail() string {
|
|
||||||
return s.thumbnail
|
|
||||||
}
|
|
||||||
|
|
||||||
// Playlist returns the playlist type for the YouTubeSong (may be nil).
|
|
||||||
func (s *YouTubeSong) Playlist() Playlist {
|
|
||||||
return s.playlist
|
|
||||||
}
|
|
||||||
|
|
||||||
// DontSkip returns the DontSkip boolean value for the YouTubeSong.
|
|
||||||
func (s *YouTubeSong) DontSkip() bool {
|
|
||||||
return s.dontSkip
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetDontSkip sets the DontSkip boolean value for the YouTubeSong.
|
|
||||||
func (s *YouTubeSong) SetDontSkip(value bool) {
|
|
||||||
s.dontSkip = value
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----------------
|
|
||||||
// YOUTUBE PLAYLIST
|
|
||||||
// ----------------
|
|
||||||
|
|
||||||
// AddSkip adds a skip to the playlist's skippers slice.
|
|
||||||
func (p *YouTubePlaylist) AddSkip(username string) error {
|
|
||||||
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.
|
|
||||||
func (p *YouTubePlaylist) RemoveSkip(username string) error {
|
|
||||||
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.
|
|
||||||
func (p *YouTubePlaylist) DeleteSkippers() {
|
|
||||||
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.
|
|
||||||
func (p *YouTubePlaylist) SkipReached(channelUsers int) bool {
|
|
||||||
if float32(len(dj.playlistSkips[p.ID()]))/float32(channelUsers) >= dj.conf.General.PlaylistSkipRatio {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// ID returns the id of the YouTubePlaylist.
|
|
||||||
func (p *YouTubePlaylist) ID() string {
|
|
||||||
return p.id
|
|
||||||
}
|
|
||||||
|
|
||||||
// Title returns the title of the YouTubePlaylist.
|
|
||||||
func (p *YouTubePlaylist) Title() string {
|
|
||||||
return p.title
|
|
||||||
}
|
|
||||||
|
|
||||||
// -----------
|
|
||||||
// YOUTUBE API
|
|
||||||
// -----------
|
|
||||||
|
|
||||||
// PerformGetRequest does all the grunt work for a YouTube HTTPS GET request.
|
// PerformGetRequest does all the grunt work for a YouTube HTTPS GET request.
|
||||||
func PerformGetRequest(url string) (*jsonq.JsonQuery, error) {
|
func PerformGetRequest(url string) (*jsonq.JsonQuery, error) {
|
||||||
jsonString := ""
|
jsonString := ""
|
||||||
|
|
|
@ -10,7 +10,8 @@ import (
|
||||||
"github.com/layeh/gumble/gumble_ffmpeg"
|
"github.com/layeh/gumble/gumble_ffmpeg"
|
||||||
)
|
)
|
||||||
|
|
||||||
type YouTubeDL struct {
|
// Extends a Song
|
||||||
|
type YouTubeDLSong struct {
|
||||||
id string
|
id string
|
||||||
title string
|
title string
|
||||||
thumbnail string
|
thumbnail string
|
||||||
|
@ -23,9 +24,18 @@ type YouTubeDL struct {
|
||||||
dontSkip bool
|
dontSkip bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type YouTubeDLPlaylist struct {
|
||||||
|
id string
|
||||||
|
title string
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------
|
||||||
|
// YouTubeDLSong
|
||||||
|
// -------------
|
||||||
|
|
||||||
// Download downloads the song via youtube-dl if it does not already exist on disk.
|
// 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.
|
// All downloaded songs are stored in ~/.mumbledj/songs and should be automatically cleaned.
|
||||||
func (dl *YouTubeDL) Download() error {
|
func (dl *YouTubeDLSong) Download() error {
|
||||||
|
|
||||||
// Checks to see if song is already downloaded
|
// Checks to see if song is already downloaded
|
||||||
if _, err := os.Stat(fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, dl.id+".m4a")); os.IsNotExist(err) {
|
if _, err := os.Stat(fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, dl.id+".m4a")); os.IsNotExist(err) {
|
||||||
|
@ -43,7 +53,7 @@ func (dl *YouTubeDL) Download() error {
|
||||||
|
|
||||||
// Play plays the song. Once the song is playing, a notification is displayed in a text message that features the song
|
// 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.
|
// thumbnail, URL, title, duration, and submitter.
|
||||||
func (dl *YouTubeDL) Play() {
|
func (dl *YouTubeDLSong) Play() {
|
||||||
if dl.offset != 0 {
|
if dl.offset != 0 {
|
||||||
offsetDuration, _ := time.ParseDuration(fmt.Sprintf("%ds", dl.offset))
|
offsetDuration, _ := time.ParseDuration(fmt.Sprintf("%ds", dl.offset))
|
||||||
dj.audioStream.Offset = offsetDuration
|
dj.audioStream.Offset = offsetDuration
|
||||||
|
@ -70,7 +80,7 @@ func (dl *YouTubeDL) Play() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete deletes the song from ~/.mumbledj/songs if the cache is disabled.
|
// Delete deletes the song from ~/.mumbledj/songs if the cache is disabled.
|
||||||
func (dl *YouTubeDL) Delete() error {
|
func (dl *YouTubeDLSong) Delete() error {
|
||||||
if dj.conf.Cache.Enabled == false {
|
if dj.conf.Cache.Enabled == false {
|
||||||
filePath := fmt.Sprintf("%s/.mumbledj/songs/%s.m4a", dj.homeDir, dl.id)
|
filePath := fmt.Sprintf("%s/.mumbledj/songs/%s.m4a", dj.homeDir, dl.id)
|
||||||
if _, err := os.Stat(filePath); err == nil {
|
if _, err := os.Stat(filePath); err == nil {
|
||||||
|
@ -86,7 +96,7 @@ func (dl *YouTubeDL) Delete() error {
|
||||||
|
|
||||||
// AddSkip adds a skip to the skippers slice. If the user is already in the slice, AddSkip
|
// 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.
|
// returns an error and does not add a duplicate skip.
|
||||||
func (dl *YouTubeDL) AddSkip(username string) error {
|
func (dl *YouTubeDLSong) AddSkip(username string) error {
|
||||||
for _, user := range dl.skippers {
|
for _, user := range dl.skippers {
|
||||||
if username == user {
|
if username == user {
|
||||||
return errors.New("This user has already skipped the current song.")
|
return errors.New("This user has already skipped the current song.")
|
||||||
|
@ -98,7 +108,7 @@ func (dl *YouTubeDL) AddSkip(username string) error {
|
||||||
|
|
||||||
// RemoveSkip removes a skip from the skippers slice. If username is not in slice, an error is
|
// RemoveSkip removes a skip from the skippers slice. If username is not in slice, an error is
|
||||||
// returned.
|
// returned.
|
||||||
func (dl *YouTubeDL) RemoveSkip(username string) error {
|
func (dl *YouTubeDLSong) RemoveSkip(username string) error {
|
||||||
for i, user := range dl.skippers {
|
for i, user := range dl.skippers {
|
||||||
if username == user {
|
if username == user {
|
||||||
dl.skippers = append(dl.skippers[:i], dl.skippers[i+1:]...)
|
dl.skippers = append(dl.skippers[:i], dl.skippers[i+1:]...)
|
||||||
|
@ -111,7 +121,7 @@ func (dl *YouTubeDL) RemoveSkip(username string) error {
|
||||||
// SkipReached calculates the current skip ratio based on the number of users within MumbleDJ's
|
// 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
|
// 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.
|
// to the skip ratio defined in the config, the function returns true, and returns false otherwise.
|
||||||
func (dl *YouTubeDL) SkipReached(channelUsers int) bool {
|
func (dl *YouTubeDLSong) SkipReached(channelUsers int) bool {
|
||||||
if float32(len(dl.skippers))/float32(channelUsers) >= dj.conf.General.SkipRatio {
|
if float32(len(dl.skippers))/float32(channelUsers) >= dj.conf.General.SkipRatio {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -119,46 +129,98 @@ func (dl *YouTubeDL) SkipReached(channelUsers int) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Submitter returns the name of the submitter of the Song.
|
// Submitter returns the name of the submitter of the Song.
|
||||||
func (dl *YouTubeDL) Submitter() string {
|
func (dl *YouTubeDLSong) Submitter() string {
|
||||||
return dl.submitter
|
return dl.submitter
|
||||||
}
|
}
|
||||||
|
|
||||||
// Title returns the title of the Song.
|
// Title returns the title of the Song.
|
||||||
func (dl *YouTubeDL) Title() string {
|
func (dl *YouTubeDLSong) Title() string {
|
||||||
return dl.title
|
return dl.title
|
||||||
}
|
}
|
||||||
|
|
||||||
// ID returns the id of the Song.
|
// ID returns the id of the Song.
|
||||||
func (dl *YouTubeDL) ID() string {
|
func (dl *YouTubeDLSong) ID() string {
|
||||||
return dl.id
|
return dl.id
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filename returns the filename of the Song.
|
// Filename returns the filename of the Song.
|
||||||
func (dl *YouTubeDL) Filename() string {
|
func (dl *YouTubeDLSong) Filename() string {
|
||||||
return dl.id + ".m4a"
|
return dl.id + ".m4a"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Duration returns the duration of the Song.
|
// Duration returns the duration of the Song.
|
||||||
func (dl *YouTubeDL) Duration() string {
|
func (dl *YouTubeDLSong) Duration() string {
|
||||||
return dl.duration
|
return dl.duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// Thumbnail returns the thumbnail URL for the Song.
|
// Thumbnail returns the thumbnail URL for the Song.
|
||||||
func (dl *YouTubeDL) Thumbnail() string {
|
func (dl *YouTubeDLSong) Thumbnail() string {
|
||||||
return dl.thumbnail
|
return dl.thumbnail
|
||||||
}
|
}
|
||||||
|
|
||||||
// Playlist returns the playlist type for the Song (may be nil).
|
// Playlist returns the playlist type for the Song (may be nil).
|
||||||
func (dl *YouTubeDL) Playlist() Playlist {
|
func (dl *YouTubeDLSong) Playlist() Playlist {
|
||||||
return dl.playlist
|
return dl.playlist
|
||||||
}
|
}
|
||||||
|
|
||||||
// DontSkip returns the DontSkip boolean value for the Song.
|
// DontSkip returns the DontSkip boolean value for the Song.
|
||||||
func (dl *YouTubeDL) DontSkip() bool {
|
func (dl *YouTubeDLSong) DontSkip() bool {
|
||||||
return dl.dontSkip
|
return dl.dontSkip
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetDontSkip sets the DontSkip boolean value for the Song.
|
// SetDontSkip sets the DontSkip boolean value for the Song.
|
||||||
func (dl *YouTubeDL) SetDontSkip(value bool) {
|
func (dl *YouTubeDLSong) SetDontSkip(value bool) {
|
||||||
dl.dontSkip = value
|
dl.dontSkip = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------
|
||||||
|
// YOUTUBE PLAYLIST
|
||||||
|
// ----------------
|
||||||
|
|
||||||
|
// AddSkip adds a skip to the playlist's skippers slice.
|
||||||
|
func (p *YouTubeDLPlaylist) AddSkip(username string) error {
|
||||||
|
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.
|
||||||
|
func (p *YouTubeDLPlaylist) RemoveSkip(username string) error {
|
||||||
|
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.
|
||||||
|
func (p *YouTubeDLPlaylist) DeleteSkippers() {
|
||||||
|
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.
|
||||||
|
func (p *YouTubeDLPlaylist) SkipReached(channelUsers int) bool {
|
||||||
|
if float32(len(dj.playlistSkips[p.ID()]))/float32(channelUsers) >= dj.conf.General.PlaylistSkipRatio {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// ID returns the id of the YouTubeDLPlaylist.
|
||||||
|
func (p *YouTubeDLPlaylist) ID() string {
|
||||||
|
return p.id
|
||||||
|
}
|
||||||
|
|
||||||
|
// Title returns the title of the YouTubeDLPlaylist.
|
||||||
|
func (p *YouTubeDLPlaylist) Title() string {
|
||||||
|
return p.title
|
||||||
|
}
|
||||||
|
|
Reference in a new issue