Support parsing yt-dpl output to display download progress
authorJoey Hess <joeyh@joeyh.name>
Mon, 21 Nov 2022 19:04:36 +0000 (15:04 -0400)
committerSean Whitton <spwhitton@spwhitton.name>
Mon, 21 Nov 2022 23:27:18 +0000 (16:27 -0700)
Before this fix, no progress was displayed when yt-dpl was used.

Sponsored-by: Graham Spencer on Patreon
(cherry picked from commit 43f681d4c15221096975250c0809ded40bf8a5fd)

Annex/YoutubeDl.hs

index ba050d4024dcefd45b1cc9f31953bd64a12fc9f1..c6d0f2253eb527789bb52a07a8a972416dcf7d62 100644 (file)
@@ -265,8 +265,10 @@ supportedScheme uo url = case parseURIRelaxed url of
                _ -> allowedScheme uo u
 
 {- Strategy: Look for chunks prefixed with \r, which look approximately
- - like this:
+ - like this for youtube-dl:
  - "ESC[K[download]  26.6% of 60.22MiB at 254.69MiB/s ETA 00:00"
+ - or for yt-dlp, like this:
+ - "\r[download]   1.8% of    1.14GiB at    1.04MiB/s ETA 18:23"
  - Look at the number before "% of " and the number and unit after,
  - to determine the number of bytes.
  -}
@@ -292,8 +294,11 @@ parseYoutubeDlProgress = go [] . reverse . progresschunks
        calc percent total = round (percent * fromIntegral total / 100)
 
        parsepercent :: String -> Maybe Double
-       parsepercent = readMaybe . reverse . takeWhile (not . isSpace) . reverse
+       parsepercent = readMaybe 
+               . reverse . takeWhile (not . isSpace) . reverse
+               . dropWhile isSpace 
 
-       parsebytes = readSize units . takeWhile (not . isSpace)
+       parsebytes = readSize units . takeWhile (not . isSpace) 
+               . dropWhile isSpace
 
        units = committeeUnits ++ storageUnits