From 10a9aa9391ac2995527557d9d808693b57236603 Mon Sep 17 00:00:00 2001 From: Joris Guyonvarch Date: Sun, 12 Apr 2015 22:46:56 +0200 Subject: Ignore lines beginning with # in the configuration file --- README.md | 1 + src/Ad.hs | 18 +++++++++--------- src/AdListener.hs | 12 +++++++----- src/Config.hs | 3 +++ src/Utils/Text.hs | 13 +++++++++++++ 5 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 src/Utils/Text.hs diff --git a/README.md b/README.md index ceb0648..d2ecb93 100644 --- a/README.md +++ b/README.md @@ -19,5 +19,6 @@ Configuration You provide the url you want to listen in a file named conf, for example: ``` +# This line is a comment url = http://www.leboncoin.fr/locations/offres/ile_de_france/?f=a&th=1 ``` diff --git a/src/Ad.hs b/src/Ad.hs index 7bc66fc..4be47f4 100644 --- a/src/Ad.hs +++ b/src/Ad.hs @@ -1,6 +1,6 @@ module Ad - ( getResumes - , getAds + ( fetchResumes + , fetchAds ) where import Data.Text (Text) @@ -16,15 +16,15 @@ import Model.URL import Parser.Resume import Parser.Detail -getResumes :: URL -> IO (Either Text [Resume]) -getResumes url = fmap parseResumes <$> getPage url +fetchResumes :: URL -> IO (Either Text [Resume]) +fetchResumes url = fmap parseResumes <$> getPage url -getAds :: [Resume] -> IO (Either Text [Ad]) -getAds resumes = do - xs <- sequence $ map getAd resumes +fetchAds :: [Resume] -> IO (Either Text [Ad]) +fetchAds resumes = do + xs <- sequence $ map fetchAd resumes return $ sequence xs -getAd :: Resume -> IO (Either Text Ad) -getAd resume = do +fetchAd :: Resume -> IO (Either Text Ad) +fetchAd resume = do page <- getPage (url resume) fmap (\page -> Ad { resume = resume, detail = parseDetail page}) <$> getPage (url resume) diff --git a/src/AdListener.hs b/src/AdListener.hs index d676b61..dbd7a71 100644 --- a/src/AdListener.hs +++ b/src/AdListener.hs @@ -11,7 +11,7 @@ import qualified Data.Text.IO as T import Control.Concurrent (threadDelay) -import Ad (getResumes, getAds) +import Ad (fetchResumes, fetchAds) import Model.Ad import Model.URL @@ -27,7 +27,7 @@ import qualified Config as C listenToNewAds :: Config -> IO () listenToNewAds config = do - eitherResumes <- getResumes (C.url config) + eitherResumes <- fetchResumes (C.url config) case eitherResumes of Left error -> listenError config [] error @@ -37,7 +37,7 @@ listenToNewAds config = do listenToNewAdsWithViewedURLs :: Config -> [URL] -> IO () listenToNewAdsWithViewedURLs config viewedURLs = do - eitherResumes <- getResumes (C.url config) + eitherResumes <- fetchResumes (C.url config) case eitherResumes of Left error -> listenError config viewedURLs error @@ -48,7 +48,7 @@ listenToNewAdsWithResumes :: Config -> [URL] -> [Resume] -> IO () listenToNewAdsWithResumes config viewedURLs resumes = let (newURLs, newResumes) = getNewResumes viewedURLs resumes in do - eitherNewAds <- getAds newResumes + eitherNewAds <- fetchAds newResumes case eitherNewAds of Left error -> listenError config viewedURLs error @@ -68,7 +68,9 @@ newAdsMessage newAds = T.concat [ "Got " , T.pack . show . length $ newAds - , " new ads." + , " new ad" + , if length newAds > 1 then "s" else "" + , "." ] line = T.map (\_ -> '-') newAdsMessage in T.intercalate diff --git a/src/Config.hs b/src/Config.hs index c0b0bc0..7a44ec0 100644 --- a/src/Config.hs +++ b/src/Config.hs @@ -19,6 +19,8 @@ import System.Directory (doesFileExist) import Model.URL +import Utils.Text + configUsage :: Text configUsage = T.intercalate @@ -53,6 +55,7 @@ configFromFile = . catMaybes . map lineConfig . filter (not . T.null) + . filter (not . startsWith "#") . map T.strip . T.lines diff --git a/src/Utils/Text.hs b/src/Utils/Text.hs new file mode 100644 index 0000000..1297bbd --- /dev/null +++ b/src/Utils/Text.hs @@ -0,0 +1,13 @@ +module Utils.Text + ( startsWith + ) where + +import Data.Text (Text) +import qualified Data.Text as T + +startsWith :: Text -> Text -> Bool +startsWith mbStart text = + case (T.uncons mbStart, T.uncons text) of + (Just (x, xs), Just (y, ys)) -> x == y && startsWith xs ys + (Nothing, _) -> True + _ -> False -- cgit v1.2.3