module App.Cycle ( startCycle ) where import Control.Concurrent ( threadDelay ) import Network.HTTP ( simpleHTTP, getRequest ) import Control.Exception ( catch, SomeException ) import System.Exit ( exitFailure ) import Control.Monad ( forever, void ) import App.Options ( Options (..) ) import App.Login ( login ) seleniumStatusLink :: String seleniumStatusLink = "http://localhost:4444/wd/hub/status" seleniumClosed :: SomeException -> IO a seleniumClosed _ = do putStrLn "Error: Selenium server is not accessible." putStrLn $ "\t Couldn't get " ++ seleniumStatusLink putStrLn "Aborting" exitFailure startCycle :: Options -> IO () startCycle (Options rept verb email pass) = do let showOutput = if verb then putStrLn else void . return showOutput "Program started" showOutput "Checking for selenium server" _ <- simpleHTTP (getRequest seleniumStatusLink) `catch` seleniumClosed showOutput "Everything's ready, starting login cycle" -- The process won't end forever $ do showOutput "\n[======= Opening new session =======]" login email pass verb showOutput "[========== End Session ============]\n" showOutput $ "@ Pending mode: Waiting " ++ show rept ++ " minutes." threadDelay $ rept * 60 * 1000000