Add local search

This commit is contained in:
Rnhmjoj 2014-11-21 23:05:16 +01:00
parent c663cb0d3c
commit 4f497ae881

38
Main.hs
View File

@ -1,16 +1,24 @@
{-# LANGUAGE RecordWildCards #-} {-# LANGUAGE RecordWildCards, OverloadedStrings #-}
import Network.Wreq (get, responseBody) import Network.Wreq (get, responseBody)
import System.Console.ArgParser import Data.Aeson (decode, toJSON)
import Data.Maybe (fromJust)
import Data.HashMap.Strict (delete)
import Control.Monad (when)
import Control.Lens import Control.Lens
import Data.Aeson
import qualified Data.ByteString.Lazy.Char8 as C
import System.Console.ArgParser
import System.Process
import Json import Json
data Name = Name data Name = Name
{ name :: String { name :: String
, url :: String , url :: String
, local :: Bool , local :: Bool
, raw :: Bool , block :: Bool
, raw :: Bool
} deriving (Show) } deriving (Show)
parser :: ParserSpec Name parser :: ParserSpec Name
@ -18,6 +26,7 @@ parser = Name
`parsedBy` reqPos "name" `Descr` "Namecoin name id" `parsedBy` reqPos "name" `Descr` "Namecoin name id"
`andBy` optPos "http://dns.dnschain.net/" "url" `Descr` "Use custom dnschain API url" `andBy` optPos "http://dns.dnschain.net/" "url" `Descr` "Use custom dnschain API url"
`andBy` boolFlag "local" `Descr` "Use local namecoind db" `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" `andBy` boolFlag "raw" `Descr` "Print raw JSON data"
main :: IO () main :: IO ()
@ -25,10 +34,23 @@ main = mkApp parser >>= flip runApp exec
exec :: Name -> IO () exec :: Name -> IO ()
exec args@Name{..} = exec args@Name{..} =
if local then putStrLn "to do" 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 else do
req <- get (url ++ name) req <- get (url ++ name)
if raw then print $ req ^. responseBody print args
else case decode (req ^. responseBody) of let body = req ^. responseBody
if raw
then print body
else case decode body of
Just res -> putStrLn $ repr res Just res -> putStrLn $ repr res
Nothing -> putStrLn "Error parsing data" Nothing -> putStrLn "Error parsing data"