summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Perry <avaglir@gmail.com>2017-07-28 20:04:57 -0400
committerNathan Perry <avaglir@gmail.com>2017-07-28 20:04:57 -0400
commitf48248693839d803490ef92fc5ba89e25024ab96 (patch)
tree22832fc596bfabad7f3455af1e67d98e9c49a709
parent89e5b420e23eb76393c275be43c082e5410e7f4c (diff)
grab json from youtube-dl
-rw-r--r--downloader/download_manager.go15
-rw-r--r--downloader/downloader.go44
-rw-r--r--thulani.go5
3 files changed, 49 insertions, 15 deletions
diff --git a/downloader/download_manager.go b/downloader/download_manager.go
new file mode 100644
index 0000000..fb7e883
--- /dev/null
+++ b/downloader/download_manager.go
@@ -0,0 +1,15 @@
+package downloader
+
+// DownloadManager handles a download for a particular song.
+type DownloadManager struct {
+ Url string
+
+ dlChans []<-chan []byte
+}
+
+func NewDownload(url string) DownloadManager {
+ return DownloadManager{
+ Url: url,
+ dlChans: []<-chan []byte{},
+ }
+}
diff --git a/downloader/downloader.go b/downloader/downloader.go
index 76e9f01..793cd22 100644
--- a/downloader/downloader.go
+++ b/downloader/downloader.go
@@ -9,56 +9,74 @@ import (
"io/ioutil"
"os"
+ "encoding/json"
+
"github.com/mammothbane/thulani-go/wav"
"github.com/op/go-logging"
)
var log = logging.MustGetLogger("downloader")
-func getUrl(inUrl string) (string, error) {
- dl := exec.Command("youtube-dl", "-f", "bestaudio", "-x", "--get-url", inUrl)
+// responsible for decoding from youtube
+type videoInfo struct {
+ Title string `json:"fulltitle"`
+ UrlStr string `json:"url"`
+ DurationSec int `json:"duration"`
+ Url *url.URL `json:"-"`
+ Duration time.Duration `json:"-"`
+}
+
+func info(inUrl string) (*videoInfo, error) {
+ dl := exec.Command("youtube-dl", "-f", "bestaudio", "-x", "-j", inUrl)
outpipe, err := dl.StdoutPipe()
if err != nil {
- return "", err
+ return nil, err
}
errpipe, err := dl.StderrPipe()
if err != nil {
- return "", err
+ return nil, err
}
err = dl.Start()
if err != nil {
log.Errorf("starting youtube-dl failed")
- return "", err
+ return nil, err
}
o, ierr := ioutil.ReadAll(outpipe)
if ierr != nil {
log.Errorf("unable to read from output pipe")
- return "", err
+ return nil, err
}
e, ierr := ioutil.ReadAll(errpipe)
if ierr != nil {
log.Errorf("unable to read from error pipe")
- return "", err
+ return nil, err
}
if err := dl.Wait(); err != nil {
log.Errorf("error:\n%v", string(e))
- return "", err
+ return nil, err
+ }
+
+ v := videoInfo{}
+ if err := json.Unmarshal(o, &v); err != nil {
+ return nil, err
}
- tgt, err := url.Parse(string(o))
- out := tgt.Scheme + "://" + tgt.Host + tgt.Path + "?" + tgt.Query().Encode()
+ v.Duration = time.Duration(v.DurationSec) * time.Second
+ v.Url, err = url.Parse(v.UrlStr)
- return out, nil
+ //tgt, err := url.Parse(string(o))
+ //out := tgt.Scheme + "://" + tgt.Host + tgt.Path + "?" + tgt.Query().Encode()
+ return &v, err
}
func Download(inUrl string, startTime time.Duration, duration time.Duration) (<-chan []byte, error) {
- targetUrl, err := getUrl(inUrl)
+ vInfo, err := info(inUrl)
if err != nil {
return nil, err
}
@@ -68,7 +86,7 @@ func Download(inUrl string, startTime time.Duration, duration time.Duration) (<-
args := []string{
"-ss", strconv.Itoa(startSecond),
- "-i", targetUrl,
+ "-i", vInfo.Url.String(),
"-c:a", "pcm_s16le",
"-f", "wav",
"-ar", "48000",
diff --git a/thulani.go b/thulani.go
index 8ac167c..3e99c74 100644
--- a/thulani.go
+++ b/thulani.go
@@ -10,9 +10,10 @@ import (
"time"
+ "math/rand"
+
"github.com/bwmarrin/discordgo"
"github.com/mammothbane/thulani-go/downloader"
- "math/rand"
)
var config *Config
@@ -113,7 +114,7 @@ func onMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
ch, err := downloader.Download("https://www.youtube.com/watch?v=_K13GJkGvDw", time.Duration(rand.Intn(10*60))*time.Second, 5*time.Second)
if err != nil {
- log.Errorf("unable to download video")
+ log.Errorf("unable to download video: %q", err)
break
}