aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoris Guyonvarch2015-04-15 23:56:48 +0200
committerJoris Guyonvarch2015-04-15 23:56:48 +0200
commit199404efc365227cc3a94ba7c2c8add8388f6fcc (patch)
tree02959c30655bced2aaf4660b361540864a0c8904 /src
parenta9c9365ec941adcffb1241d967fd00cd889165ff (diff)
downloadad-listener-199404efc365227cc3a94ba7c2c8add8388f6fcc.tar.gz
ad-listener-199404efc365227cc3a94ba7c2c8add8388f6fcc.tar.bz2
ad-listener-199404efc365227cc3a94ba7c2c8add8388f6fcc.zip
Refactoring so that code is easier to read
Diffstat (limited to 'src')
-rw-r--r--src/AdListener.hs11
-rw-r--r--src/Mail.hs19
2 files changed, 16 insertions, 14 deletions
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 ()))