Skip to content

Commit

Permalink
Merge pull request #52 from Fransz/master
Browse files Browse the repository at this point in the history
Fixed a cookbook item: Consume a webpage with http conduit
  • Loading branch information
psibi committed Feb 2, 2021
2 parents 4c599e2 + 0894749 commit 3614a6b
Showing 1 changed file with 21 additions and 18 deletions.
@@ -1,23 +1,26 @@
```haskell
import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString as S
import Data.Conduit
import Data.Conduit.Binary as CB
import Network.HTTP.Conduit
import Conduit (ResourceT, runResourceT)
import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString as BS
import Data.Conduit
import Data.Conduit.Binary as CB
import Network.HTTP.Conduit

main :: IO ()
main = withManager $ \manager -> do
req <- parseUrl "http://www.yesodweb.com/"
res <- http req manager
responseBody res $$+- printProgress =$ CB.sinkFile "yesodweb.html"
main = do
man <- newManager tlsManagerSettings
req <- parseRequest "http://www.yesodweb.com/"
runResourceT $ do
res <- http req man
sealConduitT (responseBody res) $$+- printProgress .| CB.sinkFile "yesodweb.html"

printProgress :: Conduit S.ByteString (ResourceT IO) S.ByteString
printProgress =
loop 0
printProgress :: ConduitT BS.ByteString BS.ByteString (ResourceT IO) ()
printProgress = loop 0
where
loop len = await >>= maybe (return ()) (\bs -> do
let len' = len + S.length bs
liftIO $ putStrLn $ "Bytes consumed: " ++ show len'
yield bs
loop len')
```
loop len = await >>= maybe (return ()) (showConsumed len)
showConsumed len bs = do
let len' = len + BS.length bs
liftIO $ putStrLn $ "bytes consumed " ++ show len'
yield bs
loop len'
```

0 comments on commit 3614a6b

Please sign in to comment.