]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Twitter/Http.hs
21e47ba1b6352f71b5f5d439f6cf94e848c78c19
[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 -- Examples:
48 --
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"
51 --
52 user_new_statuses_url :: String -> Integer -> String
53 user_new_statuses_url username last_status_id =
54 url ++ "&since_id=" ++ since_id
55 where
56 url = user_timeline_url username
57 since_id = show last_status_id
58
59
60 -- | Return's username's timeline.
61 --
62 get_user_timeline :: Cfg -> String -> IO B.ByteString
63 get_user_timeline cfg username = do
64 let uri = user_timeline_url username
65 http_get cfg uri
66
67
68 -- | Returns the JSON representing all of username's statuses that are
69 -- newer than @last_status_id@.
70 --
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
74 http_get cfg uri
75
76
77 -- | Retrieve a URL, or crash. The request is signed using all of the
78 -- OAuth junk contained in the configuration.
79 --
80 http_get :: Cfg -> String -> IO B.ByteString
81 http_get cfg url = do
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
87
88 where
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)
93
94 oauth :: OAuth
95 oauth = newOAuth {
96 oauthConsumerKey = consumer_key',
97 oauthConsumerSecret = consumer_secret' }
98
99 credential :: Credential
100 credential = newCredential access_token' access_secret'