7 import qualified Data.ByteString.Lazy as B ( ByteString )
8 import qualified Data.ByteString.Char8 as BC ( pack )
9 import Network.HTTP.Client (
14 import Network.HTTP.Client.TLS ( tlsManagerSettings )
15 import Web.Authenticate.OAuth (
22 import Configuration ( Cfg(..) )
24 -- | The API URL of username's timeline.
28 -- <https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline>
30 user_timeline_url :: String -> String
31 user_timeline_url username =
32 concat [ "https://api.twitter.com/",
35 "user_timeline.json?",
42 -- | Given username's last status id, constructs the API URL for
43 -- username's new statuses. Essentially, 'user_timeline_url' with a
44 -- "since_id" parameter tacked on.
45 user_new_statuses_url :: String -> Integer -> String
46 user_new_statuses_url username last_status_id =
47 url ++ "&since_id=" ++ since_id
49 url = user_timeline_url username
50 since_id = show last_status_id
53 -- | Return's username's timeline.
54 get_user_timeline :: Cfg -> String -> IO B.ByteString
55 get_user_timeline cfg username = do
56 let uri = user_timeline_url username
60 -- | Returns the JSON representing all of username's statuses that are
61 -- newer than last_status_id.
62 get_user_new_statuses :: Cfg -> String -> Integer -> IO B.ByteString
63 get_user_new_statuses cfg username last_status_id = do
64 let uri = user_new_statuses_url username last_status_id
68 -- | Retrieve a URL, or crash.
69 http_get :: Cfg -> String -> IO B.ByteString
71 manager <- newManager tlsManagerSettings
72 request <- parseUrl url
73 signed_request <- signOAuth oauth credential request
74 response <- httpLbs signed_request manager
75 return $ responseBody response
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'