diff options
author | Joris Guyonvarch | 2015-04-11 20:38:08 +0200 |
---|---|---|
committer | Joris Guyonvarch | 2015-04-11 20:38:08 +0200 |
commit | aafc45eb5eed3839a5210a7d48928d975df6a296 (patch) | |
tree | 8574205bdf967b7338e63bc77c2f1374e0843e67 /src/AdListener.hs | |
parent | 4ddd6d1f6df2bab75d42b6d45b816e92e7173529 (diff) |
Handle a configuration file to save the url
Diffstat (limited to 'src/AdListener.hs')
-rw-r--r-- | src/AdListener.hs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/AdListener.hs b/src/AdListener.hs new file mode 100644 index 0000000..1f97e30 --- /dev/null +++ b/src/AdListener.hs @@ -0,0 +1,79 @@ +{-# LANGUAGE OverloadedStrings #-} + +module AdListener + ( listenToNewAds + ) where + +import Data.List (intersperse) +import Data.Text (Text) +import qualified Data.Text as T +import qualified Data.Text.IO as T + +import Control.Concurrent (threadDelay) + +import Ad (getResumes, getAds) + +import Model.Ad +import Model.URL +import Model.Resume + +import View.Ad (renderAds) + +import Page +import Parser.Detail + +import Config (Config) +import qualified Config as C + +listenToNewAds :: Config -> [Ad] -> IO () +listenToNewAds config viewedAds = do + eitherResumes <- getResumes (C.url config) + case eitherResumes of + Left error -> + listenError config viewedAds error + Right resumes -> + listenToNewAdsWithResumes config viewedAds resumes + +listenToNewAdsWithResumes :: Config -> [Ad] -> [Resume] -> IO () +listenToNewAdsWithResumes config viewedAds resumes = + let viewedURLs = getURLs $ map resume viewedAds + newResumes = getNewResumes viewedURLs resumes + in do + eitherNewAds <- getAds newResumes + case eitherNewAds of + Left error -> + listenError config viewedAds error + Right newAds -> + do + if not (null newAds) + then + T.putStrLn (newAdsMessage newAds) + else + return () + waitOneMinute + listenToNewAds config (viewedAds ++ newAds) + +newAdsMessage :: [Ad] -> Text +newAdsMessage newAds = + let newAdsMessage = + T.concat + [ "Got " + , T.pack . show . length $ newAds + , " new ads." + ] + line = T.map (\_ -> '-') newAdsMessage + in T.intercalate + "\n" + [ newAdsMessage + , T.concat [line, "\n"] + , renderAds newAds + ] + +listenError :: Config -> [Ad] -> Text -> IO () +listenError config viewedAds error = do + T.putStrLn error + waitOneMinute + listenToNewAds config viewedAds + +waitOneMinute :: IO () +waitOneMinute = threadDelay (1000 * 1000 * 60) |