aboutsummaryrefslogtreecommitdiff
path: root/src/Parser/Utils.hs
diff options
context:
space:
mode:
authorJoris Guyonvarch2015-04-11 11:50:48 +0200
committerJoris Guyonvarch2015-04-11 12:03:22 +0200
commit5977e1454d7738ddb086d37b20337e350e380790 (patch)
treee03261144e3d534434242c1dd037c2a4e4db5a9f /src/Parser/Utils.hs
downloadad-listener-5977e1454d7738ddb086d37b20337e350e380790.tar.gz
ad-listener-5977e1454d7738ddb086d37b20337e350e380790.tar.bz2
ad-listener-5977e1454d7738ddb086d37b20337e350e380790.zip
Fetch first page ads of a given leboncoin url, fetch also the description page of each item.
Diffstat (limited to 'src/Parser/Utils.hs')
-rw-r--r--src/Parser/Utils.hs30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/Parser/Utils.hs b/src/Parser/Utils.hs
new file mode 100644
index 0000000..4864e00
--- /dev/null
+++ b/src/Parser/Utils.hs
@@ -0,0 +1,30 @@
+module Parser.Utils
+ ( getTagAttribute
+ , getTagText
+ ) where
+
+import Data.List (find, findIndex)
+import Data.Maybe (listToMaybe)
+
+import Text.HTML.TagSoup
+
+getTagAttribute :: String -> String -> [Tag String] -> Maybe String
+getTagAttribute selector attribute item =
+ find (~== selector) item >>= maybeTagAttribute attribute
+
+getTagText :: String -> [Tag String] -> Maybe String
+getTagText selector item =
+ case findIndex (~== selector) item of
+ Just index -> fmap trim $ safeGetAt (index + 1) item >>= maybeTagText
+ Nothing -> Nothing
+
+maybeTagAttribute :: String -> Tag String -> Maybe String
+maybeTagAttribute name (TagOpen _ xs) =
+ fmap snd . find (\(x, _) -> x == name) $ xs
+maybeTagAttribute attribute _ = Nothing
+
+trim :: String -> String
+trim = unwords . words
+
+safeGetAt :: Int -> [a] -> Maybe a
+safeGetAt index = listToMaybe . drop index