blob: 1de56dc7f31d6acfcc1972f245ee9683576e8fa5 (
plain)
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
|
{-# 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
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 = renderConsoleAds time newAds
in do
T.putStrLn message
trySendMail config newAds
else
return ()
waitOneMinute
listenToNewAdsWithViewedURLs config (viewedURLs ++ newURLs)
trySendMail :: Config -> [Ad] -> IO ()
trySendMail config ads =
case C.mailTo config of
Nothing ->
return ()
Just mailTo ->
let (title, message) = renderAds ads
in do
eitherMailSuccess <- sendMail mailTo title message
case eitherMailSuccess of
Right () ->
putStrLn "\nMail sent."
Left error ->
T.putStrLn . T.concat $
[ "\nError sending mail: "
, error
]
listenError :: Config -> [URL] -> Text -> IO ()
listenError config viewedURLs error = do
T.putStrLn error
waitOneMinute
listenToNewAdsWithViewedURLs config viewedURLs
waitOneMinute :: IO ()
waitOneMinute = threadDelay (1000 * 1000 * 60)
|