blob: 0155074fb87476ba9398bf7521b648d65e97d7cf (
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
|
{-# 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 Ad (fetchResumes, fetchAds)
import Model.Ad
import Model.URL
import Model.Resume
import View.Ad (renderAds)
import Page
import Parser.Detail
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 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
T.putStrLn (newAdsMessage time newAds)
else
return ()
waitOneMinute
listenToNewAdsWithViewedURLs config (viewedURLs ++ newURLs)
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)
|