aboutsummaryrefslogtreecommitdiff
path: root/src/Main.hs
blob: ec602cc8e9ba3bcfe2e15fded2f00d9fb3cb7ed4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{-# LANGUAGE OverloadedStrings #-}

import qualified Data.Text as T
import Control.Monad.IO.Class
import Control.Monad.Logger
import System.Directory
import Control.Monad.Extra
import F2Md.Types
import F2Md.Import
import F2Md.Export
import F2Md.Config
import System.FilePath

-- TODO: update db on interruption with the latest date
processFeed :: FilePath -> Maybe FilePath -> FeedUserdata -> LoggingT IO ()
processFeed root dbPath feed = do
  logInfoN $ "Fetching " <> (fuUrl feed) <> "..."
  msgs <- liftIO $ toMessagesFromUrl (fuUrl feed) (fuLastUpdated feed)
  logInfoN $ "Writing " <> (T.pack $ show $ length msgs) <> " messages..."
  liftIO $ mapM_ (writeMessage root) msgs
  whenJust dbPath $ \dbPath' -> liftIO $ updateLastUpdated' dbPath' feed msgs

main :: IO ()
main = do
  (feeds, maildir, dbPath) <- getUserdata "./.f2m.json"
  whenJust maildir $ \root -> do
    mapM_ (createDirectoryIfMissing True)
      [root </> "new", root </> "cur", root </> "tmp"]
    mapM_ (runStdoutLoggingT . processFeed root dbPath) feeds