diff options
Diffstat (limited to 'app/Main.hs')
-rw-r--r-- | app/Main.hs | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/app/Main.hs b/app/Main.hs index 0368b77..6ddae49 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,4 +1,3 @@ -{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeOperators #-} @@ -43,7 +42,6 @@ import qualified Data.Text as T import Data.Text ( Text ) import qualified Data.Text.Encoding as TE import Data.Text.IO ( hGetContents ) -import GHC.Generics ( Generic ) import Network.Wai.Handler.Warp ( run ) import Network.Wreq ( Response , get @@ -87,7 +85,8 @@ import System.Process ( CreateProcess(..) , createProcess , proc ) -import Text.Pandoc ( WrapOption(..) +import Text.Pandoc ( ReaderOptions(..) + , WrapOption(..) , WriterOptions(..) , def , readMediaWiki @@ -118,7 +117,7 @@ type GetPandocFormat = "wikipedia" :> "pandoc" :> Capture "name" Text :> Get '[JSON] Text type GetWpSummary - = "wikipedia" :> "summary" :> Capture "name" Text :> Get '[PlainText] Text + = "wikipedia" :> "summary" :> Capture "name" Text :> Get '[JSON] WikiSummary type GetInfobox = "wikipedia" :> "infobox" :> Capture "name" Text :> Get '[JSON] (HM.HashMap Text Text) @@ -151,12 +150,18 @@ getWikiFormat name = do ("https://en.wikipedia.org/wiki/" <> (T.unpack name) <> "?action=raw") return $ TE.decodeUtf8 $ BSL.toStrict $ r ^. responseBody +getHtmlFormat :: Text -> Handler Text +getHtmlFormat name = do + r <- liftIO $ get ("https://en.wikipedia.org/wiki/" <> (T.unpack name)) + return $ TE.decodeUtf8 $ BSL.toStrict $ r ^. responseBody + getOrgFormat :: Text -> Handler Text getOrgFormat name = do - wiki <- getWikiFormat name + wiki <- getWikiFormat name + WikiSummary title _ _ <- getWpSummary name liftIO $ runIOorExplode - $ (wikiFilter <$> readMediaWiki def wiki) + $ (wikiFilter title <$> readMediaWiki def wiki) >>= writeOrg def { writerWrapText = WrapNone } getPandocFormat :: Text -> Handler Text @@ -166,11 +171,16 @@ getPandocFormat name = do { writerWrapText = WrapNone } -getWpSummary :: Text -> Handler Text -getWpSummary name = do - r <- liftIO $ get - ("https://en.wikipedia.org/api/rest_v1/page/summary/" <> (T.unpack name)) - return $ TE.decodeUtf8 $ BSL.toStrict $ r ^. responseBody +getWpSummary :: Text -> Handler WikiSummary +getWpSummary name = fmap (fromJust . decode) (liftIO $ getApiWpSummary name) + +getApiWpSummary :: Text -> IO BSL.ByteString +getApiWpSummary name = (^. responseBody) <$> get + ("https://en.wikipedia.org/api/rest_v1/page/summary/" <> (T.unpack name)) + +getWpSummaryFull :: Text -> Handler Text +getWpSummaryFull name = + fmap (TE.decodeUtf8 . BSL.toStrict) (liftIO $ getApiWpSummary name) getInfobox :: Text -> Handler (HM.HashMap Text Text) getInfobox name = do |