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(..) )
25 -- | The API URL of username's timeline.
29 -- <https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline>
31 user_timeline_url :: String -> String
32 user_timeline_url username =
33 concat [ "https://api.twitter.com/",
36 "user_timeline.json?",
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.
47 user_new_statuses_url :: String -> Integer -> String
48 user_new_statuses_url username last_status_id =
49 url ++ "&since_id=" ++ since_id
51 url = user_timeline_url username
52 since_id = show last_status_id
55 -- | Return's username's timeline.
57 get_user_timeline :: Cfg -> String -> IO B.ByteString
58 get_user_timeline cfg username = do
59 let uri = user_timeline_url username
63 -- | Returns the JSON representing all of username's statuses that are
64 -- newer than @last_status_id@.
66 get_user_new_statuses :: Cfg -> String -> Integer -> IO B.ByteString
67 get_user_new_statuses cfg username last_status_id = do
68 let uri = user_new_statuses_url username last_status_id
72 -- | Retrieve a URL, or crash. The request is signed using all of the
73 -- OAuth junk contained in the configuration.
75 http_get :: Cfg -> String -> IO B.ByteString
77 manager <- newManager tlsManagerSettings
78 request <- parseUrl url
79 signed_request <- signOAuth oauth credential request
80 response <- httpLbs signed_request manager
81 return $ responseBody response
84 consumer_key' = BC.pack (consumer_key cfg)
85 consumer_secret' = BC.pack (consumer_secret cfg)
86 access_token' = BC.pack (access_token cfg)
87 access_secret' = BC.pack (access_secret cfg)
91 oauthConsumerKey = consumer_key',
92 oauthConsumerSecret = consumer_secret' }
94 credential :: Credential
95 credential = newCredential access_token' access_secret'