1 {-# LANGUAGE NamedFieldPuns #-}
2 {-# LANGUAGE TemplateHaskell #-}
4 module Gargantext.API.Node.Corpus.Searx where
8 import Control.Lens (view)
9 import qualified Data.Aeson as Aeson
10 import Data.Aeson.TH (deriveJSON)
11 import qualified Data.Text as T
12 import GHC.Generics (Generic)
13 import Network.HTTP.Client
14 import Network.HTTP.Client.TLS
16 import qualified Prelude as Prelude
17 import Protolude (encodeUtf8, Text, Either)
18 import Gargantext.Prelude
19 import Gargantext.Prelude.Config
21 import Gargantext.Core (Lang(..))
22 import qualified Gargantext.Core.Text.Corpus.API as API
23 import Gargantext.Core.Utils.Prefix (unPrefix)
24 import Gargantext.Database.Action.Flow.Types (FlowCmdM)
25 import Gargantext.Database.Admin.Types.Node (CorpusId)
26 import Gargantext.Database.Prelude (hasConfig)
29 data SearxResult = SearxResult
32 , _sr_content :: Maybe Text
35 , _sr_category :: Text
36 , _sr_pretty_url :: Text }
37 deriving (Show, Eq, Generic)
42 $(deriveJSON (unPrefix "_sr_") ''SearxResult)
44 data SearxResponse = SearxResponse
46 , _srs_number_of_results :: Int
47 , _srs_results :: [SearxResult] }
48 deriving (Show, Eq, Generic)
52 -- , _srs_suggestions :: [Text]
53 -- , _srs_unresponsive_engines :: [Text] }
55 $(deriveJSON (unPrefix "_srs_") ''SearxResponse)
57 data FetchSearxParams = FetchSearxParams
58 { _fsp_language :: Lang
59 , _fsp_manager :: Manager
65 fetchSearxPage :: FetchSearxParams -> IO (Either Prelude.String SearxResponse)
66 fetchSearxPage (FetchSearxParams { _fsp_language
72 -- https://searx.github.io/searx/dev/search_api.html?highlight=json
73 req <- parseRequest $ T.unpack _fsp_url
74 let request = urlEncodedBody
75 [ --("category_general", "1")
76 ("q", encodeUtf8 _fsp_query)
77 , ("categories", "news") -- https://gitlab.iscpif.fr/gargantext/haskell-gargantext/issues/70#note_3976
78 , ("pageno", encodeUtf8 $ T.pack $ show _fsp_pageno)
79 --, ("time_range", "None")
80 , ("language", encodeUtf8 $ T.pack $ show _fsp_language)
83 res <- httpLbs request _fsp_manager
84 let dec = Aeson.eitherDecode $ responseBody res :: (Either Prelude.String SearxResponse)
87 triggerSearxSearch :: (MonadBase IO m, FlowCmdM env err m)
93 triggerSearxSearch cid q l = do
94 printDebug "[triggerSearxSearch] cid" cid
95 printDebug "[triggerSearxSearch] q" q
96 printDebug "[triggerSearxSearch] l" l
98 let surl = _gc_frame_searx_url cfg
99 printDebug "[triggerSearxSearch] surl" surl
101 manager <- liftBase $ newManager tlsManagerSettings
102 res <- liftBase $ fetchSearxPage $ FetchSearxParams { _fsp_language = l
103 , _fsp_manager = manager
108 printDebug "[triggerSearxSearch] res" res