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