1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
{-# 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 Fetch (fetchResumes, fetchAds)
import Model.Ad
import Model.URL
import Model.Resume
import View.Ad (renderAds)
import Page
import Parser.Detail
import Mail (sendMail)
import Config (Config)
import qualified Config as C
import Time (getCurrentFormattedTime)
listenToNewAds :: Config -> IO ()
listenToNewAds config = do
eitherResumes <- fetchResumes (C.url config)
case eitherResumes of
Left error ->
listenError config [] error
Right resumes ->
let newURLs = map url resumes
in do
putStrLn "Listening for new ads…"
listenToNewAdsWithViewedURLs config newURLs
listenToNewAdsWithViewedURLs :: Config -> [URL] -> IO ()
listenToNewAdsWithViewedURLs config viewedURLs = do
eitherResumes <- fetchResumes (C.url config)
case eitherResumes of
Left error ->
listenError config viewedURLs error
Right resumes ->
listenToNewAdsWithResumes config viewedURLs resumes
listenToNewAdsWithResumes :: Config -> [URL] -> [Resume] -> IO ()
listenToNewAdsWithResumes config viewedURLs resumes =
let (newURLs, newResumes) = getNewResumes viewedURLs resumes
in do
eitherNewAds <- fetchAds newResumes
case eitherNewAds of
Left error ->
listenError config viewedURLs error
Right newAds ->
do
time <- getCurrentFormattedTime
if not (null newAds)
then
let message = newAdsMessage time newAds
in do
T.putStrLn message
trySendMail config message
else
return ()
waitOneMinute
listenToNewAdsWithViewedURLs config (viewedURLs ++ newURLs)
trySendMail :: Config -> Text -> IO ()
trySendMail config message =
case C.mailTo config of
Just mailTo ->
do
eitherMailSuccess <- sendMail mailTo message
case eitherMailSuccess of
Right () ->
putStrLn "Mail sent."
Left error ->
T.putStrLn . T.concat $
[ "Error sending mail: "
, error
]
Nothing ->
return ()
newAdsMessage :: Text -> [Ad] -> Text
newAdsMessage time newAds =
let newAdsMessage =
T.concat
[ "\nAt "
, time
, ", got "
, T.pack . show . length $ newAds
, " new ad"
, if length newAds > 1 then "s" else ""
]
line = T.map (\_ -> '-') newAdsMessage
in T.intercalate
"\n"
[ newAdsMessage
, T.concat [line, "\n"]
, renderAds newAds
]
listenError :: Config -> [URL] -> Text -> IO ()
listenError config viewedURLs error = do
T.putStrLn error
waitOneMinute
listenToNewAdsWithViewedURLs config viewedURLs
waitOneMinute :: IO ()
waitOneMinute = threadDelay (1000 * 1000 * 60)
|