4 import qualified Data.ByteString.Lazy as B
5 import qualified Data.ByteString.Char8 as BC
6 import qualified Data.Conduit as C
7 import Data.Conduit.Binary (sinkLbs)
8 import Network.HTTP.Conduit
9 import Web.Authenticate.OAuth (
16 -- |The API URL of username's timeline.
20 -- <https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline>
22 user_timeline_url :: String -> String
23 user_timeline_url username =
24 concat [ "https://api.twitter.com/",
27 "user_timeline.json?",
33 status_url :: Integer -> String
34 status_url status_id =
35 concat [ "https://api.twitter.com/",
41 -- | Given username's last status id, constructs the API URL for
42 -- username's new statuses. Essentially, 'user_timeline_url' with a
43 -- "since_id" parameter tacked on.
44 user_new_statuses_url :: String -> Integer -> String
45 user_new_statuses_url username last_status_id =
46 concat [ user_timeline_url username,
47 "&since_id=" ++ (show last_status_id) ]
50 get_status :: Integer -> IO B.ByteString
51 get_status status_id = do
52 let uri = (status_url status_id)
53 status <- (http_get uri)
57 -- | Return's username's timeline.
58 get_user_timeline :: String -> IO B.ByteString
59 get_user_timeline username = do
60 let uri = (user_timeline_url username)
61 timeline <- (http_get uri)
65 -- | Returns the JSON representing all of username's statuses that are
66 -- newer than last_status_id.
67 get_user_new_statuses :: String -> Integer -> IO B.ByteString
68 get_user_new_statuses username last_status_id = do
69 let uri = (user_new_statuses_url username last_status_id)
70 new_statuses <- (http_get uri)
74 -- | Retrieve a URL, or crash.
75 http_get :: String -> IO B.ByteString
77 manager <- newManager def
78 request <- parseUrl url
81 signed_request <- signOAuth oauth credential request
82 response <- http signed_request manager
83 responseBody response C.$$+- sinkLbs
86 consumer_key = BC.pack ""
87 consumer_secret = BC.pack ""
88 access_token = BC.pack ""
89 access_secret = BC.pack ""
93 oauthConsumerKey = consumer_key,
94 oauthConsumerSecret = consumer_secret
97 credential :: Credential
98 credential = newCredential access_token access_secret