]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Twitter/Http.hs
eec7ab7187c56145ea5cce09603056e659b6c3c2
[dead/halcyon.git] / src / Twitter / Http.hs
1 module Twitter.Http (
2 get_user_new_statuses,
3 get_user_timeline,
4 http_get )
5 where
6
7 import qualified Data.ByteString.Lazy as B ( ByteString )
8 import qualified Data.ByteString.Char8 as BC ( pack )
9 import Network.HTTP.Client (
10 httpLbs,
11 newManager,
12 parseUrl,
13 responseBody )
14 import Network.HTTP.Client.TLS ( tlsManagerSettings )
15 import Web.Authenticate.OAuth (
16 OAuth(..),
17 Credential,
18 newCredential,
19 newOAuth,
20 signOAuth )
21
22 import Configuration ( Cfg(..) )
23
24
25 -- | The API URL of username's timeline.
26 --
27 -- See,
28 --
29 -- <https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline>
30 --
31 user_timeline_url :: String -> String
32 user_timeline_url username =
33 concat [ "https://api.twitter.com/",
34 "1.1/",
35 "statuses/",
36 "user_timeline.json?",
37 "screen_name=",
38 username,
39 "&include_rts=true&",
40 "count=10" ]
41
42
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 --
47 user_new_statuses_url :: String -> Integer -> String
48 user_new_statuses_url username last_status_id =
49 url ++ "&since_id=" ++ since_id
50 where
51 url = user_timeline_url username
52 since_id = show last_status_id
53
54
55 -- | Return's username's timeline.
56 --
57 get_user_timeline :: Cfg -> String -> IO B.ByteString
58 get_user_timeline cfg username = do
59 let uri = user_timeline_url username
60 http_get cfg uri
61
62
63 -- | Returns the JSON representing all of username's statuses that are
64 -- newer than @last_status_id@.
65 --
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
69 http_get cfg uri
70
71
72 -- | Retrieve a URL, or crash. The request is signed using all of the
73 -- OAuth junk contained in the configuration.
74 --
75 http_get :: Cfg -> String -> IO B.ByteString
76 http_get cfg url = do
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
82
83 where
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)
88
89 oauth :: OAuth
90 oauth = newOAuth {
91 oauthConsumerKey = consumer_key',
92 oauthConsumerSecret = consumer_secret' }
93
94 credential :: Credential
95 credential = newCredential access_token' access_secret'