2014-11-21 23:05:16 +01:00
|
|
|
{-# LANGUAGE RecordWildCards, OverloadedStrings #-}
|
2014-11-21 00:14:57 +01:00
|
|
|
|
2014-11-21 23:05:16 +01:00
|
|
|
import Network.Wreq (get, responseBody)
|
|
|
|
import Data.Aeson (decode, toJSON)
|
|
|
|
import Data.Maybe (fromJust)
|
|
|
|
import Data.HashMap.Strict (delete)
|
|
|
|
import Control.Monad (when)
|
2014-11-20 21:39:25 +01:00
|
|
|
import Control.Lens
|
2014-11-21 23:05:16 +01:00
|
|
|
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as C
|
|
|
|
import System.Console.ArgParser
|
|
|
|
import System.Process
|
2014-11-20 21:39:25 +01:00
|
|
|
import Json
|
|
|
|
|
2014-11-21 23:05:16 +01:00
|
|
|
|
2014-11-22 15:52:47 +01:00
|
|
|
data Name = Name
|
|
|
|
{ name :: String
|
|
|
|
, url :: String
|
|
|
|
, local :: Bool
|
|
|
|
, block :: Bool
|
|
|
|
, raw :: Bool
|
|
|
|
} deriving (Show)
|
2014-11-21 00:14:57 +01:00
|
|
|
|
|
|
|
parser :: ParserSpec Name
|
2014-11-22 15:52:47 +01:00
|
|
|
parser = Name
|
|
|
|
`parsedBy` reqPos "name" `Descr` "Namecoin name id"
|
|
|
|
`andBy` optPos "http://dns.dnschain.net/" "url"
|
|
|
|
`Descr` "Use custom dnschain API url"
|
|
|
|
`andBy` boolFlag "local" `Descr` "Use local namecoind db"
|
|
|
|
`andBy` boolFlag "block" `Descr` "Show blockchain data (require local connecton)"
|
|
|
|
`andBy` boolFlag "raw" `Descr` "Print raw JSON data"
|
2014-11-21 00:14:57 +01:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = mkApp parser >>= flip runApp exec
|
|
|
|
|
|
|
|
exec :: Name -> IO ()
|
2014-11-22 15:52:47 +01:00
|
|
|
exec args@Name{..} =
|
|
|
|
if local
|
|
|
|
then do
|
|
|
|
out <- readProcess "namecoind" ["name_show", name] ""
|
|
|
|
case decode (C.pack out) of
|
|
|
|
Just res -> do
|
|
|
|
putStrLn (repr value)
|
|
|
|
when block $ putStrLn (repr extra)
|
|
|
|
where
|
|
|
|
value = fromJust . decode . C.pack $ res |: "value"
|
|
|
|
extra = toJSON $ delete "value" res
|
|
|
|
Nothing -> putStrLn "Error parsing data"
|
|
|
|
else do
|
|
|
|
req <- get (url ++ name)
|
|
|
|
let body = req ^. responseBody
|
|
|
|
if raw
|
|
|
|
then print body
|
|
|
|
else putStrLn $
|
|
|
|
case decode body of
|
|
|
|
Just res -> repr res
|
|
|
|
Nothing -> "Error parsing data"
|