FeedMonad/FeedMonad/src/FeedMonad.hs

53 lines
1.6 KiB
Haskell
Raw Normal View History

2021-11-11 20:55:29 +02:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveFunctor #-}
2021-11-11 19:04:48 +02:00
module FeedMonad where
import Data.Text (Text)
import Middleware (Middleware)
import Numeric.Natural (Natural)
import Data.Category (Category)
2021-11-11 22:08:46 +02:00
import Data.Acid (openLocalState, AcidState (closeAcidState))
import Control.Exception (bracket)
import Database (emptyFeedMonadState)
import Network.HTTP.Client.TLS (newTlsManager)
import Control.Monad.App (runApp)
import Data.Environment
import Control.Monad.Trans (liftIO)
2021-11-11 22:31:38 +02:00
import Data.URL (URL)
2021-11-11 22:40:47 +02:00
import Data.Foldable (for_)
import Control.Monad.HTTP (fetch, execute)
import Text.Feed.Import (parseFeedSource)
2021-11-11 19:04:48 +02:00
newtype Minutes = Minutes Natural
data FeedMonad = FeedMonad
{ feeds :: [Category URL]
2021-11-11 19:04:48 +02:00
-- ^ The forest of urls for the feeds. It's a forest because of the categories
, filters :: Middleware
-- ^ The middleware. Modifies the scoring, tags and content
, refreshTime :: Minutes
-- ^ How often to refresh the feeds
, secretToken :: Text
-- ^ Used for authenticating the UI. This is a single user app
-- served over http, so we can get around with hardcoded
-- authentication token
}
2021-11-11 20:55:29 +02:00
defaultConfig :: FeedMonad
defaultConfig = FeedMonad
{ feeds = []
, filters = id
, refreshTime = Minutes 30
, secretToken = "i am a secret"
}
2021-11-11 19:04:48 +02:00
defaultMain :: FeedMonad -> IO ()
2021-11-11 20:55:29 +02:00
defaultMain f =
2021-11-11 22:08:46 +02:00
bracket (openLocalState emptyFeedMonadState) closeAcidState $ \st -> do
mgr <- newTlsManager
runApp (Environment mgr st) $
2021-11-11 22:40:47 +02:00
for_ (feeds f) $ \c -> for_ c $ \url -> do
feed <- parseFeedSource <$> liftIO (execute mgr (fetch url))
liftIO $ print feed