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