Book Image

Haskell Data Analysis Cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis Cookbook

By: Nishant Shukla

Overview of this book

Table of Contents (19 chapters)
Haskell Data Analysis Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Polling a web server for latest updates


Some websites change dramatically very often. For instance, Google News and Reddit are usually loaded with recent postings the moment we refresh the page. To maintain the latest data at all times, it might be best to run an HTTP request often.

In this recipe, we poll new Reddit posts every 10 seconds as summarized in the following diagram:

How to do it…

In a new file called Main.hs, perform the following steps:

  1. Import the relevant libraries:

    import Network.HTTP
    import Control.Concurrent (threadDelay)
    import qualified Data.Text as T
  2. Define the URL to poll:

    url = "http://www.reddit.com/r/pics/new.json"
  3. Define the function to obtain the latest data from an HTTP GET request:

    latest :: IO String
    
    latest = simpleHTTP (getRequest url) >>= getResponseBody
  4. Polling is simply the act of recursively conducting a task after waiting for a specified amount of time. In this case, we will wait 10 seconds before asking for the latest web data:

    poll :: IO a
    
    poll = do
     ...