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 (
18 -- |The API URL of username's timeline.
22 -- <https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline>
24 user_timeline_url :: String -> String
25 user_timeline_url username =
26 concat [ "https://api.twitter.com/",
29 "user_timeline.json?",
35 status_url :: Integer -> String
36 status_url status_id =
37 concat [ "https://api.twitter.com/",
43 -- | Given username's last status id, constructs the API URL for
44 -- username's new statuses. Essentially, 'user_timeline_url' with a
45 -- "since_id" parameter tacked on.
46 user_new_statuses_url :: String -> Integer -> String
47 user_new_statuses_url username last_status_id =
48 concat [ user_timeline_url username,
49 "&since_id=" ++ (show last_status_id) ]
52 get_status :: Cfg -> Integer -> IO B.ByteString
53 get_status cfg status_id = do
54 let uri = status_url status_id
58 -- | Return's username's timeline.
59 get_user_timeline :: Cfg -> String -> IO B.ByteString
60 get_user_timeline cfg username = do
61 let uri = user_timeline_url username
65 -- | Returns the JSON representing all of username's statuses that are
66 -- newer than last_status_id.
67 get_user_new_statuses :: Cfg -> String -> Integer -> IO B.ByteString
68 get_user_new_statuses cfg username last_status_id = do
69 let uri = user_new_statuses_url username last_status_id
73 -- | Retrieve a URL, or crash.
74 http_get :: Cfg -> String -> IO B.ByteString
76 manager <- newManager def
77 request <- parseUrl url
80 signed_request <- signOAuth oauth credential request
81 response <- http signed_request manager
82 responseBody response C.$$+- sinkLbs
85 consumer_key' = BC.pack (consumer_key cfg)
86 consumer_secret' = BC.pack (consumer_secret cfg)
87 access_token' = BC.pack (access_token cfg)
88 access_secret' = BC.pack (access_secret cfg)
92 oauthConsumerKey = consumer_key',
93 oauthConsumerSecret = consumer_secret'
96 credential :: Credential
97 credential = newCredential access_token' access_secret'