diff --git a/berlin-scraper/app/Main.hs b/berlin-scraper/app/Main.hs index 3df80f3..62cb83d 100644 --- a/berlin-scraper/app/Main.hs +++ b/berlin-scraper/app/Main.hs @@ -21,6 +21,7 @@ import Database.SQLite.Simple import GHC.Generics import Network.HTTP.Client.OpenSSL import Network.Wreq hiding (Options) +import Network.Wreq qualified as Wreq import Options.Generic import Servant.Client (mkClientEnv) import System.Exit (exitFailure) @@ -28,16 +29,18 @@ import Telegram.Bot.API import Telegram.Bot.Simple import Telegram.Bot.Simple.Debug import Text.HTML.Scalpel -import qualified Network.Wreq as Wreq main :: IO () main = withOpenSSL $ do - opts <- unwrapRecord "Berlin Scrapper" + opts :: Options Unwrapped <- unwrapRecord "Berlin Scrapper" token <- getEnvToken "TELEGRAM_BOT_TOKEN" httpMgr <- newOpenSSLManager + dbConn <- open opts.dbFile + createTable dbConn let clientEnv = mkClientEnv httpMgr (botBaseUrl token) wreqOpts = Wreq.defaults & Wreq.manager .~ Right httpMgr - startBot (traceBotDefault $ botApp opts wreqOpts) clientEnv >>= \case + env = Env {..} + startBot (traceBotDefault $ botApp env) clientEnv >>= \case Left err -> do putStrLn $ "Bot failed with: " <> show err exitFailure @@ -73,6 +76,11 @@ data IBWResponse = IBWResponse instance FromJSON IBWResponse +data Env = Env + { dbConn :: Connection, + wreqOpts :: Wreq.Options + } + -- * Parsing queryIBW :: Wreq.Options -> IO IBWResponse @@ -174,13 +182,13 @@ type Model = () newtype Action = StartChat Chat deriving (Show) -botApp :: Options Unwrapped -> Wreq.Options -> BotApp Model Action -botApp opts wreqOpts = +botApp :: Env -> BotApp Model Action +botApp env = BotApp { botInitialModel = (), botAction = action, botHandler = handler, - botJobs = [scrapeJob opts wreqOpts] + botJobs = [scrapeJob env] } action :: Update -> Model -> Maybe Action @@ -196,25 +204,23 @@ handler (StartChat chat) model = model <# do liftIO $ putStrLn $ "Chat started! " <> ppAsJSON chat -scrapeJob :: Options Unwrapped -> Wreq.Options -> BotJob Model Action -scrapeJob opts wreqOpts = +scrapeJob :: Env -> BotJob Model Action +scrapeJob env = BotJob { botJobSchedule = "* * * * *", - botJobTask = scrapeJobTask opts wreqOpts + botJobTask = scrapeJobTask env } -scrapeJobTask :: Options Unwrapped -> Wreq.Options -> Model -> Eff Action Model -scrapeJobTask opts wreqOpts m = +scrapeJobTask :: Env -> Model -> Eff Action Model +scrapeJobTask env m = m <# do liftIO $ putStrLn "Starting scrape job" - res <- liftIO $ queryIBW wreqOpts + res <- liftIO $ queryIBW env.wreqOpts let offers = fromJust $ scrapeStringLike res.searchresults scrapeOffers liftIO $ putStrLn "Fetched offers" - conn <- liftIO $ open (dbFile opts) - liftIO $ createTable conn mapM_ ( \offer -> do - isNewOffer <- liftIO $ saveOffer conn offer + isNewOffer <- liftIO $ saveOffer env.dbConn offer when isNewOffer $ do liftIO $ putStrLn "Found a new offer" notify offer