Last active
August 7, 2018 17:58
-
-
Save dimidd/44610caaf379c489a81b0082c11f718d to your computer and use it in GitHub Desktop.
download hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks to @snoyberg for suggesting
mapConcurrently_
.