2014-12-15 06:25:54 +01:00
|
|
|
/*
|
|
|
|
* MumbleDJ
|
|
|
|
* By Matthieu Grieger
|
|
|
|
* song.go
|
|
|
|
* Copyright (c) 2014 Matthieu Grieger (MIT License)
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
//"github.com/layeh/gumble/gumble_ffmpeg"
|
2014-12-19 01:28:27 +01:00
|
|
|
"encoding/json"
|
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
|
|
|
"os/user"
|
|
|
|
"strings"
|
2014-12-15 06:25:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Song struct {
|
2014-12-16 01:41:32 +01:00
|
|
|
submitter string
|
|
|
|
title string
|
|
|
|
youtubeId string
|
|
|
|
duration string
|
2014-12-15 06:25:54 +01:00
|
|
|
thumbnailUrl string
|
2014-12-16 01:41:32 +01:00
|
|
|
skippers []string
|
2014-12-15 06:25:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSong(user, id string) *Song {
|
2014-12-19 01:28:27 +01:00
|
|
|
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)
|
|
|
|
|
2014-12-15 06:25:54 +01:00
|
|
|
song := &Song{
|
2014-12-19 01:28:27 +01:00
|
|
|
submitter: user,
|
|
|
|
title: videoTitle,
|
|
|
|
youtubeId: id,
|
|
|
|
duration: videoDuration,
|
|
|
|
thumbnailUrl: videoThumbnail,
|
2014-12-15 06:25:54 +01:00
|
|
|
}
|
|
|
|
return song
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Song) Download() bool {
|
2014-12-19 01:28:27 +01:00
|
|
|
err := exec.Command(fmt.Sprintf("youtube-dl --output \"~/.mumbledj/songs/%(id)s.%(ext)s\" --quiet --format m4a %s", s.youtubeId))
|
2014-12-15 06:25:54 +01:00
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Song) Play() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Song) Delete() bool {
|
2014-12-19 01:28:27 +01:00
|
|
|
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
|
|
|
|
}
|
2014-12-15 06:25:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Song) AddSkip(username string) bool {
|
2014-12-16 01:41:32 +01:00
|
|
|
for _, user := range s.skippers {
|
2014-12-15 06:25:54 +01:00
|
|
|
if username == user {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.skippers = append(s.skippers, username)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Song) RemoveSkip(username string) bool {
|
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:]...)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-12-16 01:41:32 +01:00
|
|
|
func (s *Song) SkipReached(channelUsers int) bool {
|
|
|
|
return false
|
|
|
|
}
|