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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE NoFieldSelectors #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
module Main where
import Control.Arrow (second)
import Control.Concurrent.ParallelIO.Local (parallel, withPool)
import Control.Exception (Exception, throw, throwIO)
import Control.Monad (unless, when)
import Data.Aeson qualified as J
import Data.Attoparsec.Text qualified as A
import Data.ByteString.Lazy qualified as LB
import Data.Default
import Data.Digest.Pure.SHA (sha256, showDigest)
import Data.List
import Data.Map qualified as M
import Data.Maybe (catMaybes, fromMaybe)
import Data.Set qualified as S
import Data.String (IsString (fromString))
import Data.Text qualified as T
import Data.Text.Encoding qualified as T
import Data.Text.IO qualified as T
import Debug.Trace
import GHC.Conc (getNumProcessors)
import GHC.Generics (Generic)
import GHC.Records (HasField (..))
import Options.Applicative qualified as O
import System.Directory
import System.Environment (getEnv)
import System.FilePath
import System.IO
import System.IO.LockFile (withLockFile)
import System.IO.Temp (withSystemTempDirectory)
import System.Process.Typed
import Text.Printf (printf)
import Text.Read (readMaybe)
data Args = Args
{ cmd :: Cmd
}
data Cmd
= Consume
{ keep :: Bool,
inputs :: [FilePath]
}
| List
{ filters :: [Filter],
todo :: Bool
}
| Todo
args :: O.Parser Args
args =
Args <$> cmd
cmd :: O.Parser Cmd
cmd =
O.hsubparser . mconcat $
[ O.command "consume" . O.info consumeCmd $
O.progDesc "Consume document(s)",
O.command "list" . O.info listCmd $
O.progDesc "List document(s)",
O.command "todo" . O.info todoCmd $
O.progDesc "Interactively process new documents"
]
consumeCmd :: O.Parser Cmd
consumeCmd =
Consume
<$> keepArg
<*> inputsArg
listCmd :: O.Parser Cmd
listCmd =
List
<$> filtersArg
<*> todoArg
todoCmd :: O.Parser Cmd
todoCmd =
pure Todo
inputsArg :: O.Parser [FilePath]
inputsArg =
O.many (O.strArgument (O.metavar "FILE" <> O.action "file"))
keepArg :: O.Parser Bool
keepArg =
O.switch
( O.long "keep"
<> O.help "Keep input document"
)
filtersArg :: O.Parser [Filter]
filtersArg =
O.many $
O.option
(O.maybeReader parse)
( O.long "filter"
<> O.short 'f'
<> O.help "Filter documents by tag"
)
where
parse ('@' : tagKey) = Just (FilterByTag (T.pack tagKey))
parse _ = Nothing
todoArg :: O.Parser Bool
todoArg =
O.switch
( O.long "todo"
<> O.help "Run command `todo` on listed documents."
)
data Filter
= FilterByTag T.Text
main :: IO ()
main = do
cwd <- getCurrentDirectory
setCurrentDirectory =<< getEnv "APAPERLESS_STORE_DIR"
ensureGit
ensureDir "originals"
ensureDir "index"
O.execParser (O.info (args O.<**> O.helper) O.idm) >>= \case
Args {cmd = Consume {keep, inputs}} ->
mapM_ putStrLn
=<< parMapM (consume1 keep) (map (cwd </>) inputs)
Args {cmd = List {filters, todo = False}} -> do
mapM_
( \(Document {iFileName, index}) -> do
if hasTag (Tag "todo" Nothing) index
then printf "TODO %s\n" (takeBaseName iFileName)
else printf " %s\n" (takeBaseName iFileName)
)
. applyFilters filters
=<< getDocuments
Args {cmd = List {filters, todo = True}} -> do
processDocuments
. applyFilters filters
=<< getDocuments
Args {cmd = Todo} -> do
processDocuments
. applyFilters [FilterByTag "todo"]
=<< getDocuments
data Document = Document
{ iFileName :: String,
index :: Index
}
deriving (Show)
instance HasField "oFilePath" Document FilePath where
getField doc = "originals" </> takeBaseName doc.iFileName <.> "pdf"
instance HasField "iFilePath" Document FilePath where
getField doc = "index" </> doc.iFileName
getDocuments :: IO [Document]
getDocuments =
parMapM
( \iFileName ->
Document iFileName
<$> decodeFile @Index ("index" </> iFileName)
)
=<< sort <$> listDirectory "index"
applyFilters :: [Filter] -> [Document] -> [Document]
applyFilters filters = filter (pred filters) `at` (.index)
where
pred1 (FilterByTag tagKey) = hasTag (Tag tagKey Nothing)
pred filters = \index -> all ($ index) (map pred1 filters)
at :: ([a] -> [a]) -> (b -> a) -> [b] -> [b]
at _ _ [] = []
at g f (x : xs)
| null (g [f x]) = at g f xs
| otherwise = x : at g f xs
processDocuments :: [Document] -> IO ()
processDocuments docs =
mapM_ (uncurry processDocuments') (zip [1 :: Int ..] docs)
where
numDocs = length docs
processDocuments' n (doc@Document {iFileName, index}) = do
choice <-
promptChoiceHelp
[ ("f", "view full text"),
("p", "process document"),
("s", "skip document"),
("v", "view document")
]
( printf
"%s\n%s\n\n(%d/%d) Process this document?"
(takeBaseName iFileName)
index.shortText
n
numDocs
)
case choice of
"f" -> do
printf "%s\n" (takeBaseName doc.iFileName)
printf
"%s\n"
( T.unlines
. filter (not . T.null)
. map T.strip
. T.lines
$ doc.index.originalText
)
processDocuments' n doc
"p" -> processDocument doc
"s" -> pure ()
"v" -> do
sh_ (printf "zathura '%s'" doc.oFilePath)
processDocuments' n doc
processDocument :: Document -> IO ()
processDocument (Document {iFileName, index}) = do
printf "%s\n" index.originalText
let suggestedTags =
[ Tag "correspondent" (Just ""),
Tag "invoice" Nothing,
Tag "bill" Nothing
]
tags <-
S.fromList . catMaybes
<$> mapM processSuggestedTag suggestedTags
let tags' = S.delete (Tag "todo" Nothing) (index.tags `S.union` tags)
index' = index {tags = tags'}
iFilePath = "index" </> iFileName
withGit do
J.encodeFile iFilePath index'
commitAll [iFilePath] (printf "process %s (interactive)" iFilePath)
processSuggestedTag :: Tag -> IO (Maybe Tag)
processSuggestedTag tag@(Tag tagKey Nothing) = do
choice <- promptChoice (Just "n") ["n", "y"] (printf "tag with %s?" tagKey)
pure $ if (choice == "y") then Just tag else Nothing
processSuggestedTag (Tag tagKey (Just _)) = do
tagValue <- promptString [] (printf "tag with %s?" tagKey)
pure $
if not (T.null tagValue)
then Just (Tag tagKey (Just tagValue))
else Nothing
promptChoice :: Maybe T.Text -> [T.Text] -> String -> IO T.Text
promptChoice mDef as s = do
a <-
T.toLower
<$> promptString
[]
( s
++ ( T.unpack
( " ["
<> T.intercalate "" (map capitalizeDef as)
<> "]"
)
)
)
case (a, mDef) of
("", Just def) -> pure def
_ ->
if not (T.toLower a `elem` map T.toLower as)
then promptChoice mDef as s
else pure a
where
capitalizeDef a = (if Just a == mDef then T.toUpper else T.toLower) a
promptChoiceHelp :: [(T.Text, T.Text)] -> String -> IO T.Text
promptChoiceHelp as' s = do
a <-
T.toLower
<$> promptString
[]
( s
++ ( T.unpack
(" [" <> T.intercalate "" (as ++ ["?"]) <> "]")
)
)
if a == "?"
then do
printHelp
promptChoiceHelp as' s
else
if not (T.toLower a `elem` map T.toLower as)
then promptChoiceHelp as' s
else pure a
where
as = map fst as'
printHelp = mapM_ (uncurry (printf "%s - %s\n")) as'
promptString :: [T.Text] -> String -> IO T.Text
promptString as s = do
if null as
then do
putStr (s <> "> ")
else do
putStrLn s
mapM_ (\(n, a) -> printf "[%d] %s\n" n a) (zip [1 :: Int ..] as)
putStr "> "
hFlush stdout
a <- T.strip <$> T.getLine
case (as, readMaybe (T.unpack a)) of
((_ : _), Just n) ->
case drop (n - 1) as of
[] -> promptString as s
(a' : _) -> pure a'
_ -> pure a
ensureGit :: IO ()
ensureGit = do
doesExist <- doesDirectoryExist ".git"
unless doesExist $ sh_ "git init --initial-branch main"
ensureDir :: FilePath -> IO ()
ensureDir dirName =
createDirectoryIfMissing False dirName
debug :: Show a => String -> a -> a
debug s x =
trace (printf "%s: %s\n" s (show x)) x
fileKey :: FilePath -> IO FilePath
fileKey filePath =
showDigest . sha256 <$> LB.readFile filePath
consume1 :: Bool -> FilePath -> IO FilePath
consume1 keep filePath = do
fKey <- fileKey filePath
let oFilePath = "originals" </> fKey <.> takeExtension filePath
originalExists <- doesFileExist oFilePath
when originalExists do
error (printf "error: error adding %s: duplicate of %s\n" filePath oFilePath)
let iFilePath = "index" </> fKey <.> "json"
originalText <- do
originalText' <-
T.decodeUtf8 . LB.toStrict
<$> sh (printf "pdftotext '%s' -" filePath)
let hasText = (not . T.null) . T.strip $ originalText'
if not hasText
then ocr filePath
else pure originalText'
withGit do
J.encodeFile iFilePath Index {tags = S.singleton (Tag "todo" Nothing), ..}
if keep
then copyFile filePath oFilePath
else renameFile filePath oFilePath
commitAll
[iFilePath, oFilePath]
(printf "add %s" (takeFileName filePath))
pure (takeBaseName iFilePath)
withGit :: IO a -> IO a
withGit = withLockFile def ".gitlock"
commitAll :: [FilePath] -> String -> IO ()
commitAll fps m = do
sh_ ("git add -- " ++ intercalate " " (map (printf "'%s'") fps))
sh_ (printf "git commit -m '%s'" m)
data DecodeException = DecodeException FilePath String
deriving (Show)
instance Exception DecodeException
decodeFile :: J.FromJSON a => FilePath -> IO a
decodeFile fp =
either (throwIO . DecodeException fp) pure . J.eitherDecode
=<< LB.readFile fp
ocr :: FilePath -> IO T.Text
ocr input =
withSystemTempDirectory (takeBaseName input) $ \tmp -> do
let fn suffix = tmp </> takeBaseName input <> suffix
pdfInfo <- parsePdfInfo <$> sh (printf "pdfinfo '%s'" input)
pdfImages <- parsePdfImages <$> sh (printf "pdfimages -list '%s'" input)
let isScan =
length pdfImages == pdfInfo.numPages
&& all ((pdfInfo.pageSize ==) . imageSize) pdfImages
if isScan
then sh_ (printf "pdfimages '%s' '%s' -tiff" input (fn ""))
else sh_ (printf "pdftoppm '%s' '%s' -r 300 -tiff" input (fn "-%d.pdf"))
imageFiles <- sort <$> listDirectory tmp
-- XXX add DPI information to image so that resulting pdf preserves DPI
parMapM_
( \(pdfImage, imageFile) ->
sh_
( printf
"convert -density %dx%d -units PixelsPerInch '%s' '%s'"
pdfImage.xPpi
pdfImage.yPpi
(tmp </> imageFile)
(tmp </> imageFile)
)
)
(zip pdfImages imageFiles)
T.unlines <$> mapM (ocr1 tmp . (tmp </>)) imageFiles
ocr1 :: FilePath -> FilePath -> IO T.Text
ocr1 tmp input =
T.decodeUtf8 . LB.toStrict
<$> sh (printf "tesseract '%s' -" (tmp </> input))
data Index = Index
{ originalText :: T.Text,
tags :: S.Set Tag
}
deriving (Show, Generic, Eq)
instance J.ToJSON Index
instance J.FromJSON Index
instance HasField "shortText" Index T.Text where
getField =
T.unlines
. take 10
. filter (not . T.null)
. map T.strip
. T.lines
. (.originalText)
data Tag = Tag T.Text (Maybe T.Text)
deriving (Show, Generic, Eq, Ord)
tagKey :: Tag -> T.Text
tagKey (Tag x _) = x
tagValue :: Tag -> Maybe T.Text
tagValue (Tag _ x) = x
hasTag :: Tag -> Index -> Bool
hasTag tag =
(tagKey tag `S.member`) . S.map tagKey . (.tags)
instance J.ToJSON Tag
instance J.FromJSON Tag
data PdfInfo = PdfInfo
{ numPages :: Int,
pageSize :: (Double, Double)
}
deriving (Show)
data PdfInfoException = PdfInfoException
deriving (Show)
instance Exception PdfInfoException
parsePdfInfo :: LB.ByteString -> PdfInfo
parsePdfInfo out' =
fromMaybe (throw PdfInfoException) $ do
numPages <- readMaybe . T.unpack =<< M.lookup "Pages" kvs
pageSize <-
rightToMaybe . A.parseOnly pageSizeParser
=<< M.lookup "Page size" kvs
pure PdfInfo {..}
where
out = T.decodeUtf8 (LB.toStrict out')
kvs =
M.fromList
. map (second T.stripStart)
. map (second T.tail . T.break (== ':'))
. filter (not . T.null)
. T.lines
$ out
pageSizeParser =
(,)
<$> (A.double <* A.string " x ")
<*> (A.double <* A.string " pts (A4)")
<* A.endOfInput
type PdfImages = [PdfImage]
data PdfImage = PdfImage
{ page :: Int,
num :: Int,
type_ :: String,
width :: Int,
height :: Int,
color :: String,
comp :: Int,
bpc :: Int,
enc :: String,
interp :: String,
object :: Int,
id :: Int,
xPpi :: Int,
yPpi :: Int,
size :: String,
ratio :: String
}
deriving (Show)
imageSize :: PdfImage -> (Double, Double)
imageSize (PdfImage {..}) =
let f ppi p = 72 * fromIntegral p / fromIntegral ppi
in (f xPpi width, f yPpi height)
data PdfImagesException = PdfImagesException
deriving (Show)
instance Exception PdfImagesException
data ProcessException = ProcessException Int LB.ByteString
deriving (Show)
instance Exception ProcessException
parsePdfImages :: LB.ByteString -> PdfImages
parsePdfImages out' =
map
( \(page' : num' : type_ : width' : height' : color : comp' : bpc' : enc : interp : object' : id' : xPpi' : yPpi' : size : ratio : []) ->
PdfImage
{ page = read page',
num = read num',
width = read width',
height = read height',
comp = read comp',
bpc = read bpc',
object = read object',
id = read id',
xPpi = read xPpi',
yPpi = read yPpi',
..
}
)
. map (map T.unpack)
. map T.words
. drop 2
. filter (not . T.null)
. T.lines
$ out
where
out = T.decodeUtf8 (LB.toStrict out')
sh :: String -> IO LB.ByteString
sh cmd = do
(exitCode, out, err) <- readProcess (fromString cmd)
case exitCode of
ExitSuccess -> return out
ExitFailure exitCode' -> throwIO $ ProcessException exitCode' err
sh_ :: String -> IO ()
sh_ = fmap (\_ -> ()) . sh
rightToMaybe :: Either e a -> Maybe a
rightToMaybe = either (const Nothing) Just
parMapM :: (a -> IO b) -> [a] -> IO [b]
parMapM f xs = do
n <- getNumProcessors
withPool n $ \pool -> parallel pool (map f xs)
parMapM_ :: (a -> IO b) -> [a] -> IO ()
parMapM_ f = fmap (const ()) . parMapM f
|