aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoris Guyonvarch2015-04-12 23:16:30 +0200
committerJoris Guyonvarch2015-04-12 23:16:30 +0200
commitc0cfd3cb3d7a963cebb40f88868e5628f361376f (patch)
tree94f767f6df9b1c0dab58b9f186e5e9085097da8b
parent10a9aa9391ac2995527557d9d808693b57236603 (diff)
Show the current time when new ads are available
-rw-r--r--leboncoin-listener.cabal1
-rw-r--r--src/AdListener.hs14
-rw-r--r--src/Time.hs17
3 files changed, 27 insertions, 5 deletions
diff --git a/leboncoin-listener.cabal b/leboncoin-listener.cabal
index e1ae0af..2a3789e 100644
--- a/leboncoin-listener.cabal
+++ b/leboncoin-listener.cabal
@@ -10,5 +10,6 @@ executable leboncoin-listener
, text == 1.2.0.4
, containers == 0.5.6.3
, directory == 1.2.2.1
+ , time == 1.5.0.1
, HTTP == 4000.2.19
, tagsoup == 0.13.3
diff --git a/src/AdListener.hs b/src/AdListener.hs
index dbd7a71..0155074 100644
--- a/src/AdListener.hs
+++ b/src/AdListener.hs
@@ -25,6 +25,8 @@ 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)
@@ -54,23 +56,25 @@ listenToNewAdsWithResumes config viewedURLs resumes =
listenError config viewedURLs error
Right newAds ->
do
+ time <- getCurrentFormattedTime
if not (null newAds)
then
- T.putStrLn (newAdsMessage newAds)
+ T.putStrLn (newAdsMessage time newAds)
else
return ()
waitOneMinute
listenToNewAdsWithViewedURLs config (viewedURLs ++ newURLs)
-newAdsMessage :: [Ad] -> Text
-newAdsMessage newAds =
+newAdsMessage :: Text -> [Ad] -> Text
+newAdsMessage time newAds =
let newAdsMessage =
T.concat
- [ "Got "
+ [ "\nAt "
+ , time
+ , ", got "
, T.pack . show . length $ newAds
, " new ad"
, if length newAds > 1 then "s" else ""
- , "."
]
line = T.map (\_ -> '-') newAdsMessage
in T.intercalate
diff --git a/src/Time.hs b/src/Time.hs
new file mode 100644
index 0000000..9f35bf0
--- /dev/null
+++ b/src/Time.hs
@@ -0,0 +1,17 @@
+module Time
+ ( getCurrentFormattedTime
+ ) where
+
+import Data.Text (Text)
+import qualified Data.Text as T
+
+import Data.Time.Clock (getCurrentTime)
+import Data.Time.LocalTime (getCurrentTimeZone, utcToLocalTime)
+import Data.Time.Format (formatTime, defaultTimeLocale)
+
+getCurrentFormattedTime :: IO Text
+getCurrentFormattedTime = do
+ currentTime <- getCurrentTime
+ timeZone <- getCurrentTimeZone
+ let localTime = utcToLocalTime timeZone currentTime
+ return (T.pack $ formatTime defaultTimeLocale "%T" localTime)