1 module Data.Gargantext.Parsers.WOS where
3 import Prelude hiding (takeWhile, take, concat, readFile)
4 import qualified Data.List as DL
6 import Data.Attoparsec.ByteString
7 import Data.Attoparsec.ByteString.Char8 (anyChar, char8, endOfLine, isDigit_w8, isAlpha_ascii, isEndOfLine)
8 import Data.ByteString (ByteString, unpack, pack, concat, readFile)
10 import Data.Either.Extra(Either(..))
11 import Control.Applicative
13 import Control.Monad (join)
15 -- To be removed just for Tests
17 -- import Codec.Archive.LibZip (withArchive, fileNames, sourceFile, addFile)
18 --import Codec.Archive.LibZip.Types (ZipSource, OpenFlag (CreateFlag))
20 import Control.Concurrent.Async as CCA (mapConcurrently)
22 import System.Environment
24 import Codec.Archive.Zip
25 import Path (parseAbsFile)
26 import Path.IO (resolveFile')
27 -- import qualified Data.ByteString.Lazy as B
28 import Control.Applicative ( (<$>) )
31 zipFiles :: FilePath -> IO [ByteString]
33 path <- resolveFile' fp
34 entries <- withArchive path (DM.keys <$> getEntries)
35 bs <- mapConcurrently (\s -> withArchive path (getEntry s)) entries
39 parseFile :: ParserType -> ByteString -> IO Int
40 parseFile p x = case runParser p x of
42 Right r -> pure $ length r
44 testWos :: FilePath -> IO [Int]
45 testWos fp = join $ mapConcurrently (parseFile WOS) <$> zipFiles fp
47 -- type Parser a = a -> Text -> [Document]
48 data ParserType = WOS | CSV
50 wosParser :: Parser [Maybe [ByteString]]
52 -- TODO Warning if version /= 1.0
53 _ <- manyTill anyChar (string "\nVR 1.0")
54 ns <- many1 wosNotice <* "\nEF"
57 startNotice :: Parser ByteString
58 startNotice = "\nPT " *> takeTill isEndOfLine
60 wosNotice :: Parser (Maybe [ByteString])
62 n <- startNotice *> wosFields <* manyTill anyChar (string "\nER\n")
65 field' :: Parser (ByteString, [ByteString])
67 f <- "\n" *> take 2 <* " "
68 a <- takeTill isEndOfLine
70 let as' = case DL.length as > 0 of
73 return (f, [a] ++ as')
75 wosFields' :: Parser [(ByteString, [ByteString])]
76 wosFields' = many field'
78 wosFields :: Parser (Maybe [ByteString])
83 -- d <- field "DI" -- DOI
88 return $ DL.lookup "UT" ws
89 -- return $ HyperdataDocument
97 wosLines :: Parser [ByteString]
100 line :: Parser ByteString
101 line = "\n " *> takeTill isEndOfLine
103 runParser :: ParserType -> ByteString -> Either String [Maybe [ByteString]]
104 runParser p x = parseOnly parser x
108 _ -> error "Not implemented yet"
110 -- isTokenChar :: Word8 -> Bool
111 -- isTokenChar = inClass "!#$%&'()*+./0-9:<=>?@a-zA-Z[]^_`{|}~-\n"