Skip to content

Commit

Permalink
[NuBank] Corrigindo issue maxiwell#9 onde o servidor Selenium poderia…
Browse files Browse the repository at this point in the history
… continuar em execução no caso de exceções.
  • Loading branch information
francesquini committed Apr 25, 2016
1 parent eff79dd commit 4c97f79
Showing 1 changed file with 42 additions and 31 deletions.
73 changes: 42 additions & 31 deletions nubank/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ startSeleniumServer = do

stopSeleniumServer :: IO ()
stopSeleniumServer = do
putStrLn "Desligando Selenium Server"
res <- simpleHTTP (defaultGETRequest_ url) >>= getResponseBody ::IO String
assert (res == "OKOK") $ return ()
where
Expand All @@ -359,6 +360,42 @@ generateOFX cpf dir fatura =
putStrLn "Pegando OFX"
hj <- today
Tio.writeFile (combine dir fname) (faturaToOFX fatura hj cpf)

getLoginInfo :: IO (String, String)
getLoginInfo = do
putStr "CPF: "
hFlush stdout
cpf <- getLine
putStr "Senha: "
hFlush stdout
senha <- withEcho False getLine
putChar '\n'
return (cpf, senha)

nuBankScript :: String -> String -> FilePath -> [Flag] -> WD ()
nuBankScript cpf senha dir flags = do
setImplicitWait 200
liftIO $ putStrLn "Efetuando login"
login cpf senha
f0 <- getFatura
let fatura = filter isValidTx f0

when (CSV `elem` flags) $
liftIO (generateCSV cpf dir fatura)
when (null flags || OFX `elem` flags) $
liftIO (generateOFX cpf dir fatura)

liftIO $ putStrLn "Deslogando"
logout

startSeleniumAndRun :: [Flag] -> IO ()
startSeleniumAndRun flags = do
(cpf, senha) <- getLoginInfo
putStrLn "Iniciando Selenium Server"
startSeleniumServer
dir <- getCurrentDirectory
config <- myConfig dir
runSession config $ finallyClose $ nuBankScript cpf senha dir flags

main :: IO()
main = do
Expand All @@ -367,34 +404,8 @@ main = do

if Help `elem`flags || (length args /= length flags) then
putStrLn usageString
else do
putStr "CPF: "
hFlush stdout
cpf <- getLine
putStr "Senha: "
hFlush stdout
senha <- withEcho False getLine
putChar '\n'

putStrLn "Iniciando Selenium Server"
startSeleniumServer
dir <- getCurrentDirectory
config <- myConfig dir
runSession config $ do
setImplicitWait 200
liftIO $ putStrLn "Efetuando login"
login cpf senha
f0 <- getFatura
let fatura = filter isValidTx f0

when (CSV `elem` flags) $
liftIO (generateCSV cpf dir fatura)
when (null flags || OFX `elem` flags) $
liftIO (generateOFX cpf dir fatura)

liftIO $ putStrLn "Deslogando"
logout
liftIO $ putStrLn "Fechando sessão Selenium"
closeSession
putStrLn "Desligando Selenium Server"
stopSeleniumServer
else
startSeleniumAndRun flags `catch` exHandler `finally` stopSeleniumServer
where
exHandler :: SomeException -> IO ()
exHandler e = putStrLn $ "Erro durante a execução" ++ displayException e

0 comments on commit 4c97f79

Please sign in to comment.