]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Core/Text/Corpus/API/Istex.hs
[refactoring] add Gargantext.Defaults and default year/month/day
[gargantext.git] / src / Gargantext / Core / Text / Corpus / API / Istex.hs
1 {-|
2 Module : Gargantext.Core.Text.Corpus.API.Istex
3 Description : Pubmed API connection
4 Copyright : (c) CNRS, 2017
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 -}
11
12
13 module Gargantext.Core.Text.Corpus.API.Istex
14 where
15
16 import Data.List (concat)
17 import Data.Maybe
18 import Data.Text (Text, pack)
19
20 import Gargantext.Core (Lang(..))
21 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataDocument(..))
22 import qualified Gargantext.Defaults as Defaults
23 import Gargantext.Prelude
24 import qualified Gargantext.Core.Text.Corpus.Parsers.Date as Date
25 import qualified ISTEX as ISTEX
26 import qualified ISTEX.Client as ISTEX
27
28 get :: Lang -> Text -> Maybe Integer -> IO [HyperdataDocument]
29 get la q ml = do
30 docs <- ISTEX.getMetadataWith q (fromIntegral <$> ml)
31 either (panic . pack . show) (toDoc' la) docs
32
33 toDoc' :: Lang -> ISTEX.Documents -> IO [HyperdataDocument]
34 toDoc' la docs' = do
35 --printDebug "ISTEX" (ISTEX._documents_total docs')
36 mapM (toDoc la) (ISTEX._documents_hits docs')
37
38 -- | TODO remove dateSplit here
39 -- TODO current year as default
40 toDoc :: Lang -> ISTEX.Document -> IO HyperdataDocument
41 toDoc la (ISTEX.Document i t a ab d s) = do
42 (utctime, (pub_year, pub_month, pub_day)) <- Date.dateSplit la (maybe (Just $ pack $ show Defaults.year) (Just . pack . show) d)
43 pure $ HyperdataDocument { _hd_bdd = Just "Istex"
44 , _hd_doi = Just i
45 , _hd_url = Nothing
46 , _hd_uniqId = Nothing
47 , _hd_uniqIdBdd = Nothing
48 , _hd_page = Nothing
49 , _hd_title = t
50 , _hd_authors = Just $ foldl (\x y -> x <> ", " <> y) "" (map ISTEX._author_name a)
51 , _hd_institutes = Just $ foldl (\x y -> x <> ", " <> y) "" (concat $ (map ISTEX._author_affiliations) a)
52 , _hd_source = Just $ foldl (\x y -> x <> ", " <> y) "" (catMaybes $ map ISTEX._source_title s)
53 , _hd_abstract = ab
54 , _hd_publication_date = fmap (pack . show) utctime
55 , _hd_publication_year = pub_year
56 , _hd_publication_month = pub_month
57 , _hd_publication_day = pub_day
58 , _hd_publication_hour = Nothing
59 , _hd_publication_minute = Nothing
60 , _hd_publication_second = Nothing
61 , _hd_language_iso2 = Just $ (pack . show) la }
62