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/Main.hs | |
parent | 4ddd6d1f6df2bab75d42b6d45b816e92e7173529 (diff) |
Handle a configuration file to save the url
Diffstat (limited to 'src/Main.hs')
-rw-r--r-- | src/Main.hs | 80 |
1 files changed, 10 insertions, 70 deletions
diff --git a/src/Main.hs b/src/Main.hs index 6208953..ad70402 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -4,78 +4,18 @@ module Main ( main ) where -import Data.List (intersperse) -import qualified Data.Text as T import qualified Data.Text.IO as T -import Control.Concurrent (threadDelay) +import AdListener (listenToNewAds) -import Ad (getResumes, getAds) - -import Model.Ad -import Model.URL -import Model.Resume - -import View.Ad (renderAds) - -import Page -import Parser.Detail - -listenURL :: URL -listenURL = "http://www.leboncoin.fr/annonces/offres/corse/" +import Config (Config) +import qualified Config as C main :: IO () -main = listenToNewAds [] - -listenToNewAds :: [Ad] -> IO () -listenToNewAds viewedAds = do - eitherResumes <- getResumes listenURL - case eitherResumes of - Left error -> - listenError viewedAds error - Right resumes -> - listenToNewAdsWithResumes viewedAds resumes - -listenToNewAdsWithResumes :: [Ad] -> [Resume] -> IO () -listenToNewAdsWithResumes viewedAds resumes = - let viewedURLs = getURLs $ map resume viewedAds - newResumes = getNewResumes viewedURLs resumes - in do - eitherNewAds <- getAds newResumes - case eitherNewAds of - Left error -> - listenError viewedAds error - Right newAds -> - do - if not (null newAds) - then - T.putStrLn (newAdsMessage newAds) - else - return () - waitOneMinute - listenToNewAds (viewedAds ++ newAds) - -newAdsMessage :: [Ad] -> T.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 :: [Ad] -> T.Text -> IO () -listenError viewedAds error = do - T.putStrLn error - waitOneMinute - listenToNewAds viewedAds - -waitOneMinute :: IO () -waitOneMinute = threadDelay (1000 * 1000 * 60) +main = do + maybeConfig <- C.getConfig + case maybeConfig of + Just config -> + listenToNewAds config [] + Nothing -> + T.putStrLn C.configUsage |