Last active
April 11, 2017 09:23
-
-
Save dpwiz/5140973 to your computer and use it in GitHub Desktop.
Haskell version of http://eax.me/scala/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Aeson | |
import Network.HTTP | |
import System.Environment (getArgs) | |
import Control.Monad (mapM_) | |
import Data.Maybe (fromMaybe) | |
import qualified Data.ByteString.Lazy.Char8 as LBS | |
import qualified Data.Text as T | |
import qualified Data.HashMap.Strict as HM | |
import qualified Data.Vector as V | |
main = do | |
args <- getArgs | |
case args of | |
[q] -> fetchTweets q | |
_ -> putStrLn "twitsearch <query>" | |
fetchTweets q = do | |
let req = getRequest $ "http://search.twitter.com/search.json?" ++ urlEncodeVars [("q", q)] | |
body <- simpleHTTP req >>= getResponseBody | |
let doc = fromMaybe Null . decode . LBS.pack $ body | |
let tweets = mapV (asString . key "text") . key "results" $ doc | |
mapM_ putStrLn tweets | |
-- Ad-Hoc JSON queries | |
key :: String -> Value -> Value | |
key k (Object o) = maybe Null id (HM.lookup (T.pack k) o) | |
key k _ = Null | |
mapV :: (Value -> a) -> Value -> [a] | |
mapV f (Array l) = map f (V.toList l) | |
mapV _ _ = [] | |
asString :: Value -> String | |
asString (String t) = T.unpack t | |
asString x = show x |
В принцпе, конкретно JSON ведь несёт уже в себе аналог Nothing, значит можно по-быстрому нашлёпать что-то похожее.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Всегда можно что-нибудь смешное написать, типа:
https://gist.github.com/polachok/5154974