]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Twitter/Http.hs
be10b8697a5be241a23b22ca0e8b5b197ab54013
[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 qualified Data.Conduit as C
11 import Data.Conduit.Binary (sinkLbs)
12 import Network.HTTP.Conduit
13 import Web.Authenticate.OAuth (
14 OAuth(..),
15 Credential,
16 newCredential,
17 newOAuth,
18 signOAuth)
19
20 import Configuration (Cfg(..))
21
22 -- |The API URL of username's timeline.
23 --
24 -- See,
25 --
26 -- <https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline>
27 --
28 user_timeline_url :: String -> String
29 user_timeline_url username =
30 concat [ "https://api.twitter.com/",
31 "1.1/",
32 "statuses/",
33 "user_timeline.json?",
34 "screen_name=",
35 username,
36 "&include_rts=true&",
37 "count=10" ]
38
39
40 -- | Given username's last status id, constructs the API URL for
41 -- username's new statuses. Essentially, 'user_timeline_url' with a
42 -- "since_id" parameter tacked on.
43 user_new_statuses_url :: String -> Integer -> String
44 user_new_statuses_url username last_status_id =
45 url ++ "&since_id=" ++ since_id
46 where
47 url = user_timeline_url username
48 since_id = show last_status_id
49
50
51 -- | Return's username's timeline.
52 get_user_timeline :: Cfg -> String -> IO B.ByteString
53 get_user_timeline cfg username = do
54 let uri = user_timeline_url username
55 http_get cfg uri
56
57
58 -- | Returns the JSON representing all of username's statuses that are
59 -- newer than last_status_id.
60 get_user_new_statuses :: Cfg -> String -> Integer -> IO B.ByteString
61 get_user_new_statuses cfg username last_status_id = do
62 let uri = user_new_statuses_url username last_status_id
63 http_get cfg uri
64
65
66 -- | Retrieve a URL, or crash.
67 http_get :: Cfg -> String -> IO B.ByteString
68 http_get cfg url = do
69 manager <- newManager def
70 request <- parseUrl url
71
72 C.runResourceT $ do
73 signed_request <- signOAuth oauth credential request
74 response <- http signed_request manager
75 responseBody response C.$$+- sinkLbs
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'