module Webrexp (
evalWebRexp
, evalWebRexpDepthFirst
, parseWebRexp
, evalParsedWebRexp
, Conf (..)
, defaultConf
, evalWebRexpWithConf
) where
import Control.Monad
import Control.Monad.IO.Class
import Text.Parsec
import System.IO
import System.Exit
import Webrexp.Exprtypes
import Webrexp.Parser( webRexpParser )
import Webrexp.HaXmlNode
import Webrexp.JsonNode
import Webrexp.UnionNode
import Webrexp.DirectoryNode
import Webrexp.ResourcePath
import Webrexp.WebContext
import Webrexp.WebRexpAutomata
data Conf = Conf
{ hammeringDelay :: Int
, userAgent :: String
, output :: Handle
, verbose :: Bool
, quiet :: Bool
, expr :: String
, showHelp :: Bool
, depthEvaluation :: Bool
, outputGraphViz :: Bool
}
defaultConf :: Conf
defaultConf = Conf
{ hammeringDelay = 1500
, userAgent = ""
, output = stdout
, verbose = False
, quiet = False
, expr = ""
, showHelp = False
, outputGraphViz = False
, depthEvaluation = True
}
type CrawledNode =
UnionNode HaXmLNode
(UnionNode JsonNode DirectoryNode)
type Crawled a =
WebCrawler CrawledNode ResourcePath a
initialState :: IO (EvalState CrawledNode ResourcePath)
initialState = do
node <- currentDirectoryNode
return . Node $ repurposeNode (UnionRight . UnionRight) node
parseWebRexp :: String -> Maybe WebRexp
parseWebRexp str =
case runParser webRexpParser () "expr" str of
Left _ -> Nothing
Right e -> Just e
evalParsedWebRexp :: WebRexp -> IO Bool
evalParsedWebRexp wexpr = evalWithEmptyContext crawled
where crawled :: Crawled Bool = evalBreadthFirst (Text "") wexpr
evalWebRexp :: String -> IO Bool
evalWebRexp = evalWebRexpWithEvaluator $ evalBreadthFirst (Text "")
evalWebRexpDepthFirst :: String -> IO Bool
evalWebRexpDepthFirst = evalWebRexpWithEvaluator $ evalDepthFirst (Text "")
evalWebRexpWithEvaluator :: (WebRexp -> Crawled Bool) -> String -> IO Bool
evalWebRexpWithEvaluator evaluator str =
case runParser webRexpParser () "expr" str of
Left err -> do
putStrLn "Parsing error :\n"
print err
return False
Right wexpr ->
let crawled :: Crawled Bool = evaluator wexpr
in evalWithEmptyContext crawled
evalWebRexpWithConf :: Conf -> IO Bool
evalWebRexpWithConf conf =
case runParser webRexpParser () "expr" (expr conf) of
Left err -> do
putStrLn "Parsing error :\n"
print err
return False
Right wexpr -> do
when (outputGraphViz conf)
(do let packed = packRefFiltering wexpr
dumpAutomata (expr conf) stdout $ buildAutomata packed
exitWith ExitSuccess)
when (verbose conf) (print wexpr)
let crawled :: Crawled Bool = do
setUserAgent $ userAgent conf
setOutput $ output conf
setHttpDelay $ hammeringDelay conf
when (quiet conf) (setLogLevel Quiet)
when (verbose conf) (setLogLevel Verbose)
initState <- liftIO initialState
if depthEvaluation conf
then evalDepthFirst initState wexpr
else evalBreadthFirst initState wexpr
rez <- evalWithEmptyContext crawled
when (output conf /= stdout)
(hClose $ output conf)
return rez