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