diff options
author | Joris Guyonvarch | 2015-04-11 14:06:14 +0200 |
---|---|---|
committer | Joris Guyonvarch | 2015-04-11 14:06:14 +0200 |
commit | 9f389a05cc883213327b8d17db6d23c3ff8fb4e1 (patch) | |
tree | 36185d54d0695be0c812e7917ac5e919a3e8d045 /src/Main.hs | |
parent | 2f90280ae5059833b5e533e823b1c01338990cb1 (diff) |
Set up a listener that diff new ads and show only the new ones
Diffstat (limited to 'src/Main.hs')
-rw-r--r-- | src/Main.hs | 56 |
1 files changed, 46 insertions, 10 deletions
diff --git a/src/Main.hs b/src/Main.hs index f352a43..3f2dd37 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -4,24 +4,60 @@ module Main import Data.List (intersperse) -import Ad (getAds) +import Control.Concurrent (threadDelay) + +import Ad (getResumes) import Model.Ad +import Model.URL +import Model.Resume (getURLs, getNewResumes) -import View.Ad (renderAd) +import View.Ad (renderResume) import Page import Parser.Detail -url :: String +url :: URL url = "http://www.leboncoin.fr/annonces/offres/corse/" main :: IO () -main = do - eitherAds <- getAds url - case eitherAds of - Left error -> +main = initListenToNewResumes + +initListenToNewResumes :: IO () +initListenToNewResumes = do + eitherResumes <- getResumes url + case eitherResumes of + Left error -> do putStrLn error - Right ads -> do - writeFile "result.html" (concat . intersperse "\n\n" . map renderAd $ ads) - putStrLn "Done!" + waitOneMinute + initListenToNewResumes + Right resumes -> + let viewedURLs = getURLs resumes + in do + putStrLn "Initialization complete" + waitOneMinute + listenToNewResumes viewedURLs + +listenToNewResumes :: [URL] -> IO () +listenToNewResumes viewedURLs = do + eitherResumes <- getResumes url + case eitherResumes of + Left error -> do + putStrLn error + waitOneMinute + listenToNewResumes viewedURLs + Right resumes -> + let (newViewdURLs, newResumes) = getNewResumes viewedURLs resumes + newAdsCount = length newResumes + in do + if newAdsCount > 0 + then do + putStrLn ("Got " ++ (show newAdsCount) ++ " new ads.\n") + putStrLn (concat . intersperse "\n\n" . map renderResume $ newResumes) + else + return () + waitOneMinute + listenToNewResumes newViewdURLs + +waitOneMinute :: IO () +waitOneMinute = threadDelay (1000 * 1000 * 60) |