Add percentage

This commit is contained in:
Andrea Fazzi 2022-02-24 17:10:37 +01:00
parent 8f72f51810
commit 4825510c98
3 changed files with 20 additions and 20 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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 @@
<div class="pb-2 flex-grow-1 bd-highlight"><h5>{videoInfo.Title}</h5></div>
<div class="pb-2 px-2 bd-highlight"><small>{videoInfo.Duration} seconds</small></div>
{#if task}
<div class="pb-2 px-2 bd-highlight"><span class={statusBadge[task.Status].class}>{statusBadge[task.Status].text}</span></div>
<!-- <div class="pb-2 px-2 bd-highlight"><span class={statusBadge[task.Status].class}>{statusBadge[task.Status].text}</span></div> -->
<div class="pb-2 px-2 bd-highlight"><span class={statusBadge[task.Status].class}>{task.Percentage}%</span></div>
{/if}
</div>
<p><small>{videoInfo.ID}</small></p>