Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View progbar.hs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
module Main where
 
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C8
import System.IO.Streams (InputStream, OutputStream)
import qualified System.IO.Streams as S
import Network.Http.Client
import Text.Printf (printf)
 
withProgressBar :: Integer -> InputStream ByteString -> OutputStream ByteString -> IO ()
withProgressBar fileSize inS outS = go (0 :: Int)
where
go blocksRead = do
block <- S.read inS
case block of
(Just d) -> do
let currentBlocks = blocksRead + B.length d
let percentage = fromIntegral (currentBlocks * 100) / fromIntegral fileSize
printf "%10d [%3.2f%%]\r" currentBlocks (percentage :: Double)
S.write (Just d) outS >> go currentBlocks
Nothing -> return ()
 
-- Note for the Windows user: you need withSocketsDo before get, like
-- withSocketsDo $ get ...
downloadFile :: URL -> FilePath -> IO ()
downloadFile url name = get url $ \response inStream ->
case getStatusCode response of
200 -> let fileSize = maybe 0 (\fs -> read (C8.unpack fs) :: Integer)
(getHeader response "Content-Length")
in S.withFileAsOutput name (withProgressBar fileSize inStream)
code -> error $ "Failed to download " ++ name ++ ": http response returned " ++ show code
 
main :: IO ()
main = do
let url = "http://audacity.googlecode.com/files/audacity-macosx-ub-2.0.3.dmg"
print $ "Downloading " ++ url
downloadFile (C8.pack url) "audacity.dmg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.