diff --git a/internal/dl/progress.go b/internal/dl/progress.go index 8bd482c..b754d41 100644 --- a/internal/dl/progress.go +++ b/internal/dl/progress.go @@ -11,7 +11,7 @@ import ( "uptodownBot/internal/domain" ) -var progressRE = regexp.MustCompile(`\[\w+\]\s+([\d.]+)%\s+of\s+~?([\d.]+)(\w+)`) +var progressRE = regexp.MustCompile(`\[\w+\]\s+([\d.]+)%\s+of\s+~?\s*([\d.]+)(\w+)`) var progressTemplateRE = regexp.MustCompile(`^DLPROG\|\s*([\d.]+)%\|\s*(.*?)\s*\|(.*)$`) type progressUpdate struct { @@ -71,15 +71,28 @@ func parseProgressLine(line string) *progressUpdate { rest = rest[idx+1:] } if atIdx := strings.Index(rest, " at "); atIdx >= 0 { - afterAt := rest[atIdx+4:] + afterAt := strings.TrimSpace(rest[atIdx+4:]) if spaceIdx := strings.Index(afterAt, " "); spaceIdx >= 0 { - u.speed = strings.TrimSpace(afterAt[:spaceIdx]) + speedVal := afterAt[:spaceIdx] + if !strings.EqualFold(speedVal, "unknown") && speedVal != "N/A" { + u.speed = speedVal + } rest = afterAt[spaceIdx:] + } else { + if !strings.EqualFold(afterAt, "unknown") && afterAt != "N/A" { + u.speed = afterAt + } + rest = "" } } if etaIdx := strings.Index(rest, "ETA "); etaIdx >= 0 { etaStr := strings.TrimSpace(rest[etaIdx+4:]) - u.eta = etaStr + if parenPos := strings.Index(etaStr, "("); parenPos > 0 { + etaStr = strings.TrimSpace(etaStr[:parenPos]) + } + if !strings.EqualFold(etaStr, "unknown") && etaStr != "N/A" { + u.eta = etaStr + } } return u diff --git a/internal/dl/progress_test.go b/internal/dl/progress_test.go index f512d31..95cf316 100644 --- a/internal/dl/progress_test.go +++ b/internal/dl/progress_test.go @@ -67,6 +67,27 @@ func TestParseProgressLine(t *testing.T) { eta: "00:02", hasRes: true, }, + { + line: "[download] 100.0% of ~ 1.00KiB at 976.20B/s ETA Unknown (frag 0/18)", + pct: 100.0, + speed: "976.20B/s", + eta: "", + hasRes: true, + }, + { + line: "[download] 16.7% of ~ 18.00KiB at 976.20B/s ETA Unknown (frag 0/18)", + pct: 16.7, + speed: "976.20B/s", + eta: "", + hasRes: true, + }, + { + line: "[download] 11.1% of ~ 8.34MiB at 716.47KiB/s ETA 00:08 (frag 1/18)", + pct: 11.1, + speed: "716.47KiB/s", + eta: "00:08", + hasRes: true, + }, { line: "[download] 1.2% of unknown size at 0.00B/s ETA Unknown", hasRes: false, @@ -196,6 +217,9 @@ func TestParseProgressLineEdgeCases(t *testing.T) { {"[download] 0.0% of 1.00GiB at 0.00B/s ETA 00:00", "multiple spaces", true}, {"[download] 100.0% of 1.00GiB at 999.99MiB/s ETA 00:00", "high speed", true}, {"[download] 100.0% of 999.99TiB at 1.00TiB/s ETA 00:00", "large numbers", true}, + {"[download] 100.0% of ~ 1.00KiB at 976.20B/s ETA Unknown (frag 0/18)", "hls frag first", true}, + {"[download] 16.7% of ~ 18.00KiB at 976.20B/s ETA Unknown (frag 0/18)", "hls frag", true}, + {"[download] 11.1% of ~ 8.34MiB at 716.47KiB/s ETA 00:08 (frag 1/18)", "hls frag with eta", true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/dl/ytdlp.go b/internal/dl/ytdlp.go index a34dd9f..c7c401d 100644 --- a/internal/dl/ytdlp.go +++ b/internal/dl/ytdlp.go @@ -283,9 +283,8 @@ func (c *Client) StartDownload(job *domain.DownloadJob, downloadDir string) (<-c outputPath := filepath.Join(downloadDir, job.ID+"_%(id)s.%(ext)s") args = append(args, "--output", outputPath) - // Progress reporting using progress-template for machine-readable output + // Use --newline so each progress line is newline-delimited in pipe args = append(args, "--newline") - args = append(args, "--progress-template", "DLPROG|%(progress._percent_str)s|%(progress._speed_str)s|%(progress._eta_str)s") // Language selection if specified if job.Language != "" {