diff options
author | Yuchen Pei <hi@ypei.me> | 2022-05-16 11:21:51 +1000 |
---|---|---|
committer | Yuchen Pei <hi@ypei.me> | 2022-05-16 11:21:51 +1000 |
commit | 8fb42d96402983d5960796717320cf086193838b (patch) | |
tree | b1aa70a704eaa0dccb7f58563d5912e76785a5aa /Main.hs |
first commit
Diffstat (limited to 'Main.hs')
-rw-r--r-- | Main.hs | 72 |
1 files changed, 72 insertions, 0 deletions
@@ -0,0 +1,72 @@ +{-# 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' |