]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Twitter/Http.hs
6d055b90f0da7444568670f39586ab6fbc790a87
[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 concat [ user_timeline_url username,
49 "&since_id=" ++ (show last_status_id) ]
50
51
52 get_status :: Cfg -> Integer -> IO B.ByteString
53 get_status cfg status_id = do
54 let uri = status_url status_id
55 http_get cfg uri
56
57
58 -- | Return's username's timeline.
59 get_user_timeline :: Cfg -> String -> IO B.ByteString
60 get_user_timeline cfg username = do
61 let uri = user_timeline_url username
62 http_get cfg uri
63
64
65 -- | Returns the JSON representing all of username's statuses that are
66 -- newer than last_status_id.
67 get_user_new_statuses :: Cfg -> String -> Integer -> IO B.ByteString
68 get_user_new_statuses cfg username last_status_id = do
69 let uri = user_new_statuses_url username last_status_id
70 http_get cfg uri
71
72
73 -- | Retrieve a URL, or crash.
74 http_get :: Cfg -> String -> IO B.ByteString
75 http_get cfg url = do
76 manager <- newManager def
77 request <- parseUrl url
78
79 C.runResourceT $ do
80 signed_request <- signOAuth oauth credential request
81 response <- http signed_request manager
82 responseBody response C.$$+- sinkLbs
83
84 where
85 consumer_key' = BC.pack (consumer_key cfg)
86 consumer_secret' = BC.pack (consumer_secret cfg)
87 access_token' = BC.pack (access_token cfg)
88 access_secret' = BC.pack (access_secret cfg)
89
90 oauth :: OAuth
91 oauth = newOAuth {
92 oauthConsumerKey = consumer_key',
93 oauthConsumerSecret = consumer_secret'
94 }
95
96 credential :: Credential
97 credential = newCredential access_token' access_secret'