SongQueue now working, Song almost completed

This commit is contained in:
Matthieu Grieger 2014-12-18 16:28:27 -08:00
parent 131fac09aa
commit 7c1933e93f
3 changed files with 74 additions and 15 deletions

10
main.go
View file

@ -21,14 +21,14 @@ type mumbledj struct {
keepAlive chan bool
defaultChannel string
conf DjConfig
queue SongQueue
queue *SongQueue
}
func (dj *mumbledj) OnConnect(e *gumble.ConnectEvent) {
if dj.client.Channels().Find(dj.defaultChannel) != nil {
dj.client.Self().Move(dj.client.Channels().Find(dj.defaultChannel))
} else {
fmt.Println("No channel specified, moving to root...")
fmt.Println("Channel doesn't exist, moving to root channel...")
}
err := loadConfiguration()
@ -37,6 +37,7 @@ func (dj *mumbledj) OnConnect(e *gumble.ConnectEvent) {
} else {
panic(err)
}
dj.queue = NewSongQueue()
}
func (dj *mumbledj) OnDisconnect(e *gumble.DisconnectEvent) {
@ -64,15 +65,18 @@ func (dj *mumbledj) HasPermission(username string, command bool) bool {
var dj = mumbledj{
keepAlive: make(chan bool),
queue: NewSongQueue(),
}
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(&port, "port", "64738", "port for Mumble server")
flag.StringVar(&username, "username", "MumbleDJ", "username of MumbleDJ on server")
flag.StringVar(&password, "password", "", "password for Mumble server (if needed)")
flag.StringVar(&channel, "channel", "", "default channel for MumbleDJ")
flag.StringVar(&debug, "debug", "false", "toggle debug messages")
flag.Parse()
dj.client = gumble.NewClient(&dj.config)

54
song.go
View file

@ -9,8 +9,15 @@ package main
import (
//"github.com/layeh/gumble/gumble_ffmpeg"
"encoding/json"
"fmt"
"github.com/jmoiron/jsonq"
"io/ioutil"
"net/http"
"os"
"os/exec"
"os/user"
"strings"
)
type Song struct {
@ -23,15 +30,39 @@ type Song struct {
}
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{
submitter: user,
youtubeId: id,
submitter: user,
title: videoTitle,
youtubeId: id,
duration: videoDuration,
thumbnailUrl: videoThumbnail,
}
return song
}
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 {
return true
} else {
@ -44,7 +75,22 @@ func (s *Song) Play() 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 {

View file

@ -8,21 +8,30 @@
package main
type SongQueue struct {
queue Queue
queue *Queue
}
func NewSongQueue() *SongQueue {
return &SongQueue{}
return &SongQueue{
queue: NewQueue(),
}
}
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 {
return false
func (q *SongQueue) NextSong() *Song {
return q.queue.Poll().(*Song)
}
//func (q *SongQueue) CurrentSong() Song {
// return false
//}
func (q *SongQueue) CurrentSong() *Song {
return q.queue.Peek().(*Song)
}