{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
module Web.WikiCFP.Scraper
(
scrapeConfEvents
, scrapeSearchEvents
, ErrorMsg
, HTML (..)
, When (..)
, Event (..)
) where
import qualified Data.ByteString as SB
import qualified Data.ByteString.Lazy as LB
import Data.Text (Text, pack)
import Data.Text.Encoding (decodeUtf8')
import qualified Data.Text.Lazy as LT
import Text.HTML.Scalpel.Core (scrapeStringLike)
import Web.WikiCFP.Scraper.Scalpel (ErrorMsg, Scraper', confRoot, searchRoot)
import Web.WikiCFP.Scraper.Type (Event (..), When (..))
class HTML a where
decodeToText :: a -> Either ErrorMsg Text
instance HTML Text where
decodeToText :: Text -> Either ErrorMsg Text
decodeToText = forall a b. b -> Either a b
Right
instance HTML LT.Text where
decodeToText :: Text -> Either ErrorMsg Text
decodeToText = forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
LT.toStrict
instance HTML SB.ByteString where
decodeToText :: ByteString -> Either ErrorMsg Text
decodeToText = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\UnicodeException
e -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ ErrorMsg
"UTF-8 decoding error: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> ErrorMsg
show UnicodeException
e) forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either UnicodeException Text
decodeUtf8'
instance HTML LB.ByteString where
decodeToText :: ByteString -> Either ErrorMsg Text
decodeToText = forall a. HTML a => a -> Either ErrorMsg Text
decodeToText forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
LB.toStrict
instance HTML String where
decodeToText :: ErrorMsg -> Either ErrorMsg Text
decodeToText = forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorMsg -> Text
pack
runScraper :: Scraper' (Either ErrorMsg a) -> Text -> Either ErrorMsg a
runScraper :: forall a. Scraper' (Either ErrorMsg a) -> Text -> Either ErrorMsg a
runScraper Scraper' (Either ErrorMsg a)
s Text
input = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a b. a -> Either a b
Left ErrorMsg
"Scraping error") forall a. a -> a
id forall a b. (a -> b) -> a -> b
$ forall str a. StringLike str => str -> Scraper str a -> Maybe a
scrapeStringLike Text
input Scraper' (Either ErrorMsg a)
s
scrapeConfEvents :: HTML input => input -> Either ErrorMsg [Event]
scrapeConfEvents :: forall input. HTML input => input -> Either ErrorMsg [Event]
scrapeConfEvents input
t = forall a. Scraper' (Either ErrorMsg a) -> Text -> Either ErrorMsg a
runScraper Scraper' (Either ErrorMsg [Event])
confRoot forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. HTML a => a -> Either ErrorMsg Text
decodeToText input
t
scrapeSearchEvents :: HTML input => input -> Either ErrorMsg [Event]
scrapeSearchEvents :: forall input. HTML input => input -> Either ErrorMsg [Event]
scrapeSearchEvents input
t = forall a. Scraper' (Either ErrorMsg a) -> Text -> Either ErrorMsg a
runScraper Scraper' (Either ErrorMsg [Event])
searchRoot forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. HTML a => a -> Either ErrorMsg Text
decodeToText input
t