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" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -49,19 +50,22 @@ func (d *YoutubeDlDownloader) StartDownload(video *downloader.Video, tasks task.
if err != nil { if err != nil {
break break
} }
logger.Debug(string(tmp)) log.Println(tasks[video.ID].Percentage)
percentage, err := d.getPercentage(string(tmp)) if tasks[video.ID].Percentage < 100 {
if err != nil { percentage, err := d.getPercentage(string(tmp))
panic(err) 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)) logger.Info(fmt.Sprintf("Download of video ID %s COMPLETED.", video.ID))
tasks[video.ID] = &task.Task{ tasks[video.ID] = &task.Task{
ID: video.ID, ID: video.ID,
Status: task.StatusCompleted, Status: task.StatusCompleted,
Filename: video.Filename, 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) { func (d *YoutubeDlDownloader) getPercentage(s string) (float64, error) {
re, err := regexp.Compile(`\[download\]\s+(\d+(\.\d)?)\%`) re := regexp.MustCompile(`\[download\]\s+(\d+(\.\d)?)\%`)
if err != nil {
return 0, nil
}
match := re.FindStringSubmatch(s) match := re.FindStringSubmatch(s)
if len(match) < 2 { 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) n, err := strconv.ParseFloat(match[1], 64)

View file

@ -32,7 +32,7 @@ func TestRunner(t *testing.T) {
func (t *testSuite) BeforeAll() { func (t *testSuite) BeforeAll() {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
logger.SetLevel(logger.DebugLevel) logger.SetLevel(logger.Disabled)
config = new(Config) config = new(Config)
err := ReadConfig("config.yaml", config) err := ReadConfig("config.yaml", config)

View file

@ -9,6 +9,7 @@
let task: { let task: {
ID: string, ID: string,
Status: number, Status: number,
Percentage: number,
Filename: string Filename: string
} }
@ -34,11 +35,9 @@
let downloadUrl: string 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})}) const res = await fetch(`${endpoint}/task`, {method: 'POST', body: new URLSearchParams({'url': url})})
videoInfo = await res.json() 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 flex-grow-1 bd-highlight"><h5>{videoInfo.Title}</h5></div>
<div class="pb-2 px-2 bd-highlight"><small>{videoInfo.Duration} seconds</small></div> <div class="pb-2 px-2 bd-highlight"><small>{videoInfo.Duration} seconds</small></div>
{#if task} {#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} {/if}
</div> </div>
<p><small>{videoInfo.ID}</small></p> <p><small>{videoInfo.ID}</small></p>