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 (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 = Right
instance HTML LT.Text where
decodeToText = Right . LT.toStrict
instance HTML SB.ByteString where
decodeToText = either (\e -> Left $ "UTF-8 decoding error: " ++ show e) Right . decodeUtf8'
instance HTML LB.ByteString where
decodeToText = decodeToText . LB.toStrict
instance HTML String where
decodeToText = Right . pack
runScraper :: Scraper' (Either ErrorMsg a) -> Text -> Either ErrorMsg a
runScraper s input = maybe (Left "Scraping error") id $ scrapeStringLike input s
scrapeConfEvents :: HTML input => input -> Either ErrorMsg [Event]
scrapeConfEvents t = runScraper confRoot =<< decodeToText t
scrapeSearchEvents :: HTML input => input -> Either ErrorMsg [Event]
scrapeSearchEvents t = runScraper searchRoot =<< decodeToText t