Skip to content

Instantly share code, notes, and snippets.

@polachok
Forked from dpwiz/twitsearch.hs
Last active December 14, 2015 21:59
Show Gist options
  • Save polachok/5154974 to your computer and use it in GitHub Desktop.
Save polachok/5154974 to your computer and use it in GitHub Desktop.
import Data.Aeson
import Network.HTTP
import System.Environment (getArgs)
import Control.Monad (mapM_, void)
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Class (lift)
import qualified Data.ByteString.Lazy.Char8 as LBS
import qualified Data.Text as T -- can be traded for OverloadedStrings option
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
void $ runMaybeT $ do
MaybeT (return $ decode (LBS.pack body)) >>=
\(Object o) -> MaybeT (return $ HM.lookup (T.pack "results") o) >>=
\(Array l) -> mapM_ printResult (V.toList l)
printResult (Object o) = MaybeT (return $ HM.lookup (T.pack "text") o) >>=
\(String t) -> lift $ putStrLn (T.unpack t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment