From 2d867f0e32ec8eacdd0c9bb51094cd38707e4d88 Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Fri, 28 Jul 2017 23:15:43 -0400 Subject: just working with downloader --- downloader/download_manager.go | 85 +++--------------------------------------- 1 file changed, 5 insertions(+), 80 deletions(-) (limited to 'downloader/download_manager.go') diff --git a/downloader/download_manager.go b/downloader/download_manager.go index 004efc1..de028ab 100644 --- a/downloader/download_manager.go +++ b/downloader/download_manager.go @@ -1,89 +1,14 @@ package downloader -import ( - "time" -) +import "github.com/bwmarrin/discordgo" -// DownloadManager handles a download for a particular song. type DownloadManager struct { - Url string - - Start time.Duration - Duration time.Duration - End time.Duration - - pb chan (<-chan []byte) - - info videoInfo + session *discordgo.Session } -const clipTime = 10 * time.Second -const preloadCount = 5 +func NewManager(s *discordgo.Session) *DownloadManager { -func NewDownload(url string, startTime, dur time.Duration) (*DownloadManager, error) { - vInfo, err := info(url) - if err != nil { - return nil, err + return &DownloadManager{ + session: s, } - - if dur == 0 { - dur = vInfo.Duration - startTime - } - - dl := &DownloadManager{ - Url: url, - - Start: startTime, - Duration: dur, - End: startTime + dur, - - pb: make(chan (<-chan []byte), preloadCount), - info: *vInfo, - } - - go dl.schedule() - - return dl, nil -} - -func (d *DownloadManager) SendOn(ch chan<- []byte) <-chan struct{} { - out := make(chan struct{}, 1) - - go func() { - defer close(out) - for c := range d.pb { - for b := range c { - ch <- b - } - } - }() - - return out -} - -func (d *DownloadManager) schedule() { - go func() { - defer close(d.pb) - for i := 0; ; i++ { - clipStart := time.Duration(i)*clipTime + d.Start - clipEnd := time.Duration(i+1)*clipTime + d.Start - - if clipStart >= d.End { - return - } - - dur := clipTime - if clipEnd > d.End { - dur = d.End - clipStart - } - - ch, err := d.download(clipStart, dur) - if err != nil { - log.Errorf("error setting up download: %q", err) - return - } - - d.pb <- ch - } - }() } -- cgit v1.3.1