Skip to content

Instantly share code, notes, and snippets.

@dimidd
Last active August 7, 2018 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimidd/44610caaf379c489a81b0082c11f718d to your computer and use it in GitHub Desktop.
Save dimidd/44610caaf379c489a81b0082c11f718d to your computer and use it in GitHub Desktop.
download hs
#!/usr/bin/env stack
-- stack --resolver lts-12.0 script
import Network.HTTP.Simple (parseRequest, httpLbs, getResponseBody)
import qualified Data.ByteString.Lazy as BL (writeFile)
import System.Environment (getArgs)
import Data.Foldable (for_, traverse_)
import Control.Concurrent.Async
-- Get the following program working to download the 4 URLs and save
-- their contents in the given files.
--
-- Bonus points: use the async package to do the downloads in
-- parallel.
downloads :: [(String, FilePath)]
downloads =
[ ("https://raw.githubusercontent.com/haskell-lang/haskell-lang/master/static/tutorial/package-async.md", "async.md")
, ("https://raw.githubusercontent.com/haskell-lang/haskell-lang/master/static/tutorial/package-binary.md", "binary.md")
, ("https://raw.githubusercontent.com/haskell-lang/haskell-lang/master/static/tutorial/package-conduit.md", "conduit.md")
, ("https://raw.githubusercontent.com/haskell-lang/haskell-lang/master/static/tutorial/package-containers.md", "containers.md")
]
download :: String -> FilePath -> IO ()
download url fp = do
req <- parseRequest url
res <- httpLbs req
BL.writeFile fp $ getResponseBody res
downloadAll :: [(String, FilePath)] -> IO ()
downloadAll = mapConcurrently_ (uncurry download)
main :: IO ()
main = downloadAll downloads
@dimidd
Copy link
Author

dimidd commented Aug 7, 2018

Thanks to @snoyberg for suggesting mapConcurrently_.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment