]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Twitter/Http.hs
Rewrite everything to use the JSON API with OAuth authentication.
[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 -- |The API URL of username's timeline.
17 --
18 -- See,
19 --
20 -- <https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline>
21 --
22 user_timeline_url :: String -> String
23 user_timeline_url username =
24 concat [ "https://api.twitter.com/",
25 "1.1/",
26 "statuses/",
27 "user_timeline.json?",
28 "screen_name=",
29 username,
30 "&include_rts=true&",
31 "count=10" ]
32
33 status_url :: Integer -> String
34 status_url status_id =
35 concat [ "https://api.twitter.com/",
36 "1.1/",
37 "statuses/",
38 "show.json?id=",
39 (show status_id) ]
40
41 -- | Given username's last status id, constructs the API URL for
42 -- username's new statuses. Essentially, 'user_timeline_url' with a
43 -- "since_id" parameter tacked on.
44 user_new_statuses_url :: String -> Integer -> String
45 user_new_statuses_url username last_status_id =
46 concat [ user_timeline_url username,
47 "&since_id=" ++ (show last_status_id) ]
48
49
50 get_status :: Integer -> IO B.ByteString
51 get_status status_id = do
52 let uri = (status_url status_id)
53 status <- (http_get uri)
54 return status
55
56
57 -- | Return's username's timeline.
58 get_user_timeline :: String -> IO B.ByteString
59 get_user_timeline username = do
60 let uri = (user_timeline_url username)
61 timeline <- (http_get uri)
62 return timeline
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 :: String -> Integer -> IO B.ByteString
68 get_user_new_statuses username last_status_id = do
69 let uri = (user_new_statuses_url username last_status_id)
70 new_statuses <- (http_get uri)
71 return new_statuses
72
73
74 -- | Retrieve a URL, or crash.
75 http_get :: String -> IO B.ByteString
76 http_get 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 ""
87 consumer_secret = BC.pack ""
88 access_token = BC.pack ""
89 access_secret = BC.pack ""
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