2014-12-15 06:25:54 +01:00
|
|
|
/*
|
|
|
|
* MumbleDJ
|
|
|
|
* By Matthieu Grieger
|
|
|
|
* song.go
|
2015-01-18 23:44:40 +01:00
|
|
|
* Copyright (c) 2014, 2015 Matthieu Grieger (MIT License)
|
2014-12-15 06:25:54 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-12-19 01:28:27 +01:00
|
|
|
"encoding/json"
|
2014-12-27 09:25:49 +01:00
|
|
|
"errors"
|
2014-12-15 06:25:54 +01:00
|
|
|
"fmt"
|
2014-12-19 01:28:27 +01:00
|
|
|
"github.com/jmoiron/jsonq"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2014-12-16 01:41:32 +01:00
|
|
|
"os/exec"
|
2014-12-19 01:28:27 +01:00
|
|
|
"strings"
|
2014-12-15 06:25:54 +01:00
|
|
|
)
|
|
|
|
|
2014-12-27 19:05:13 +01:00
|
|
|
// Song type declaration.
|
2014-12-15 06:25:54 +01:00
|
|
|
type Song struct {
|
2015-02-12 22:27:04 +01:00
|
|
|
submitter string
|
|
|
|
title string
|
|
|
|
youtubeId string
|
|
|
|
duration string
|
|
|
|
thumbnailUrl string
|
|
|
|
skippers []string
|
|
|
|
playlist *Playlist
|
|
|
|
dontSkip bool
|
2014-12-15 06:25:54 +01:00
|
|
|
}
|
|
|
|
|
2014-12-27 19:05:13 +01:00
|
|
|
// Returns a new Song type. Before returning the new type, the song's metadata is collected
|
|
|
|
// via the YouTube Gdata API.
|
2015-02-12 22:27:04 +01:00
|
|
|
func NewSong(user, id string, playlist *Playlist) (*Song, error) {
|
2014-12-19 01:28:27 +01:00
|
|
|
jsonUrl := fmt.Sprintf("http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc", id)
|
|
|
|
jsonString := ""
|
2014-12-27 09:25:49 +01:00
|
|
|
|
|
|
|
if response, err := http.Get(jsonUrl); err == nil {
|
2014-12-19 01:28:27 +01:00
|
|
|
defer response.Body.Close()
|
2015-02-03 04:45:17 +01:00
|
|
|
if response.StatusCode != 400 && response.StatusCode != 404 {
|
2015-02-02 21:05:00 +01:00
|
|
|
if body, err := ioutil.ReadAll(response.Body); err == nil {
|
|
|
|
jsonString = string(body)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("Invalid YouTube ID supplied.")
|
2014-12-19 01:28:27 +01:00
|
|
|
}
|
2015-02-03 04:45:17 +01:00
|
|
|
} else {
|
|
|
|
return nil, errors.New("An error occurred while receiving HTTP GET request.")
|
2014-12-19 01:28:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonData := map[string]interface{}{}
|
|
|
|
decoder := json.NewDecoder(strings.NewReader(jsonString))
|
|
|
|
decoder.Decode(&jsonData)
|
|
|
|
jq := jsonq.NewQuery(jsonData)
|
|
|
|
|
|
|
|
videoTitle, _ := jq.String("data", "title")
|
2014-12-27 09:25:49 +01:00
|
|
|
videoThumbnail, _ := jq.String("data", "thumbnail", "hqDefault")
|
2014-12-19 01:28:27 +01:00
|
|
|
duration, _ := jq.Int("data", "duration")
|
|
|
|
videoDuration := fmt.Sprintf("%d:%02d", duration/60, duration%60)
|
|
|
|
|
2014-12-15 06:25:54 +01:00
|
|
|
song := &Song{
|
2015-02-12 22:27:04 +01:00
|
|
|
submitter: user,
|
|
|
|
title: videoTitle,
|
|
|
|
youtubeId: id,
|
|
|
|
duration: videoDuration,
|
|
|
|
thumbnailUrl: videoThumbnail,
|
|
|
|
playlist: playlist,
|
|
|
|
dontSkip: false,
|
2014-12-15 06:25:54 +01:00
|
|
|
}
|
2015-02-02 21:05:00 +01:00
|
|
|
return song, nil
|
2014-12-15 06:25:54 +01:00
|
|
|
}
|
|
|
|
|
2015-02-18 00:43:20 +01:00
|
|
|
// 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.
|
2014-12-27 09:25:49 +01:00
|
|
|
func (s *Song) Download() error {
|
2015-02-18 00:43:20 +01:00
|
|
|
if _, err := os.Stat(fmt.Sprintf("%s/.mumbledj/songs/%s.m4a", dj.homeDir, s.youtubeId)); os.IsNotExist(err) {
|
|
|
|
cmd := exec.Command("youtube-dl", "--output", fmt.Sprintf(`~/.mumbledj/songs/%s.m4a`, s.youtubeId), "--format", "m4a", s.youtubeId)
|
|
|
|
if err := cmd.Run(); err == nil {
|
|
|
|
if dj.conf.Cache.Enabled {
|
|
|
|
dj.cache.CheckMaximumDirectorySize()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return errors.New("Song download failed.")
|
|
|
|
}
|
2014-12-15 06:25:54 +01:00
|
|
|
} else {
|
2015-02-18 00:43:20 +01:00
|
|
|
return nil
|
2014-12-15 06:25:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-27 19:05:13 +01:00
|
|
|
// 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.
|
2014-12-27 09:25:49 +01:00
|
|
|
func (s *Song) Play() {
|
2015-02-12 22:27:04 +01:00
|
|
|
if err := dj.audioStream.Play(fmt.Sprintf("%s/.mumbledj/songs/%s.m4a", dj.homeDir, s.youtubeId), dj.queue.OnSongFinished); err != nil {
|
2015-02-02 20:19:24 +01:00
|
|
|
panic(err)
|
|
|
|
} else {
|
2015-02-12 22:27:04 +01:00
|
|
|
if s.playlist == nil {
|
2015-02-12 00:25:47 +01:00
|
|
|
dj.client.Self.Channel.Send(fmt.Sprintf(NOW_PLAYING_HTML, s.thumbnailUrl, s.youtubeId, s.title,
|
2015-02-08 02:54:24 +01:00
|
|
|
s.duration, s.submitter), false)
|
|
|
|
} else {
|
2015-02-12 00:25:47 +01:00
|
|
|
dj.client.Self.Channel.Send(fmt.Sprintf(NOW_PLAYING_PLAYLIST_HTML, s.thumbnailUrl, s.youtubeId,
|
2015-02-12 22:27:04 +01:00
|
|
|
s.title, s.duration, s.submitter, s.playlist.title), false)
|
2015-02-08 02:54:24 +01:00
|
|
|
}
|
2015-02-02 20:19:24 +01:00
|
|
|
}
|
2014-12-15 06:25:54 +01:00
|
|
|
}
|
|
|
|
|
2015-02-18 00:43:20 +01:00
|
|
|
// Deletes the song from ~/.mumbledj/songs if the cache is disabled.
|
2014-12-27 09:25:49 +01:00
|
|
|
func (s *Song) Delete() error {
|
2015-02-18 00:43:20 +01:00
|
|
|
if dj.conf.Cache.Enabled == false {
|
|
|
|
filePath := fmt.Sprintf("%s/.mumbledj/songs/%s.m4a", dj.homeDir, s.youtubeId)
|
|
|
|
if _, err := os.Stat(filePath); err == nil {
|
|
|
|
if err := os.Remove(filePath); err == nil {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return errors.New("Error occurred while deleting audio file.")
|
|
|
|
}
|
2014-12-19 01:28:27 +01:00
|
|
|
} else {
|
2015-02-18 00:43:20 +01:00
|
|
|
return nil
|
2014-12-19 01:28:27 +01:00
|
|
|
}
|
|
|
|
} else {
|
2014-12-27 09:25:49 +01:00
|
|
|
return nil
|
2014-12-19 01:28:27 +01:00
|
|
|
}
|
2014-12-15 06:25:54 +01:00
|
|
|
}
|
|
|
|
|
2014-12-27 19:05:13 +01:00
|
|
|
// 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.
|
2014-12-27 09:25:49 +01:00
|
|
|
func (s *Song) AddSkip(username string) error {
|
2014-12-16 01:41:32 +01:00
|
|
|
for _, user := range s.skippers {
|
2014-12-15 06:25:54 +01:00
|
|
|
if username == user {
|
2014-12-27 09:25:49 +01:00
|
|
|
return errors.New("This user has already skipped the current song.")
|
2014-12-15 06:25:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s.skippers = append(s.skippers, username)
|
2014-12-27 09:25:49 +01:00
|
|
|
return nil
|
2014-12-15 06:25:54 +01:00
|
|
|
}
|
|
|
|
|
2014-12-27 19:05:13 +01:00
|
|
|
// Removes a skip from the skippers slice. If username is not in the slice, an error is
|
|
|
|
// returned.
|
2014-12-27 09:25:49 +01:00
|
|
|
func (s *Song) RemoveSkip(username string) error {
|
2014-12-16 01:41:32 +01:00
|
|
|
for i, user := range s.skippers {
|
2014-12-15 06:25:54 +01:00
|
|
|
if username == user {
|
|
|
|
s.skippers = append(s.skippers[:i], s.skippers[i+1:]...)
|
2014-12-27 09:25:49 +01:00
|
|
|
return nil
|
2014-12-15 06:25:54 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-27 09:25:49 +01:00
|
|
|
return errors.New("This user has not skipped the song.")
|
2014-12-15 06:25:54 +01:00
|
|
|
}
|
|
|
|
|
2014-12-27 19:05:13 +01:00
|
|
|
// Calculates current skip ratio based on number of users within MumbleDJ's channel and the
|
|
|
|
// amount of values in the skippers slice. If the value is greater than or equal to the skip ratio
|
|
|
|
// defined in mumbledj.gcfg, the function returns true. Returns false otherwise.
|
2014-12-16 01:41:32 +01:00
|
|
|
func (s *Song) SkipReached(channelUsers int) bool {
|
2014-12-27 09:25:49 +01:00
|
|
|
if float32(len(s.skippers))/float32(channelUsers) >= dj.conf.General.SkipRatio {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
2014-12-16 01:41:32 +01:00
|
|
|
}
|