SongQueue now working, Song almost completed
This commit is contained in:
parent
131fac09aa
commit
7c1933e93f
10
main.go
10
main.go
|
@ -21,14 +21,14 @@ type mumbledj struct {
|
||||||
keepAlive chan bool
|
keepAlive chan bool
|
||||||
defaultChannel string
|
defaultChannel string
|
||||||
conf DjConfig
|
conf DjConfig
|
||||||
queue SongQueue
|
queue *SongQueue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dj *mumbledj) OnConnect(e *gumble.ConnectEvent) {
|
func (dj *mumbledj) OnConnect(e *gumble.ConnectEvent) {
|
||||||
if dj.client.Channels().Find(dj.defaultChannel) != nil {
|
if dj.client.Channels().Find(dj.defaultChannel) != nil {
|
||||||
dj.client.Self().Move(dj.client.Channels().Find(dj.defaultChannel))
|
dj.client.Self().Move(dj.client.Channels().Find(dj.defaultChannel))
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("No channel specified, moving to root...")
|
fmt.Println("Channel doesn't exist, moving to root channel...")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := loadConfiguration()
|
err := loadConfiguration()
|
||||||
|
@ -37,6 +37,7 @@ func (dj *mumbledj) OnConnect(e *gumble.ConnectEvent) {
|
||||||
} else {
|
} else {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
dj.queue = NewSongQueue()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dj *mumbledj) OnDisconnect(e *gumble.DisconnectEvent) {
|
func (dj *mumbledj) OnDisconnect(e *gumble.DisconnectEvent) {
|
||||||
|
@ -64,15 +65,18 @@ func (dj *mumbledj) HasPermission(username string, command bool) bool {
|
||||||
|
|
||||||
var dj = mumbledj{
|
var dj = mumbledj{
|
||||||
keepAlive: make(chan bool),
|
keepAlive: make(chan bool),
|
||||||
|
queue: NewSongQueue(),
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var address, port, username, password, channel string
|
var address, port, username, password, channel, debug string
|
||||||
|
|
||||||
flag.StringVar(&address, "server", "localhost", "address for Mumble server")
|
flag.StringVar(&address, "server", "localhost", "address for Mumble server")
|
||||||
flag.StringVar(&port, "port", "64738", "port for Mumble server")
|
flag.StringVar(&port, "port", "64738", "port for Mumble server")
|
||||||
flag.StringVar(&username, "username", "MumbleDJ", "username of MumbleDJ on server")
|
flag.StringVar(&username, "username", "MumbleDJ", "username of MumbleDJ on server")
|
||||||
flag.StringVar(&password, "password", "", "password for Mumble server (if needed)")
|
flag.StringVar(&password, "password", "", "password for Mumble server (if needed)")
|
||||||
flag.StringVar(&channel, "channel", "", "default channel for MumbleDJ")
|
flag.StringVar(&channel, "channel", "", "default channel for MumbleDJ")
|
||||||
|
flag.StringVar(&debug, "debug", "false", "toggle debug messages")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
dj.client = gumble.NewClient(&dj.config)
|
dj.client = gumble.NewClient(&dj.config)
|
||||||
|
|
54
song.go
54
song.go
|
@ -9,8 +9,15 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
//"github.com/layeh/gumble/gumble_ffmpeg"
|
//"github.com/layeh/gumble/gumble_ffmpeg"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/jmoiron/jsonq"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"os/user"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Song struct {
|
type Song struct {
|
||||||
|
@ -23,15 +30,39 @@ type Song struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSong(user, id string) *Song {
|
func NewSong(user, id string) *Song {
|
||||||
|
jsonUrl := fmt.Sprintf("http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc", id)
|
||||||
|
response, err := http.Get(jsonUrl)
|
||||||
|
jsonString := ""
|
||||||
|
if err == nil {
|
||||||
|
defer response.Body.Close()
|
||||||
|
body, err := ioutil.ReadAll(response.Body)
|
||||||
|
if err == nil {
|
||||||
|
jsonString = string(body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonData := map[string]interface{}{}
|
||||||
|
decoder := json.NewDecoder(strings.NewReader(jsonString))
|
||||||
|
decoder.Decode(&jsonData)
|
||||||
|
jq := jsonq.NewQuery(jsonData)
|
||||||
|
|
||||||
|
videoTitle, _ := jq.String("data", "title")
|
||||||
|
videoThumbnail, _ := jq.String("data", "thumbnail", "sqDefault")
|
||||||
|
duration, _ := jq.Int("data", "duration")
|
||||||
|
videoDuration := fmt.Sprintf("%d:%02d", duration/60, duration%60)
|
||||||
|
|
||||||
song := &Song{
|
song := &Song{
|
||||||
submitter: user,
|
submitter: user,
|
||||||
youtubeId: id,
|
title: videoTitle,
|
||||||
|
youtubeId: id,
|
||||||
|
duration: videoDuration,
|
||||||
|
thumbnailUrl: videoThumbnail,
|
||||||
}
|
}
|
||||||
return song
|
return song
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Song) Download() bool {
|
func (s *Song) Download() bool {
|
||||||
err := exec.Command(fmt.Sprintf("youtube-dl --output ~/.mumbledj/songs/%(id)s.%(ext)s --quiet --format bestaudio --audio-format vorbis --prefer-ffmpeg https://youtube.com/watch?v=%s", s.youtubeId))
|
err := exec.Command(fmt.Sprintf("youtube-dl --output \"~/.mumbledj/songs/%(id)s.%(ext)s\" --quiet --format m4a %s", s.youtubeId))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
|
@ -44,7 +75,22 @@ func (s *Song) Play() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Song) Delete() bool {
|
func (s *Song) Delete() bool {
|
||||||
return false
|
usr, err := user.Current()
|
||||||
|
if err == nil {
|
||||||
|
filePath := fmt.Sprintf("%s/.mumbledj/songs/%s.m4a", usr.HomeDir, s.youtubeId)
|
||||||
|
if _, err := os.Stat(filePath); err == nil {
|
||||||
|
err := os.Remove(filePath)
|
||||||
|
if err == nil {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Song) AddSkip(username string) bool {
|
func (s *Song) AddSkip(username string) bool {
|
||||||
|
|
25
songqueue.go
25
songqueue.go
|
@ -8,21 +8,30 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
type SongQueue struct {
|
type SongQueue struct {
|
||||||
queue Queue
|
queue *Queue
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSongQueue() *SongQueue {
|
func NewSongQueue() *SongQueue {
|
||||||
return &SongQueue{}
|
return &SongQueue{
|
||||||
|
queue: NewQueue(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *SongQueue) AddSong(s *Song) bool {
|
func (q *SongQueue) AddSong(s *Song) bool {
|
||||||
return false
|
beforeLen := q.queue.Len()
|
||||||
|
q.queue.Push(s)
|
||||||
|
if q.queue.Len() == beforeLen+1 {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *SongQueue) NextSong() bool {
|
func (q *SongQueue) NextSong() *Song {
|
||||||
return false
|
return q.queue.Poll().(*Song)
|
||||||
}
|
}
|
||||||
|
|
||||||
//func (q *SongQueue) CurrentSong() Song {
|
func (q *SongQueue) CurrentSong() *Song {
|
||||||
// return false
|
return q.queue.Peek().(*Song)
|
||||||
//}
|
}
|
||||||
|
|
Reference in a new issue