1 {-# LANGUAGE NamedFieldPuns #-}
2 {-# LANGUAGE TemplateHaskell #-}
4 module Gargantext.API.Node.Corpus.Searx where
6 import Control.Lens (view)
7 import qualified Data.Aeson as Aeson
8 import Data.Aeson.TH (deriveJSON)
9 import qualified Data.Text as T
10 import GHC.Generics (Generic)
11 import Network.HTTP.Client
12 import Network.HTTP.Client.TLS
14 import qualified Prelude as Prelude
15 import Protolude (encodeUtf8, Text, Either)
16 import Gargantext.Prelude
17 import Gargantext.Prelude.Config
19 import Gargantext.Core (Lang(..))
20 import qualified Gargantext.Core.Text.Corpus.API as API
21 import Gargantext.Core.Utils.Prefix (unPrefix)
22 import Gargantext.Database.Action.Flow (FlowCmdM)
23 import Gargantext.Database.Admin.Types.Node (CorpusId)
24 import Gargantext.Database.Prelude (hasConfig)
27 data SearxResult = SearxResult
30 , _sr_content :: Maybe Text
33 , _sr_category :: Text
34 , _sr_pretty_url :: Text }
35 deriving (Show, Eq, Generic)
40 $(deriveJSON (unPrefix "_sr_") ''SearxResult)
42 data SearxResponse = SearxResponse
44 , _srs_number_of_results :: Int
45 , _srs_results :: [SearxResult] }
46 deriving (Show, Eq, Generic)
50 -- , _srs_suggestions :: [Text]
51 -- , _srs_unresponsive_engines :: [Text] }
53 $(deriveJSON (unPrefix "_srs_") ''SearxResponse)
55 data FetchSearxParams = FetchSearxParams
56 { _fsp_manager :: Manager
62 fetchSearxPage :: FetchSearxParams -> IO (Either Prelude.String SearxResponse)
63 fetchSearxPage (FetchSearxParams { _fsp_manager
68 -- https://searx.github.io/searx/dev/search_api.html?highlight=json
69 req <- parseRequest $ T.unpack _fsp_url
70 let request = urlEncodedBody
71 [ --("category_general", "1")
72 ("q", encodeUtf8 _fsp_query)
73 , ("pageno", encodeUtf8 $ T.pack $ show _fsp_pageno)
74 --, ("time_range", "None")
75 --, ("language", "en-US") -- TODO
78 res <- httpLbs request _fsp_manager
79 let dec = Aeson.eitherDecode $ responseBody res :: (Either Prelude.String SearxResponse)
82 triggerSearxSearch :: (MonadBase IO m, FlowCmdM env err m)
88 triggerSearxSearch cid q l = do
89 printDebug "[triggerSearxSearch] cid" cid
90 printDebug "[triggerSearxSearch] q" q
91 printDebug "[triggerSearxSearch] l" l
93 let surl = _gc_frame_searx_url cfg
94 printDebug "[triggerSearxSearch] surl" surl
96 manager <- liftBase $ newManager tlsManagerSettings
97 res <- liftBase $ fetchSearxPage $ FetchSearxParams { _fsp_manager = manager
102 printDebug "[triggerSearxSearch] res" res