quotes-api/lib/Parsers/Readwise.hs

52 lines
2.1 KiB
Haskell
Raw Normal View History

2023-04-11 09:02:42 +00:00
{-# LANGUAGE OverloadedStrings #-}
module Parsers.Readwise where
import Data.Csv
import Data.Text
import Data.Vector (toList)
import Data.ByteString.Lazy (ByteString)
import Api.Types (Quote(..))
data RwHighlight = RwHighlight { rhHightlight :: Text
, rhTitle :: Text
, rhAuthor :: Text
, rhBookID :: Text
, rhNote :: Text
, rhColor :: Text
, rhTags :: Text
, rhLocationType :: Text
, rhLocation :: Text
, rhHighlighedAt :: Text
, rhDocumentTags :: Text
} deriving (Show, Eq)
instance FromNamedRecord RwHighlight where
parseNamedRecord m = RwHighlight <$> m .: "Highlight"
<*> m .: "Book Title"
<*> m .: "Book Author"
<*> m .: "Amazon Book ID"
<*> m .: "Note"
<*> m .: "Color"
<*> m .: "Tags"
<*> m .: "Location Type"
<*> m .: "Location"
<*> m .: "Highlighted at"
<*> m .: "Document tags"
2023-04-13 21:04:39 +00:00
parseDocument :: ByteString -> Either String [RwHighlight]
2023-04-11 09:02:42 +00:00
parseDocument d = case decodeByName d of
2023-04-13 21:04:39 +00:00
Left err -> Left err
Right (_, va) -> Right $ toList va
2023-04-11 09:02:42 +00:00
2023-04-13 21:04:39 +00:00
parse :: ByteString -> Either String [Quote]
parse d = case parseDocument d of
Left err -> Left err
Right rw -> Right $ fmap (\r -> Quote { qQuote = rhHightlight r
2023-04-11 09:02:42 +00:00
, qAuthor = rhAuthor r
, qTitle = rhTitle r
, qPage = rhLocation r
, qChapter = Nothing
, qCreatedOn = Nothing
2023-04-13 21:04:39 +00:00
}) rw