aboutsummaryrefslogtreecommitdiff
path: root/src/lib/haskell/Parser/LeboncoinParser.hs
blob: 3e6775d13548fbaa72debedd2ceda978984c1af5 (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
module Parser.LeboncoinParser
  ( parse
  ) where

import           Data.Maybe        (catMaybes)
import           Data.Text         (Text)
import qualified Data.Text         as T
import           Text.HTML.TagSoup

import           Model.Ad          (Ad (Ad))
import           Parser.Utils

parse :: Text -> [Ad]
parse =
  catMaybes
    . fmap parseAd
    . partitions (~== (T.unpack "<li>"))
    . parseTags

parseAd :: [Tag Text] -> Maybe Ad
parseAd tags = do
  name <- getTagTextAfter "<span data-qa-id=aditem_title>" tags
  location <- getTagTextAfter "<p data-qa-id=aditem_location>" tags
  let price =
        case getTagsBetween "<span itemprop=priceCurrency>" "</span>" tags of
          [] -> Nothing
          xs -> Just $ innerText xs
  url <- getTagAttribute "<a>" "href" tags
  return (Ad name location price (T.concat ["https://www.leboncoin.fr", url]))