module Twitter.Http where import Network.Curl import System.IO (hPutStrLn, stderr) -- |The API URL of username's timeline. -- -- See, -- -- -- user_timeline_url :: String -> String user_timeline_url username = concat [ "http://api.twitter.com/1/statuses/user_timeline.xml", "?screen_name=" ++ username, "&include_rts=true", "&count=10" ] -- |Given username's last status id, constructs the API URL for -- username's new statuses. Essentially, 'user_timeline_url' with a -- "since_id" parameter tacked on. user_new_statuses_url :: String -> Integer -> String user_new_statuses_url username last_status_id = concat [ user_timeline_url username, "&since_id=" ++ (show last_status_id) ] -- |Return's username's timeline, or 'Nothing' if there was an error. get_user_timeline :: String -> IO (Maybe String) get_user_timeline username = do let uri = (user_timeline_url username) timeline <- (http_get uri) return timeline -- Returns the XML representing all of username's statuses that are -- newer than last_status_id. get_user_new_statuses :: String -> Integer -> IO (Maybe String) get_user_new_statuses username last_status_id = do let uri = (user_new_statuses_url username last_status_id) new_statuses <- (http_get uri) return new_statuses -- |Uses the CURL API to retrieve uri. Returns 'Nothing' if there was -- an error. http_get :: String -> IO (Maybe String) http_get uri = withCurlDo $ do resp <- curlGetString uri [CurlTimeout 45] case resp of (CurlOK, body) -> return (Just body) (code, _) -> do hPutStrLn stderr ("HTTP Error: " ++ (show code)) return Nothing