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.
49 -- >>> user_new_statuses_url "someuser" 8675309
50 -- "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=someuser&include_rts=true&count=10&since_id=8675309"
52 user_new_statuses_url :: String -> Integer -> String
53 user_new_statuses_url username last_status_id =
54 url ++ "&since_id=" ++ since_id
56 url = user_timeline_url username
57 since_id = show last_status_id
60 -- | Return's username's timeline.
62 get_user_timeline :: Cfg -> String -> IO B.ByteString
63 get_user_timeline cfg username = do
64 let uri = user_timeline_url username
68 -- | Returns the JSON representing all of username's statuses that are
69 -- newer than @last_status_id@.
71 get_user_new_statuses :: Cfg -> String -> Integer -> IO B.ByteString
72 get_user_new_statuses cfg username last_status_id = do
73 let uri = user_new_statuses_url username last_status_id
77 -- | Retrieve a URL, or crash. The request is signed using all of the
78 -- OAuth junk contained in the configuration.
80 http_get :: Cfg -> String -> IO B.ByteString
82 manager <- newManager tlsManagerSettings
83 request <- parseUrl url
84 signed_request <- signOAuth oauth credential request
85 response <- httpLbs signed_request manager
86 return $ responseBody response
89 consumer_key' = BC.pack (consumer_key cfg)
90 consumer_secret' = BC.pack (consumer_secret cfg)
91 access_token' = BC.pack (access_token cfg)
92 access_secret' = BC.pack (access_secret cfg)
96 oauthConsumerKey = consumer_key',
97 oauthConsumerSecret = consumer_secret' }
99 credential :: Credential
100 credential = newCredential access_token' access_secret'