diff --git a/backend/downloader/youtube-dl/youtube-dl.go b/backend/downloader/youtube-dl/youtube-dl.go index 14ea8ec..6c34419 100644 --- a/backend/downloader/youtube-dl/youtube-dl.go +++ b/backend/downloader/youtube-dl/youtube-dl.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "log" "os/exec" "path/filepath" "regexp" @@ -49,19 +50,22 @@ func (d *YoutubeDlDownloader) StartDownload(video *downloader.Video, tasks task. if err != nil { break } - logger.Debug(string(tmp)) - percentage, err := d.getPercentage(string(tmp)) - if err != nil { - panic(err) + log.Println(tasks[video.ID].Percentage) + if tasks[video.ID].Percentage < 100 { + percentage, err := d.getPercentage(string(tmp)) + if err != nil { + panic(err) + } + tasks[video.ID].Percentage = percentage } - tasks[video.ID].Percentage = percentage } logger.Info(fmt.Sprintf("Download of video ID %s COMPLETED.", video.ID)) tasks[video.ID] = &task.Task{ - ID: video.ID, - Status: task.StatusCompleted, - Filename: video.Filename, + ID: video.ID, + Status: task.StatusCompleted, + Filename: video.Filename, + Percentage: 100, } } @@ -84,15 +88,11 @@ func (d *YoutubeDlDownloader) getVideoJson(url string) (*downloader.Video, error } func (d *YoutubeDlDownloader) getPercentage(s string) (float64, error) { - re, err := regexp.Compile(`\[download\]\s+(\d+(\.\d)?)\%`) - if err != nil { - return 0, nil - } - + re := regexp.MustCompile(`\[download\]\s+(\d+(\.\d)?)\%`) match := re.FindStringSubmatch(s) if len(match) < 2 { - return -1, fmt.Errorf("An error occurred in parsing the percentage string %s", s) + return 0, nil } n, err := strconv.ParseFloat(match[1], 64) diff --git a/backend/main_test.go b/backend/main_test.go index 8608912..55598d4 100644 --- a/backend/main_test.go +++ b/backend/main_test.go @@ -32,7 +32,7 @@ func TestRunner(t *testing.T) { func (t *testSuite) BeforeAll() { gin.SetMode(gin.ReleaseMode) - logger.SetLevel(logger.DebugLevel) + logger.SetLevel(logger.Disabled) config = new(Config) err := ReadConfig("config.yaml", config) diff --git a/frontend/src/Task.svelte b/frontend/src/Task.svelte index 5098035..9a6ed51 100644 --- a/frontend/src/Task.svelte +++ b/frontend/src/Task.svelte @@ -9,6 +9,7 @@ let task: { ID: string, Status: number, + Percentage: number, Filename: string } @@ -34,11 +35,9 @@ let downloadUrl: string - onMount(startDownload( - - )) + onMount(startDownload) - async function startDownload(cb: () => string) { + async function startDownload() { const res = await fetch(`${endpoint}/task`, {method: 'POST', body: new URLSearchParams({'url': url})}) videoInfo = await res.json() @@ -69,7 +68,8 @@
{videoInfo.ID}