-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefine.hs
More file actions
46 lines (36 loc) · 1.52 KB
/
define.hs
File metadata and controls
46 lines (36 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import Control.Monad
import Data.Aeson
import Data.ByteString.Internal
import Data.ByteString.Lazy
import Data.String
import Data.Text
import qualified Data.HashMap.Strict as DHS
import Network.HTTP
import System.Environment
import Text.Pandoc
main :: IO()
main = do
args <- getArgs
let queryString = "http://en.wiktionary.org/w/api.php?format=json&action=query&prop=extracts&titles=" ++ Prelude.head args ++ "&redirects=true"
response_string <- getResponseBodyFromURL queryString
let byteStringResponse = Data.ByteString.Lazy.pack $ Prelude.map c2w response_string;
let x = (decode >=> anObject >=> DHS.lookup (Data.Text.pack "query")
>=> anObject >=> DHS.lookup (Data.Text.pack "pages")
>=> anObject >=> (\x -> Just (snd $ Prelude.head $ DHS.toList x)) -- Select first element of JSON array with unknown key (page id);
>=> anObject >=> DHS.lookup (Data.Text.pack "extract")) byteStringResponse
case x of
Just (String y) -> Prelude.putStrLn $ htmlToPlain $ Data.Text.unpack y
Nothing -> Prelude.putStrLn "err"
Prelude.putStrLn "done"
htmlToPlain :: String -> String
htmlToPlain =
(writePlain def) .
readHtml def
getResponseBodyFromURL :: String -> IO String
getResponseBodyFromURL url = do
response <- simpleHTTP $ getRequest url
getResponseBody response
-- Helper for passing values in Maybe context in the Kleisli composition (lol)
anObject :: Value -> Maybe Object
anObject (Object m) = Just m
anObject _ = Nothing