module Network.API.GoogleDictionary.Internal where
import Control.Applicative
import Data.Char (chr)
import Data.List (dropWhileEnd)
import Network.HTTP (getRequest, getResponseBody, simpleHTTP)
import Numeric (readHex)
getJson :: String -> IO String
getJson url = simpleHTTP (getRequest url) >>= getResponseBody
decodeHex :: String -> Maybe String
decodeHex ('\\':'x':x:y:ys) =
case readHex [x,y] of
[(n,"")] -> fmap (chr n :) (decodeHex ys)
_ -> Nothing
decodeHex (x:xs) = fmap (x:) (decodeHex xs)
decodeHex [] = Just []
writeResponseDebug :: String -> FilePath -> IO ()
writeResponseDebug word path = do
let url = "http://www.google.com/dictionary/json?callback=a&sl=en&tl=en&q=" ++ word
contents <- dropWhileEnd (/= '}') . drop 2 <$> getJson url
writeFile path contents