2 Module : Gargantext.API.Node.Document.Export
3 Description : Document export
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
11 module Gargantext.API.Node.Document.Export
14 import qualified Data.ByteString.Lazy.Char8 as BSC
15 import Data.Csv (encodeDefaultOrderedByName)
16 import qualified Data.Text as T
17 --import qualified Data.Text.Conversions as TC
18 import qualified Data.Text.Encoding as TE
19 import qualified Data.Text.IO as T
20 import Data.Version (showVersion)
21 import Gargantext.API.Node.Document.Export.Types
22 import Gargantext.API.Prelude (GargNoServer, GargServer)
23 import Gargantext.Core (toDBid)
24 import Gargantext.Core.Types
25 -- import Gargantext.Database.Admin.Types.Hyperdata (HyperdataDocument(..))
26 import Gargantext.Database.Query.Facet (runViewDocuments, Facet(..))
27 import Gargantext.Database.Query.Table.Node (getClosestParentIdByType)
28 import Gargantext.Database.Schema.Node (NodePoly(..))
29 import Gargantext.Prelude
30 import qualified Paths_gargantext as PG -- cabal magic build module
33 api :: UserId -> DocId -> GargServer API
34 api uid dId = getDocumentsJSON uid dId
35 :<|> getDocumentsCSV uid dId
37 --------------------------------------------------
38 -- | Hashes are ordered by Set
39 getDocumentsJSON :: UserId
41 -> GargNoServer DocumentExport
42 getDocumentsJSON uId pId = do
43 printDebug "[getDocuments] pId" pId
44 mcId <- getClosestParentIdByType pId NodeCorpus
45 let cId = maybe (panic "[G.A.N.D.Export] Node has no parent") identity mcId
46 printDebug "[getDocuments] cId" cId
47 --docs <- getDocumentsWithParentId cId -- NodeDocument (Proxy :: Proxy HyperdataDocument)
48 docs <- runViewDocuments cId False Nothing Nothing Nothing Nothing
49 --printDebug "[getDocuments] got docs" docs
50 --de_docs <- mapM mapFacetDoc docs
51 pure $ DocumentExport { _de_documents = mapFacetDoc <$> docs
52 , _de_garg_version = T.pack $ showVersion PG.version }
54 mapFacetDoc (FacetDoc { .. }) =
55 Document { _d_document =
56 Node { _node_id = facetDoc_id
57 , _node_hash_id = Nothing
58 , _node_typename = toDBid NodeDocument
60 , _node_parent_id = Nothing
61 , _node_name = facetDoc_title
62 , _node_date = facetDoc_created
63 , _node_hyperdata = facetDoc_hyperdata }
64 , _d_ngrams = Ngrams { _ng_ngrams = []
67 _mapDoc d = Document { _d_document = d
68 , _d_ngrams = Ngrams { _ng_ngrams = []
72 getDocumentsCSV :: UserId
74 -> GargNoServer T.Text -- [Document]
75 getDocumentsCSV uId pId = do
76 DocumentExport { _de_documents } <- getDocumentsJSON uId pId
78 printDebug "[getDocumentsCSV] documents" $ _d_document <$> head _de_documents
80 --let ret = T.pack $ BSC.unpack $ encodeDefaultOrderedByName _de_documents
81 --let ret = TC.convertText $ encodeDefaultOrderedByName _de_documents
82 let ret = TE.decodeUtf8 $ BSC.toStrict $ encodeDefaultOrderedByName _de_documents
84 --printDebug "[getDocumentsCSV] ret" ret
85 liftBase $ T.writeFile "/tmp/out.csv" ret
86 --liftBase $ BSC.writeFile "/tmp/out.csv" $ encodeDefaultOrderedByName _de_documents