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