aboutsummaryrefslogtreecommitdiff
path: root/Main.hs
blob: 1f56a14f9d0c3a98016fea25ea1aa5e691b11728 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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'