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