{-# LANGUAGE OverloadedStrings #-} import Data.Generics import Data.Maybe import Data.Text (Text) import Data.Text qualified as T import Text.Feed.Import import Text.Feed.Query import Text.Feed.Types data FeedMetadata = FeedMetadata { title :: Text, author :: Maybe Text } deriving (Show) data Message = Message { from :: Text, subject :: Text, date :: Text, messageId :: Text, body :: Text, newsGroup :: Text } deriving (Show) formatMessage :: Message -> Text formatMessage (Message from subject date messageId body newsGroup) = T.concat [ "Date: ", date, "\nMessage-ID: ", messageId, "\nSubject: ", subject, "\nFrom: ", from, "\n\n", body ] getItemAuthor' :: Item -> Maybe Text getItemAuthor' item = case getItemAuthor item of Just "" -> Nothing Just author -> Just author Nothing -> Nothing toMessage :: FeedMetadata -> Item -> Message toMessage (FeedMetadata title author) item = Message (fromMaybe title $ orElse (getItemAuthor' item) author) (fromMaybe "Untitled" $ getItemTitle item) (fromMaybe "" $ getItemDate item) (fromMaybe "" $ snd <$> getItemId item) (fromMaybe "" $ getItemDescription item) title toFeedMetadata :: Feed -> FeedMetadata toFeedMetadata feed = FeedMetadata (getFeedTitle feed) (getFeedAuthor feed) -- for testing purposes writeFirstItem :: Maybe [Text] -> IO () writeFirstItem msgs = writeFile "./firstMsg" $ T.unpack $ head $ fromMaybe [""] msgs main = do feed <- parseFeedFromFile "./plug.xml" -- print $ do writeFirstItem $ do feed' <- feed return $ formatMessage <$> toMessage (toFeedMetadata feed') <$> getFeedItems feed'