From 8fb42d96402983d5960796717320cf086193838b Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Mon, 16 May 2022 11:21:51 +1000 Subject: first commit --- Main.hs | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Main.hs (limited to 'Main.hs') diff --git a/Main.hs b/Main.hs new file mode 100644 index 0000000..1f56a14 --- /dev/null +++ b/Main.hs @@ -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' -- cgit v1.2.3