Add an importer binary.

This commit is contained in:
Dhananjay Balan 2023-04-13 23:43:39 +02:00
parent 11c4e92cc7
commit 72b3e38980
6 changed files with 117 additions and 16 deletions

View File

@ -7,7 +7,7 @@ Personal, bare bones [readwise] alternative.
Have Have
- Parsers - Parsers
- [-] Read from kindle - [-] Read from kindle
- [ ] Read from readwise - [x] Read from readwise
- [x] Read from KOReader - [x] Read from KOReader
- [x] Minimal API - [x] Minimal API
- Expose random-quote API - Expose random-quote API

View File

@ -22,6 +22,7 @@ import qualified Parsers.KOReader as KO
import qualified Parsers.Readwise as RW import qualified Parsers.Readwise as RW
import Config import Config
import Options.Applicative import Options.Applicative
import Database
data User = User data User = User
{ username :: T.Text { username :: T.Text
@ -50,16 +51,6 @@ checkBasicAuth user passhash = BasicAuthCheck $ \authData ->
_ -> return Unauthorized _ -> return Unauthorized
initDb :: FilePath -> IO ()
initDb dbFile = withConnection dbFile $ \conn ->
execute_ conn
[sql|CREATE TABLE IF NOT EXISTS quotes ( quote text non null
, author text
, title text
, page text
, chapter text
, created_on integer);|]
-- | TODO: readerT -- | TODO: readerT
server :: FilePath -> Server API server :: FilePath -> Server API
server dbf = randomQuote dbf server dbf = randomQuote dbf
@ -83,12 +74,8 @@ listQuotes db = liftIO $ withConnection db $ \conn -> query_ conn [sql|SELECT *
addKoReader :: FilePath -> KO.KoHighlight -> Handler NoContent addKoReader :: FilePath -> KO.KoHighlight -> Handler NoContent
addKoReader db hl = do addKoReader db hl = do
liftIO $ withConnection db $ \c -> liftIO $ insertQts db (KO.parse hl)
executeMany c qry qts
pure NoContent pure NoContent
where
qry = [sql|INSERT INTO quotes VALUES (?,?,?,?,?,?);|]
qts = KO.parse hl
addReadwise :: FilePath -> T.Text -> Handler NoContent addReadwise :: FilePath -> T.Text -> Handler NoContent
addReadwise db hl = do addReadwise db hl = do

23
importer/Main.hs Normal file
View File

@ -0,0 +1,23 @@
module Main where
import Options.Applicative
import qualified Data.ByteString.Lazy as BSL
import Config
import Database
import qualified Parsers.Readwise as RW
runImporter :: FilePath -> FilePath -> IO ()
runImporter db rw = do
x <- BSL.readFile rw
let y = RW.parse x
case y of
Left err -> print err
Right qts -> insertQts db qts
main :: IO ()
main = do
conf <- execParser importerParserOpts
initDb (ioAppDbFile conf)
runImporter (ioAppDbFile conf) (ioReadwiseFile conf)

View File

@ -2,6 +2,8 @@
module Config module Config
( parserOpts ( parserOpts
, AppConfig(..) , AppConfig(..)
, importerParserOpts
, ImporterConfig(..)
) where ) where
import Options.Applicative import Options.Applicative
@ -43,3 +45,29 @@ parserOpts = info (appConfig <**> helper)
( fullDesc ( fullDesc
<> progDesc "Serve Quotes API" <> progDesc "Serve Quotes API"
<> header "quotes api" ) <> header "quotes api" )
data ImporterConfig = ImporterConfig
{ ioAppDbFile :: FilePath
, ioReadwiseFile :: FilePath
} deriving (Show, Eq)
importerConfig :: Parser ImporterConfig
importerConfig = ImporterConfig
<$> strOption
( long "dbpath"
<> help "sqlite db file path"
<> showDefault
<> value "quotes.db"
<> metavar "TARGET")
<*> strOption
( long "readwise"
<> help "readwise export file path"
<> showDefault
<> value "readwise.csv"
<> metavar "RWCSV")
importerParserOpts :: ParserInfo ImporterConfig
importerParserOpts = info (importerConfig <**> helper)
( fullDesc
<> progDesc "Import data into db"
<> header "importer API")

25
lib/Database.hs Normal file
View File

@ -0,0 +1,25 @@
{-# LANGUAGE QuasiQuotes #-}
module Database where
import Database.SQLite.Simple.QQ
import Database.SQLite.Simple
import Api.Types
initDb :: FilePath -> IO ()
initDb dbFile = withConnection dbFile $ \conn ->
execute_ conn
[sql|CREATE TABLE IF NOT EXISTS quotes ( quote text non null
, author text
, title text
, page text
, chapter text
, created_on integer);|]
insertQts :: FilePath -> [Quote] -> IO ()
insertQts db qts = do
withConnection db $ \c ->
executeMany c qry qts
where
qry = [sql|INSERT INTO quotes VALUES (?,?,?,?,?,?);|]

View File

@ -63,6 +63,7 @@ library
Parsers.KOReader, Parsers.KOReader,
Parsers.Readwise, Parsers.Readwise,
Config, Config,
Database,
-- Modules included in this library but not exported. -- Modules included in this library but not exported.
-- other-modules: -- other-modules:
@ -124,6 +125,43 @@ executable quotes-api
-- Base language which the package is written in. -- Base language which the package is written in.
default-language: Haskell2010 default-language: Haskell2010
executable quotes-importer
-- Import common warning flags.
import: warnings
-- .hs or .lhs file containing the Main module.
main-is: Main.hs
-- Modules included in this executable, other than Main.
-- other-modules:
-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
-- Other library packages from which modules are imported.
build-depends:
base ^>=4.16.3.0,
sqlite-simple ^>=0.4.18.0,
text ^>=1.2.5.0,
servant-server ^>=0.19.1,
wai,
warp,
aeson,
deriving-aeson,
quotes-api,
servant-blaze,
optparse-applicative,
argon2 >= 1.3.0,
text-short,
bytestring,
QuickCheck,
-- Directories containing source files.
hs-source-dirs: importer
-- Base language which the package is written in.
default-language: Haskell2010
test-suite quotes-api-test test-suite quotes-api-test
-- Import common warning flags. -- Import common warning flags.
import: warnings import: warnings