From 199404efc365227cc3a94ba7c2c8add8388f6fcc Mon Sep 17 00:00:00 2001 From: Joris Guyonvarch Date: Wed, 15 Apr 2015 23:56:48 +0200 Subject: Refactoring so that code is easier to read --- src/AdListener.hs | 11 ++++++----- src/Mail.hs | 19 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/AdListener.hs b/src/AdListener.hs index 1de56dc..c08efc5 100644 --- a/src/AdListener.hs +++ b/src/AdListener.hs @@ -34,11 +34,12 @@ listenToNewAds config = do eitherResumes <- fetchResumes (C.url config) case eitherResumes of Left error -> - listenError config [] error + showErrorAndListenBack config [] error Right resumes -> let newURLs = map url resumes in do putStrLn "Listening for new ads…" + waitOneMinute listenToNewAdsWithViewedURLs config newURLs listenToNewAdsWithViewedURLs :: Config -> [URL] -> IO () @@ -46,7 +47,7 @@ listenToNewAdsWithViewedURLs config viewedURLs = do eitherResumes <- fetchResumes (C.url config) case eitherResumes of Left error -> - listenError config viewedURLs error + showErrorAndListenBack config viewedURLs error Right resumes -> listenToNewAdsWithResumes config viewedURLs resumes @@ -57,7 +58,7 @@ listenToNewAdsWithResumes config viewedURLs resumes = eitherNewAds <- fetchAds newResumes case eitherNewAds of Left error -> - listenError config viewedURLs error + showErrorAndListenBack config viewedURLs error Right newAds -> do time <- getCurrentFormattedTime @@ -90,8 +91,8 @@ trySendMail config ads = , error ] -listenError :: Config -> [URL] -> Text -> IO () -listenError config viewedURLs error = do +showErrorAndListenBack :: Config -> [URL] -> Text -> IO () +showErrorAndListenBack config viewedURLs error = do T.putStrLn error waitOneMinute listenToNewAdsWithViewedURLs config viewedURLs diff --git a/src/Mail.hs b/src/Mail.hs index 1b15f30..45962a5 100644 --- a/src/Mail.hs +++ b/src/Mail.hs @@ -17,17 +17,18 @@ import Utils.Either (mapLeft) sendMail :: [Text] -> Text -> Text -> IO (Either Text ()) sendMail mailTo subject body = safeSendMail (mail mailTo subject body) +safeSendMail :: Mail -> IO (Either Text ()) +safeSendMail mail = + mapLeft (T.pack . show) <$> (try (renderSendMail mail) :: IO (Either SomeException ())) + mail :: [Text] -> Text -> Text -> Mail mail mailTo subject body = - (emptyMail (address "no-reply@leboncoin-listener.com")) - { mailTo = map address mailTo - , mailParts = [[plainPart (toLazyText . fromText $ body)]] - , mailHeaders = [("Subject", subject)] - } + let fromMail = emptyMail (address "no-reply@leboncoin-listener.com") + in fromMail + { mailTo = map address mailTo + , mailParts = [[plainPart (toLazyText . fromText $ body)]] + , mailHeaders = [("Subject", subject)] + } address :: Text -> Address address mail = Address { addressName = Nothing, addressEmail = mail } - -safeSendMail :: Mail -> IO (Either Text ()) -safeSendMail mail = - mapLeft (T.pack . show) <$> (try (renderSendMail mail) :: IO (Either SomeException ())) -- cgit v1.2.3