]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Twitter/Http.hs
Add Haddock documentation for most functions and types.
[dead/halcyon.git] / src / Twitter / Http.hs
1 module Twitter.Http
2 where
3
4 import Network.Curl
5 import System.IO (hPutStrLn, stderr)
6
7 -- |The API URL of username's timeline.
8 --
9 -- See,
10 --
11 -- <http://dev.twitter.com/doc/get/statuses/user_timeline>
12 --
13 user_timeline_url :: String -> String
14 user_timeline_url username =
15 concat [ "http://api.twitter.com/1/statuses/user_timeline.xml",
16 "?screen_name=" ++ username,
17 "&include_rts=true",
18 "&count=10" ]
19
20 -- |Given username's last status id, constructs the API URL for
21 -- username's new statuses. Essentially, 'user_timeline_url' with a
22 -- "since_id" parameter tacked on.
23 user_new_statuses_url :: String -> Integer -> String
24 user_new_statuses_url username last_status_id =
25 concat [ user_timeline_url username,
26 "&since_id=" ++ (show last_status_id) ]
27
28
29 -- |Return's username's timeline, or 'Nothing' if there was an error.
30 get_user_timeline :: String -> IO (Maybe String)
31 get_user_timeline username = do
32 let uri = (user_timeline_url username)
33 timeline <- (http_get uri)
34 return timeline
35
36
37 -- Returns the XML representing all of username's statuses that are
38 -- newer than last_status_id.
39 get_user_new_statuses :: String -> Integer -> IO (Maybe String)
40 get_user_new_statuses username last_status_id = do
41 let uri = (user_new_statuses_url username last_status_id)
42 new_statuses <- (http_get uri)
43 return new_statuses
44
45
46 -- |Uses the CURL API to retrieve uri. Returns 'Nothing' if there was
47 -- an error.
48 http_get :: String -> IO (Maybe String)
49 http_get uri = withCurlDo $ do
50 resp <- curlGetString uri [CurlTimeout 45]
51
52 case resp of
53 (CurlOK, body) -> return (Just body)
54 (code, _) -> do
55 hPutStrLn stderr ("HTTP Error: " ++ (show code))
56 return Nothing