KOReader data parsing

This commit is contained in:
Dhananjay Balan 2023-02-20 23:10:02 +05:30
parent 4e9ed112c4
commit 6921c98014
3 changed files with 63 additions and 9 deletions

View File

@ -1,19 +1,14 @@
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DeriveGeneric #-}
module Main where
import Data.Text (Text(..))
import Data.Text (Text)
import Data.Aeson
import qualified MyLib (someFunc)
import Database.SQLite.Simple
import Database.SQLite.Simple.FromRow (FromRow)
import Database.SQLite.Simple.FromField (fromField)
import Database.SQLite.Simple.QQ
import Network.Wai
import Network.Wai.Handler.Warp
import Deriving.Aeson
import Data.Proxy
@ -23,7 +18,10 @@ import Control.Monad.IO.Class
data Quote = Quote { qQuote :: Text
, qAuthor :: Text
, qBook :: Text
, qTitle :: Text
-- , qPage :: Text
-- , qChapter :: Text
-- , qTime :: UnixTime
} deriving (Show, Eq, Ord, Generic)
deriving (FromJSON,ToJSON)
via CustomJSON '[OmitNothingFields, FieldLabelModifier '[StripPrefix "q", CamelToSnake]] Quote

51
lib/KOReader.hs Normal file
View File

@ -0,0 +1,51 @@
-- | Module to parse KOReader hightlight file
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
module KOReader where
import Deriving.Aeson
import Data.Aeson
import Data.Text
showT :: Show a => a -> Text
showT = pack . show
newtype KoPage = KoPage Text
deriving (Show, Eq, Generic)
instance FromJSON KoPage where
parseJSON (Number s) = pure (KoPage $ showT $ round s)
parseJSON (String s) = pure (KoPage (showT s))
parseJSON _ = undefined
instance ToJSON KoPage where
toJSON (KoPage a) = toJSON a
data KoHighlight = KoHighlight { khlDocuments :: [KoDocument]
, khlVersion :: Text
-- TODO: UNIX time
, khlCreatedOn :: Integer }
deriving (Show, Eq, Generic)
deriving (FromJSON, ToJSON)
via CustomJSON '[OmitNothingFields, FieldLabelModifier '[StripPrefix "khl", CamelToSnake]] KoHighlight
data KoDocument = KoDocument { kodTitle :: Text
, kodAuthor :: Maybe Text
, kodFile :: Maybe Text
, kodEntries :: [KoEntry] }
deriving (Show, Eq, Generic)
deriving (FromJSON, ToJSON)
via CustomJSON '[OmitNothingFields, FieldLabelModifier '[StripPrefix "kod", CamelToSnake]] KoDocument
data KoEntry = KoEntry { koeChapter :: Maybe Text
, koePage :: KoPage
-- TODO: unix time
, koeTime :: Maybe Integer
, koeSort :: Maybe Text
, koeDrawer :: Maybe Text
, koeText :: Text}
deriving (Show, Eq, Generic)
deriving (FromJSON, ToJSON)
via CustomJSON '[OmitNothingFields, FieldLabelModifier '[StripPrefix "koe", CamelToSnake]] KoEntry

View File

@ -59,7 +59,7 @@ library
import: warnings
-- Modules exported by the library.
exposed-modules: MyLib
exposed-modules: KOReader
-- Modules included in this library but not exported.
-- other-modules:
@ -68,7 +68,12 @@ library
-- other-extensions:
-- Other library packages from which modules are imported.
build-depends: base ^>=4.16.3.0
build-depends: base ^>=4.16.3.0,
text,
aeson,
deriving-aeson,
bytestring,
unordered-containers,
-- Directories containing source files.
hs-source-dirs: lib