-
-
Save polachok/5154974 to your computer and use it in GitHub Desktop.
This file contains 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_, 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