This repository has been archived on 2019-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mumbledj/song.go

119 lines
2.4 KiB
Go
Raw Normal View History

/*
* MumbleDJ
* By Matthieu Grieger
* song.go
* Copyright (c) 2014 Matthieu Grieger (MIT License)
*/
package main
import (
//"github.com/layeh/gumble/gumble_ffmpeg"
"encoding/json"
"fmt"
"github.com/jmoiron/jsonq"
"io/ioutil"
"net/http"
"os"
2014-12-16 01:41:32 +01:00
"os/exec"
"os/user"
"strings"
)
type Song struct {
2014-12-16 01:41:32 +01:00
submitter string
title string
youtubeId string
duration string
thumbnailUrl string
2014-12-16 01:41:32 +01:00
skippers []string
}
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,
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 m4a %s", s.youtubeId))
if err == nil {
return true
} else {
return false
}
}
func (s *Song) Play() bool {
return false
}
func (s *Song) Delete() bool {
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 {
2014-12-16 01:41:32 +01:00
for _, user := range s.skippers {
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 {
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
}