8 import qualified Data.ByteString.Lazy as B
9 import qualified Data.ByteString.Char8 as BC
10 import qualified Data.Conduit as C
11 import Data.Conduit.Binary (sinkLbs)
12 import Network.HTTP.Conduit
13 import Web.Authenticate.OAuth (
20 import Configuration (Cfg(..))
22 -- |The API URL of username's timeline.
26 -- <https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline>
28 user_timeline_url :: String -> String
29 user_timeline_url username =
30 concat [ "https://api.twitter.com/",
33 "user_timeline.json?",
40 -- | Given username's last status id, constructs the API URL for
41 -- username's new statuses. Essentially, 'user_timeline_url' with a
42 -- "since_id" parameter tacked on.
43 user_new_statuses_url :: String -> Integer -> String
44 user_new_statuses_url username last_status_id =
45 url ++ "&since_id=" ++ since_id
47 url = user_timeline_url username
48 since_id = show last_status_id
51 -- | Return's username's timeline.
52 get_user_timeline :: Cfg -> String -> IO B.ByteString
53 get_user_timeline cfg username = do
54 let uri = user_timeline_url username
58 -- | Returns the JSON representing all of username's statuses that are
59 -- newer than last_status_id.
60 get_user_new_statuses :: Cfg -> String -> Integer -> IO B.ByteString
61 get_user_new_statuses cfg username last_status_id = do
62 let uri = user_new_statuses_url username last_status_id
66 -- | Retrieve a URL, or crash.
67 http_get :: Cfg -> String -> IO B.ByteString
69 manager <- newManager def
70 request <- parseUrl url
73 signed_request <- signOAuth oauth credential request
74 response <- http signed_request manager
75 responseBody response C.$$+- sinkLbs
78 consumer_key' = BC.pack (consumer_key cfg)
79 consumer_secret' = BC.pack (consumer_secret cfg)
80 access_token' = BC.pack (access_token cfg)
81 access_secret' = BC.pack (access_secret cfg)
85 oauthConsumerKey = consumer_key',
86 oauthConsumerSecret = consumer_secret'
89 credential :: Credential
90 credential = newCredential access_token' access_secret'