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