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