2 Module : Gargantext.Text.Parsers.IsidoreApi
3 Description : To query French Humanities publication database from its API
4 Copyright : (c) CNRS, 2019-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
13 {-# LANGUAGE NoImplicitPrelude #-}
14 {-# LANGUAGE ScopedTypeVariables #-}
15 {-# LANGUAGE OverloadedStrings #-}
17 module Gargantext.Text.Parsers.IsidoreApi where
19 import System.FilePath (FilePath())
20 import Data.Text (Text)
21 import Gargantext.Core (Lang(..))
22 import Gargantext.Database.Types.Node (HyperdataDocument(..))
23 import Gargantext.Prelude
26 import qualified Data.Text as Text
27 import qualified Gargantext.Text.Parsers.Date as Date
28 import qualified Isidore as Isidore
29 import Gargantext.Text.Parsers.CSV (writeDocs2Csv)
30 import Gargantext.Text.Parsers (cleanText)
32 -- | TODO work with the ServantErr
33 get :: Lang -> Maybe Isidore.Limit
34 -> Maybe Isidore.TextQuery -> Maybe Isidore.AuthorQuery
35 -> IO [HyperdataDocument]
38 printErr (DecodeFailure e _) = panic e
39 printErr e = panic (cs $ show e)
41 toIsidoreDocs :: Reply -> [IsidoreDoc]
42 toIsidoreDocs (ReplyOnly r) = [r]
43 toIsidoreDocs (Replies rs) = rs
45 iDocs <- either printErr _content <$> Isidore.get l q a
47 hDocs <- mapM (\d -> isidoreToDoc la d) (toIsidoreDocs iDocs)
50 isidore2csvFile :: FilePath -> Lang -> Maybe Isidore.Limit
51 -> Maybe Isidore.TextQuery -> Maybe Isidore.AuthorQuery
53 isidore2csvFile fp la li tq aq = do
54 hdocs <- get la li tq aq
55 writeDocs2Csv fp hdocs
57 isidoreToDoc :: Lang -> IsidoreDoc -> IO HyperdataDocument
58 isidoreToDoc l (IsidoreDoc t a d u s as) = do
60 author :: Author -> Text
61 author (Author fn ln) = (_name fn) <> ", " <> (_name ln)
62 author (Authors aus) = Text.intercalate ". " $ map author aus
64 creator2text :: Creator -> Text
65 creator2text (Creator au) = author au
66 creator2text (Creators aus') = Text.intercalate ". " $ map author aus'
68 langText :: LangText -> Text
69 langText (LangText _l t1) = t1
70 langText (OnlyText t2 ) = t2
71 langText (ArrayText ts ) = Text.intercalate " " $ map langText ts
73 (utcTime, (pub_year, pub_month, pub_day)) <- Date.split l (maybe (Just "2019") (Just) d)
75 pure $ HyperdataDocument (Just "IsidoreApi")
81 (Just $ cleanText $ langText t)
85 (cleanText <$> langText <$> a)
86 (fmap (Text.pack . show) utcTime)
93 (Just $ (Text.pack . show) l)