aboutsummaryrefslogtreecommitdiff
path: root/src/AdListener.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/AdListener.hs')
-rw-r--r--src/AdListener.hs77
1 files changed, 26 insertions, 51 deletions
diff --git a/src/AdListener.hs b/src/AdListener.hs
index 3db4c6a..d8400d8 100644
--- a/src/AdListener.hs
+++ b/src/AdListener.hs
@@ -6,14 +6,11 @@ module AdListener
import Prelude hiding (error)
-import Data.Text (Text)
import qualified Data.Text.IO as T
-import qualified Data.Text.Lazy as LT
-import Data.Text.Lazy.Builder (toLazyText, fromText)
import Control.Concurrent (threadDelay)
-import Fetch (fetchResumes, fetchAds)
+import qualified Fetch
import Model.Ad
import Model.URL
@@ -32,61 +29,39 @@ import Time (getCurrentFormattedTime)
start :: Conf -> IO ()
start conf = do
- eitherResumes <- fetchResumes (Conf.url conf)
- case eitherResumes of
- Left error ->
- showErrorAndListenBack conf [] error
- Right resumes -> do
- let newURLs = map url resumes
- T.putStrLn "Listening to new ads…"
- waitListenInterval conf
- listenToNewAdsWithViewedURLs conf newURLs
+ resumes <- Fetch.resumes . Conf.urls $ conf
+ let newURLs = map url resumes
+ T.putStrLn "Listening to new ads…"
+ waitListenInterval conf
+ listenToNewAdsWithViewedURLs conf newURLs
listenToNewAdsWithViewedURLs :: Conf -> [URL] -> IO ()
listenToNewAdsWithViewedURLs conf viewedURLs = do
- eitherResumes <- fetchResumes (Conf.url conf)
- case eitherResumes of
- Left error ->
- showErrorAndListenBack conf viewedURLs error
- Right resumes ->
- listenToNewAdsWithResumes conf viewedURLs resumes
-
-listenToNewAdsWithResumes :: Conf -> [URL] -> [Resume] -> IO ()
-listenToNewAdsWithResumes conf viewedURLs resumes =
+ resumes <- Fetch.resumes . Conf.urls $ conf
let (newURLs, newResumes) = getNewResumes viewedURLs resumes
- in do
- eitherNewAds <- fetchAds newResumes
- case eitherNewAds of
- Left error ->
- showErrorAndListenBack conf viewedURLs error
- Right newAds -> do
- time <- getCurrentFormattedTime
- if not (null newAds)
- then
- let message = P.renderConsoleAds conf time newAds
- in do
- T.putStrLn message
- trySendMail conf newAds
- else
- return ()
- waitListenInterval conf
- listenToNewAdsWithViewedURLs conf (viewedURLs ++ newURLs)
+ eitherNewAds <- Fetch.ads newResumes
+ case eitherNewAds of
+ Left error -> do
+ T.putStrLn error
+ waitListenInterval conf
+ listenToNewAdsWithViewedURLs conf viewedURLs
+ Right newAds -> do
+ time <- getCurrentFormattedTime
+ if not (null newAds)
+ then
+ let message = P.renderConsoleAds conf time newAds
+ in T.putStrLn message >> sendMail conf newAds
+ else
+ return ()
+ waitListenInterval conf
+ listenToNewAdsWithViewedURLs conf (viewedURLs ++ newURLs)
-trySendMail :: Conf -> [Ad] -> IO ()
-trySendMail conf ads =
+sendMail :: Conf -> [Ad] -> IO ()
+sendMail conf ads =
let (title, plainBody) = P.renderAds conf ads
htmlBody = H.renderAds conf ads
- mail = Mail (Conf.mailFrom conf) (Conf.mailTo conf) title (strictToLazy plainBody) (strictToLazy htmlBody)
+ mail = Mail (Conf.mailFrom conf) (Conf.mailTo conf) title plainBody htmlBody
in Mail.send mail >> return ()
-strictToLazy :: Text -> LT.Text
-strictToLazy = toLazyText . fromText
-
-showErrorAndListenBack :: Conf -> [URL] -> Text -> IO ()
-showErrorAndListenBack conf viewedURLs error = do
- T.putStrLn error
- waitListenInterval conf
- listenToNewAdsWithViewedURLs conf viewedURLs
-
waitListenInterval :: Conf -> IO ()
waitListenInterval = threadDelay . (*) 1000000 . round . Conf.listenInterval