Commit f3e517cc authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

Fix for checking if page is empty to break out of conduit

parent 3a61fb84
...@@ -5,9 +5,12 @@ import Conduit ...@@ -5,9 +5,12 @@ import Conduit
main :: IO () main :: IO ()
main = do main = do
(cnt, resC) <- searchAxv' $ simpleQuery ["banach space"] (cnt, resC) <- searchAxv' $ simpleQuery ["besov space"]
putStrLn $ "Total count: " <> show cnt putStrLn $ "Total count: " <> show cnt
runConduitRes $ resC .| outSnk runConduitRes $ resC .| outSnk
--let resC = searchAxv $ simpleQuery ["besov fluid"]
--runConduitRes $ resC .| outSnk
...@@ -27,7 +27,7 @@ type Limit = Int ...@@ -27,7 +27,7 @@ type Limit = Int
type Soup = Tag String type Soup = Tag String
batchSize :: Int batchSize :: Int
batchSize = 50 batchSize = 500
------------------------------------------------------------ ------------------------------------------------------------
...@@ -41,7 +41,7 @@ batchSize = 50 ...@@ -41,7 +41,7 @@ batchSize = 50
apiSimple :: Maybe Limit -> [String] -> IO [Result] apiSimple :: Maybe Limit -> [String] -> IO [Result]
apiSimple limit query = execQuery limit $ simpleQuery query apiSimple limit query = execQuery limit $ simpleQuery query
apiSimpleC :: MonadResource m => Maybe Limit -> [String] -> IO (Int, C.ConduitT () Result m ()) apiSimpleC :: (Monad m, C.MonadIO m) => Maybe Limit -> [String] -> IO (Int, C.ConduitT () Result m ())
apiSimpleC Nothing query = searchAxv' $ simpleQuery query apiSimpleC Nothing query = searchAxv' $ simpleQuery query
apiSimpleC (Just limit) query = do apiSimpleC (Just limit) query = do
(cnt, resC) <- searchAxv' $ simpleQuery query (cnt, resC) <- searchAxv' $ simpleQuery query
...@@ -87,7 +87,7 @@ searchAxvBody q = ...@@ -87,7 +87,7 @@ searchAxvBody q =
(Status 200 _) -> pure $ getResponseBody rsp (Status 200 _) -> pure $ getResponseBody rsp
st -> error $ "Error:" ++ show st st -> error $ "Error:" ++ show st
searchAxv' :: MonadResource m => Ax.Query -> IO (Int, C.ConduitT () Result m ()) searchAxv' :: (Monad m, C.MonadIO m) => Ax.Query -> IO (Int, C.ConduitT () Result m ())
searchAxv' q = searchAxv' q =
let s = Ax.mkQuery q let s = Ax.mkQuery q
in do rsp <- HT.httpBS =<< liftIO (parseRequest s) in do rsp <- HT.httpBS =<< liftIO (parseRequest s)
...@@ -97,12 +97,18 @@ searchAxv' q = ...@@ -97,12 +97,18 @@ searchAxv' q =
soups@(s:_) -> pure (Ax.totalResults soups, results q soups) soups@(s:_) -> pure (Ax.totalResults soups, results q soups)
st -> error $ "Error:" ++ show st st -> error $ "Error:" ++ show st
searchAxv :: MonadResource m => Ax.Query -> C.ConduitT () Result m () searchAxv :: (Monad m, C.MonadIO m) => Ax.Query -> C.ConduitT () Result m ()
searchAxv q = searchAxv q =
let s = Ax.mkQuery q let s = Ax.mkQuery q
in do rsp <- HT.httpBS =<< liftIO (parseRequest s) in do rsp <- HT.httpBS =<< liftIO (parseRequest s)
case getResponseStatus rsp of case getResponseStatus rsp of
(Status 200 _) -> getSoupC (getResponseBody rsp) >>= results q (Status 200 _) ->
let body = getResponseBody rsp
in
-- If no mo results, break, otherwise this keeps looping for next page forever
case Ax.getEntry (getSoup body) of
([], _) -> C.yieldMany []
_ -> getSoupC body >>= results q
st -> error $ "Error:" ++ show st st -> error $ "Error:" ++ show st
---------------------------------------------------------------------- ----------------------------------------------------------------------
...@@ -114,8 +120,8 @@ getSoup b = parseTags $ B.unpack b ...@@ -114,8 +120,8 @@ getSoup b = parseTags $ B.unpack b
--getSoupC :: MonadResource m => --getSoupC :: MonadResource m =>
-- B.ByteString -> C.ConduitT () a m [Soup] -- B.ByteString -> C.ConduitT () a m [Soup]
getSoupC :: Monad m => B.ByteString -> C.ConduitT () a m [Soup] getSoupC :: (Monad m, C.MonadIO m) => B.ByteString -> C.ConduitT () a m [Soup]
getSoupC b = concat <$> (C.yield b .| toSoup .| CL.consume) getSoupC b = concat <$> (C.yield b .| C.mapM_C (liftIO . putStrLn . show) .| toSoup .| CL.consume)
---------------------------------------------------------------------- ----------------------------------------------------------------------
...@@ -128,7 +134,7 @@ toSoup = C.awaitForever (C.yield . parseTags . B.unpack) ...@@ -128,7 +134,7 @@ toSoup = C.awaitForever (C.yield . parseTags . B.unpack)
---------------------------------------------------------------------- ----------------------------------------------------------------------
-- Yield all entries and fetch next page -- Yield all entries and fetch next page
---------------------------------------------------------------------- ----------------------------------------------------------------------
results :: MonadResource m => results :: (Monad m, C.MonadIO m) =>
Ax.Query -> [Soup] -> C.ConduitT () Result m () Ax.Query -> [Soup] -> C.ConduitT () Result m ()
results q sp = Ax.forEachEntryM sp (C.yield . mkResult) results q sp = Ax.forEachEntryM sp (C.yield . mkResult)
>> searchAxv (Ax.nextPage q) >> searchAxv (Ax.nextPage q)
...@@ -140,6 +146,7 @@ data Result = Result { abstract :: String ...@@ -140,6 +146,7 @@ data Result = Result { abstract :: String
, authors :: [Ax.Author] , authors :: [Ax.Author]
, categories :: [Ax.Category] , categories :: [Ax.Category]
, doi :: String , doi :: String
, id :: String
, journal :: String , journal :: String
, primaryCategory :: Maybe Ax.Category , primaryCategory :: Maybe Ax.Category
, publication_date :: String , publication_date :: String
...@@ -154,6 +161,7 @@ mkResult sp = let abstract = Ax.getSummary sp & clean' ...@@ -154,6 +161,7 @@ mkResult sp = let abstract = Ax.getSummary sp & clean'
authors = Ax.getAuthors sp authors = Ax.getAuthors sp
categories = Ax.getCategories sp categories = Ax.getCategories sp
doi = Ax.getDoi sp doi = Ax.getDoi sp
id = Ax.getId sp
journal = Ax.getJournal sp journal = Ax.getJournal sp
primaryCategory = Ax.getPrimaryCategory sp primaryCategory = Ax.getPrimaryCategory sp
publication_date = Ax.getPublished sp publication_date = Ax.getPublished sp
...@@ -172,6 +180,6 @@ mkResult sp = let abstract = Ax.getSummary sp & clean' ...@@ -172,6 +180,6 @@ mkResult sp = let abstract = Ax.getSummary sp & clean'
---------------------------------------------------------------------- ----------------------------------------------------------------------
-- Sink results To print -- Sink results To print
---------------------------------------------------------------------- ----------------------------------------------------------------------
outSnk :: MonadResource m => C.ConduitT Result C.Void m () outSnk :: (Monad m, C.MonadIO m) => C.ConduitT Result C.Void m ()
outSnk = C.awaitForever (liftIO . putStrLn . show) outSnk = C.awaitForever (liftIO . putStrLn . show)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment