Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 377 lines (339 sloc) 12.301 kb
c2e5631 @baphled Added source file, will clean up later
authored
1 %%%-------------------------------------------------------------------
af44636 @baphled Refactored trends to use json_parse for our JSON parsing.
authored
2 %%% File : twitterl.erl
c2e5631 @baphled Added source file, will clean up later
authored
3 %%% Author : Yomi (baphled boodah) Akindayini <yomi@boodah.net>
dcbd78f @baphled General housekeeping and refactoring to clean up twitterl_interfaces
authored
4 %%% Description : Basic Twitter ineterface, used by twitterl to retrieve
5 %%% search and status information from twitter.
c2e5631 @baphled Added source file, will clean up later
authored
6 %%%
af44636 @baphled Refactored trends to use json_parse for our JSON parsing.
authored
7 %%% twitterl is free software: you can redistribute it and/or modify
8 %%% it under the terms of the GNU Lesser General Public License as
9 %%% published by the Free Software Foundation, either version 3 of
10 %%% the License, or (at your option) any later version.
11 %%%
12 %%% twitterl is distributed in the hope that it will be useful,
13 %%% but WITHOUT ANY WARRANTY; without even the implied warranty of
14 %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 %%% GNU Lesser General Public License for more details.
16 %%%
17 %%% You should have received a copy of the GNU Lesser General Public
18 %%% License along with json_parser. If not, see
19 %%% <http://www.gnu.org/licenses/>.
20 %%%
c2e5631 @baphled Added source file, will clean up later
authored
21 %%% Created : 29 Nov 2008 by Yomi Akindayini <yomi@boodah.net>
22 %%%-------------------------------------------------------------------
52af3cc @baphled Renamed twitterl to twitter_interface, twitterl_client to be removed sho...
authored
23 -module(twitterl_interface).
af44636 @baphled Refactored trends to use json_parse for our JSON parsing.
authored
24 -author("Yomi (baphled) Akindayini").
5c80a53 @baphled Refactored trends to know rerun itself if we encounter
authored
25 -vsn(01).
8f811e5 Some cleaning up.
Yomi (baphled boodah) Akindayini authored
26 -purpose("A basic Twitter client.").
c2e5631 @baphled Added source file, will clean up later
authored
27
46bf144 @baphled A little more tidying up, will working on refactoring this into a gen_se...
authored
28 %% Will be useful for getting RSS feedsparsing JSON
af44636 @baphled Refactored trends to use json_parse for our JSON parsing.
authored
29 -import(json_parser).
c2e5631 @baphled Added source file, will clean up later
authored
30
6b6773d @baphled General housekeeping, will add a recieve loop shortly to so a user
authored
31 -record(status, {created_at, id, text, source, truncated, in_reply_to_status_id, in_reply_to_user_id, favorited,user}).
f2afed9 @baphled Added parse_item(s) to parse our RSS data, will remove parse_xml/2 &
authored
32 -record(tweet, {title, pubDate, link}).
b3c83f1 @baphled General housekeeping.
authored
33 -record(user, {id, name, screen_name, location, description, profile_image_url, url, protected, followers_count, friends_count, created_at, favourites_count, utc_offset, time_zone, following, notifications, statuses_count,status}).
fb2753e @baphled Added functionality to parse user information, need to improve,
authored
34
68dd0e9 @baphled Refactored handle_status, is now split into logical order, handle_status...
authored
35 -export([handle_status/3,handle_status/4,handle_user/3,handle_user/4]).
57ce6b7 @baphled Renamed friends/followers to user_friends/user_followers
authored
36
de152de @baphled Finally got the follower method working, is currently only responding
authored
37 % Search methods
c2e5631 @baphled Added source file, will clean up later
authored
38 -export([auth_user/2,trends/0,tweets/2,term/1]).
39 %% Twitter specific methods
661e670 @baphled Had a problem with get_xml(Url, nil,nil), constantly stalled. now
authored
40 -export([user_timeline/1,public_timeline/0]).
c2e5631 @baphled Added source file, will clean up later
authored
41
42 -include_lib("xmerl/include/xmerl.hrl").
43
86fa481 @baphled General tidying up, for some reason am still unable to get a users
authored
44 -define(App, "TwitterlClient/0.1").
c2e5631 @baphled Added source file, will clean up later
authored
45
46 %% Urls used to make our queries, as our api is twitter centric
47 %% it makes sense to define them constant initially.
48 -define(TwitUrl, "http://twitter.com").
49 -define(SearchUrl, "http://search.twitter.com").
50 -define(PubTimeUrl, ?TwitUrl"/statuses/public_timeline.rss").
51 -define(UserTimeUrl, ?TwitUrl"/statuses/user_timeline/").
5a11004 @baphled The method user_show is now working, along with the rest of the
authored
52 -define(UsersUrl, ?TwitUrl"/users/").
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
53 -define(StatusesUrl, ?TwitUrl"/statuses/").
c2e5631 @baphled Added source file, will clean up later
authored
54 -define(SearchHashUrl, ?SearchUrl"/search.rss?q=").
55 -define(SearchTrendsUrl, ?SearchUrl"/trends.json").
56 -define(VerifyUrl, ?TwitUrl"/account/verify_credentials.xml").
fc8047e @baphled General cleanup.
authored
57
c2e5631 @baphled Added source file, will clean up later
authored
58 %% Retrieve the top 10 trends, only available under JSON atm.
fab2db1 @baphled Refactored so that trends now returns a list of
authored
59 %% Seems to be a bug in the parsing, sometimes we get a mismatch
60 %% causing an error.
c2e5631 @baphled Added source file, will clean up later
authored
61 trends() ->
33f27a8 @baphled General house keeping.
authored
62 parse_json(?SearchTrendsUrl).
63
64 parse_json(Url) ->
26606e0 @baphled More housekeeping.
authored
65 case request_url(get, Url, nil, nil) of
55db5aa @baphled Refactored twitterl slightly so that all methods calls to twitter now us...
authored
66 {ok,Body} ->
af44636 @baphled Refactored trends to use json_parse for our JSON parsing.
authored
67 Json = json_parser:dvm_parser(list_to_binary(Body)),
fab2db1 @baphled Refactored so that trends now returns a list of
authored
68 {ok,{struct,Reply},_} = Json,
52af3cc @baphled Renamed twitterl to twitter_interface, twitterl_client to be removed sho...
authored
69 [H|_T] = Reply,
fab2db1 @baphled Refactored so that trends now returns a list of
authored
70 case H of
71 {_,Result} ->
44c7832 @baphled A little more refactoring to trends so that in the advent
authored
72 case is_binary(Result) of
73 false ->
a63d9b4 Put {ok,Results} back into trends, seeing as it would error out otherwis...
Yomi (baphled boodah) Akindayini authored
74 {ok,loop_json([],Result)};
44c7832 @baphled A little more refactoring to trends so that in the advent
authored
75 true ->
043794b @baphled Rename twitterl to twitter_interface, will now call our
authored
76 trends()
44c7832 @baphled A little more refactoring to trends so that in the advent
authored
77 end;
fab2db1 @baphled Refactored so that trends now returns a list of
authored
78 _ ->
79 {error,'Can not retrieve trends.'}
80 end;
55db5aa @baphled Refactored twitterl slightly so that all methods calls to twitter now us...
authored
81 {error,Error} ->
82 {error,Error}
83 end.
c2e5631 @baphled Added source file, will clean up later
authored
84
fab2db1 @baphled Refactored so that trends now returns a list of
authored
85 %% Loop through our JSON result retrieving each result & turning
86 %% into the desired format.
87 loop_json(List,[H|T]) ->
88 {_,Json} = H,
89 [{_,Title},{_,Value}] = Json,
88a28b1 @baphled Fixed bug in user_exists, which was introduced when refactoring
authored
90 Data = [[binary_to_list(Title),binary_to_list(Value)]|List],
fab2db1 @baphled Refactored so that trends now returns a list of
authored
91 loop_json(Data,T);
92 loop_json(List,[]) ->
c43166d Refactored loop_json so that it returns the proper pattern {ok, Results}
Yomi (baphled boodah) Akindayini authored
93 list_to_tuple(List).
fab2db1 @baphled Refactored so that trends now returns a list of
authored
94
c2e5631 @baphled Added source file, will clean up later
authored
95 %% Used to print out tweets to or from a specific user.
f66f088 @baphled Refactored to use atoms instead of items as Type parameter.
authored
96 tweets(Type,User) ->
97 case from =:= Type orelse to =:= Type andalso is_atom(Type) of
c2e5631 @baphled Added source file, will clean up later
authored
98 false ->
99 {error,"Wrong type"};
100 true ->
101 case user_exists(User) of
102 {true,_} ->
f66f088 @baphled Refactored to use atoms instead of items as Type parameter.
authored
103 get_twitters(?SearchHashUrl++atom_to_list(Type)++"%3A"++User);
c2e5631 @baphled Added source file, will clean up later
authored
104 {false,Error} ->
d5a28d4 @baphled Fixed another bug, this time in tweets, with the same issue as
authored
105 {false,Error};
106 {error, Error} ->
107 {error, Error}
76729ce @baphled Need to implement status functionality, am having problems parsing
authored
108 end
c2e5631 @baphled Added source file, will clean up later
authored
109 end.
110
111 %% Gets twitters with related to term.
112 %%
d5a28d4 @baphled Fixed another bug, this time in tweets, with the same issue as
authored
113 term(Term) ->
114 case is_list(Term) of
115 true -> get_twitters(?SearchHashUrl++"%23"++Term);
116 _ -> {error, 'Term must be a list'}
117 end.
c2e5631 @baphled Added source file, will clean up later
authored
118
119 %% Used to determine whether a twitters user exists or not.
120 %%
121 %% If an error message is found then we know that the user doesnt exist.
122 %%
123 user_exists(User) ->
661e670 @baphled Had a problem with get_xml(Url, nil,nil), constantly stalled. now
authored
124 case get_xml(?UserTimeUrl ++ User ++ ".rss") of
88a28b1 @baphled Fixed bug in user_exists, which was introduced when refactoring
authored
125 {error,Error} ->
126 {error, Error};
127 {ok, Xml} ->
128 case xmerl_xpath:string("//hash/error/text()", Xml) of
129 [{xmlText, _, _, _, Error, text}] ->
130 {false, User++": "++Error};
131 _ ->
132 {true, User++": found!"}
133 end
c2e5631 @baphled Added source file, will clean up later
authored
134 end.
135
82c7093 @baphled Removed followers functionality and moved it back into twitterl_interfac...
authored
136
c2e5631 @baphled Added source file, will clean up later
authored
137 %% Get a specific user's twitters.
55db5aa @baphled Refactored twitterl slightly so that all methods calls to twitter now us...
authored
138 %% Don't really need any more, seeing as tweets will do the same thing.
c2e5631 @baphled Added source file, will clean up later
authored
139 user_timeline(User) ->
140 case user_exists(User) of
141 {false,Error} ->
142 {error, Error};
143 _ ->
144 get_twitters(?UserTimeUrl ++ User ++ ".rss")
145 end.
146
147 %% Retrieve the public timeline.
148 public_timeline() ->
149 get_twitters(?PubTimeUrl).
82c7093 @baphled Removed followers functionality and moved it back into twitterl_interfac...
authored
150
dcbd78f @baphled General housekeeping and refactoring to clean up twitterl_interfaces
authored
151 %% Checks to see if the user can actually log in.
152 auth_user(Login, Password) ->
26606e0 @baphled More housekeeping.
authored
153 case request_url(get,?VerifyUrl, Login, Password) of
dcbd78f @baphled General housekeeping and refactoring to clean up twitterl_interfaces
authored
154 {ok,Body} ->
155 case Body of
156 "<authorized>true</authorized>" -> true;
157 _ -> false
158 end;
159 {error,Error} ->
160 {error,Error}
161 end.
162
82c7093 @baphled Removed followers functionality and moved it back into twitterl_interfac...
authored
163 %% Methods to retrieve user based information.
164
a522d11 @baphled More house keeping, will create a bot shortly.
authored
165 %%% Handles each of our user & status requests
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
166 %@private
1b1d4f6 @baphled Found a bug in handle_status, for some reason passing nil to handle_stat...
authored
167 handle_status(Type,User,Pass) ->
168 handle_status(Type,User,Pass,nil).
b36e2d1 @baphled Added functionality to show a specific status, will need to work on
authored
169 handle_status(Type,User,Pass,Args) ->
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
170 case Type of
517d888 @baphled Refactored get_xml and handle_status, added new status functionality
authored
171 new ->
172 HttpResponse = request_url(post,?StatusesUrl++"update.xml", User, Pass, Args),
173 HttpResponse;
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
174 user_timeline ->
5a308df @baphled Refactored to show a user, will wrap this into twitterl shortly,
authored
175 get_status(?StatusesUrl++"user_timeline.xml",User,Pass);
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
176 public_timeline ->
5a308df @baphled Refactored to show a user, will wrap this into twitterl shortly,
authored
177 get_status(?StatusesUrl++"public_timeline.xml",User,Pass);
88dd8c7 @baphled General cleanup in interface, actually named show status show, as
authored
178 status_show ->
b36e2d1 @baphled Added functionality to show a specific status, will need to work on
authored
179 case is_list(Args) of
5a308df @baphled Refactored to show a user, will wrap this into twitterl shortly,
authored
180 true -> get_status(?StatusesUrl++"show.xml?id="++Args, User,Pass);
b36e2d1 @baphled Added functionality to show a specific status, will need to work on
authored
181 _ -> {error, {Type, Args}}
182 end;
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
183 _ ->
184 {error,"Invalid status type"}
185 end.
186
68dd0e9 @baphled Refactored handle_status, is now split into logical order, handle_status...
authored
187 handle_user(Type,User,Pass) ->
188 handle_user(Type,User,Pass,nil).
189 handle_user(Type,User,Pass,Args) ->
190 case Type of
191 user_followers ->
192 get_user(?StatusesUrl++"followers.xml",User,Pass);
193 user_friends ->
194 get_user(?StatusesUrl++"friends.xml",User,Pass);
195 user_show ->
196 case is_list(Args) of
197 true -> get_user(?UsersUrl"show/" ++ Args ++ ".xml", User,Pass);
198 _ -> {error, {Type, Args}}
199 end
200 end.
517d888 @baphled Refactored get_xml and handle_status, added new status functionality
authored
201
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
202 get_user(Url,User,Pass) ->
517d888 @baphled Refactored get_xml and handle_status, added new status functionality
authored
203 case get_xml(get,Url, User, Pass) of
82c7093 @baphled Removed followers functionality and moved it back into twitterl_interfac...
authored
204 {ok,Xml} ->
b3c83f1 @baphled General housekeeping.
authored
205 lists:reverse(parse_users(Xml));
82c7093 @baphled Removed followers functionality and moved it back into twitterl_interfac...
authored
206 {error,Error} ->
207 {error,Error}
208 end.
209
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
210 get_status(Url,User,Pass) ->
517d888 @baphled Refactored get_xml and handle_status, added new status functionality
authored
211 case get_xml(get,Url, User, Pass) of
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
212 {ok,Xml} ->
f3079ba @baphled Sorted out results so that the latest are at the end.
authored
213 lists:reverse(parse_statuses(Xml));
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
214 {error,Error} ->
215 {error,Error}
216 end.
217
dcbd78f @baphled General housekeeping and refactoring to clean up twitterl_interfaces
authored
218 %% Parsing functionality
661e670 @baphled Had a problem with get_xml(Url, nil,nil), constantly stalled. now
authored
219 get_xml(Url) ->
517d888 @baphled Refactored get_xml and handle_status, added new status functionality
authored
220 get_xml(get,Url, nil, nil).
221 get_xml(post,Url,Login,Password) ->
222 case request_url(post,Url,Login,Password) of
223 {ok, Body} ->
224 {ok, get_body(Body)};
225 {error, Error} ->
226 {error, Error}
227 end;
c2e5631 @baphled Added source file, will clean up later
authored
228 %% Get the actual XML response.
517d888 @baphled Refactored get_xml and handle_status, added new status functionality
authored
229 get_xml(get,Url,Login,Password) ->
26606e0 @baphled More housekeeping.
authored
230 case request_url(get,Url,Login,Password) of
76729ce @baphled Need to implement status functionality, am having problems parsing
authored
231 {ok, Body} ->
232 {ok, get_body(Body)};
233 {error, Error} ->
234 {error, Error}
55db5aa @baphled Refactored twitterl slightly so that all methods calls to twitter now us...
authored
235 end.
76729ce @baphled Need to implement status functionality, am having problems parsing
authored
236
c2e5631 @baphled Added source file, will clean up later
authored
237 %% Get the the twitters in XML format.
238 get_twitters(Url) ->
517d888 @baphled Refactored get_xml and handle_status, added new status functionality
authored
239 case get_xml(get,Url,nil, nil) of
55db5aa @baphled Refactored twitterl slightly so that all methods calls to twitter now us...
authored
240 {ok,Xml} ->
f3079ba @baphled Sorted out results so that the latest are at the end.
authored
241 lists:reverse(parse_items(Xml));
55db5aa @baphled Refactored twitterl slightly so that all methods calls to twitter now us...
authored
242 {error,Error} ->
243 {error,Error}
244 end.
fb2753e @baphled Added functionality to parse user information, need to improve,
authored
245
33f27a8 @baphled General house keeping.
authored
246 get_body(Html) ->
247 case xmerl_scan:string(Html) of
248 {Xml, _Rest} ->
249 Xml;
250 _ ->
251 {error,"Unable to parse XML"}
252 end.
253
6b6773d @baphled General housekeeping, will add a recieve loop shortly to so a user
authored
254 %% Parses our XML
f2afed9 @baphled Added parse_item(s) to parse our RSS data, will remove parse_xml/2 &
authored
255 parse_items(Xml) ->
256 [parse_item(Item) || Item <- xmerl_xpath:string("/rss/channel/item", Xml)].
257
82c7093 @baphled Removed followers functionality and moved it back into twitterl_interfac...
authored
258 parse_users(Xml) ->
1b1d4f6 @baphled Found a bug in handle_status, for some reason passing nil to handle_stat...
authored
259 [parse_user(User) || User <- xmerl_xpath:string("//users/user|//user",Xml)].
82c7093 @baphled Removed followers functionality and moved it back into twitterl_interfac...
authored
260
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
261 parse_statuses(Xml) ->
1b1d4f6 @baphled Found a bug in handle_status, for some reason passing nil to handle_stat...
authored
262 [parse_status(Status) || Status <- xmerl_xpath:string("//statuses/status|//status",Xml)].
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
263
79ad71f @baphled Finally able to parse_statuses, basically had the guards the wrong
authored
264 parse_status(Xml) when is_list(Xml) ->
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
265 xmerl_xpath:string("/status",Xml);
79ad71f @baphled Finally able to parse_statuses, basically had the guards the wrong
authored
266 parse_status(Node) when is_tuple(Node)->
6b6773d @baphled General housekeeping, will add a recieve loop shortly to so a user
authored
267 Status = #status{
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
268 created_at = format_text(Node, ["/status/created_at/text()"],""),
269 id = format_text(Node, ["/status/id/text()"],""),
270 text = format_text(Node, ["/status/text/text()"],""),
271 source = format_text(Node, ["/status/source/text()"],""),
272 truncated = format_text(Node, ["/status/truncated/text()"],""),
273 in_reply_to_status_id = format_text(Node, ["/status/in_reply_to_status_id/text()"],""),
274 in_reply_to_user_id = format_text(Node, ["/status/in_reply_to_user_id/text()"],""),
275 favorited = format_text(Node, ["/status/favourited/text()"],"")
276 },
76729ce @baphled Need to implement status functionality, am having problems parsing
authored
277 case xmerl_xpath:string("/status/user", Node) of
a522d11 @baphled More house keeping, will create a bot shortly.
authored
278 [] -> Status;
6b6773d @baphled General housekeeping, will add a recieve loop shortly to so a user
authored
279 [User] -> Status#status{ user = parse_user(User) }
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
280 end.
281
a522d11 @baphled More house keeping, will create a bot shortly.
authored
282 parse_item(Node) ->
283 Item = #tweet {
284 title = format_text(Node, ["/item/title/text()"],""),
285 pubDate = format_text(Node, ["/item/pubDate/text()"],""),
286 link = format_text(Node, ["/item/link/text()"], "")
f2afed9 @baphled Added parse_item(s) to parse our RSS data, will remove parse_xml/2 &
authored
287 },
a522d11 @baphled More house keeping, will create a bot shortly.
authored
288 Item.
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
289
290 %%% Parses our user information.
291 %%% Should really find a better way of doing this, we repeating ourselves
292 %%%
293 parse_user(Node) ->
82c7093 @baphled Removed followers functionality and moved it back into twitterl_interfac...
authored
294 User = #user {
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
295 id = format_text(Node,["/user/id/text()"], ""),
296 name = format_text(Node, ["/user/name/text()"], ""),
297 screen_name = format_text(Node, ["/user/screen_name/text()"], ""),
298 location = format_text(Node, ["/user/location/text()"], ""),
299 description = format_text(Node, ["/user/description/text()"], ""),
300 profile_image_url = format_text(Node, ["/user/profile_image_url/text()"], ""),
301 url = format_text(Node, ["/user/url/text()"], ""),
302 protected = format_text(Node, ["/user/protected/text()"], ""),
b3c83f1 @baphled General housekeeping.
authored
303 followers_count = format_text(Node, ["/user/followers_count/text()"], ""),
304 friends_count = format_text(Node, ["/user/friends_count/text()"], ""),
305 created_at = format_text(Node, ["/user/created_at/text()"], ""),
306 favourites_count = format_text(Node, ["/user/favourites_count/text()"], ""),
307 utc_offset = format_text(Node, ["/user/utc_offset/text()"], ""),
308 time_zone = format_text(Node, ["/user/time_zone/text()"], ""),
309 following = format_text(Node, ["/user/following/text()"], ""),
310 notifications = format_text(Node, ["/user/notifications/text()"], ""),
311 statuses_count = format_text(Node, ["/user/statuses_count/text()"], "")
029afd8 @baphled Refactored show to status show, as we will add the user show functionali...
authored
312 },
9ed95a6 @baphled Sorted out handle_status calls, though we are able to get users
authored
313 case xmerl_xpath:string("/user/status", Node) of
314 [] -> User;
315 [Status] -> User#user{ status = parse_status(Status) }
316 end.
82c7093 @baphled Removed followers functionality and moved it back into twitterl_interfac...
authored
317
318 %% @private
319 format_text(_, [], Result) -> Result;
320 format_text(Xml, [Xpath | Tail], Result) ->
321 Results = lists:foldr(
f3079ba @baphled Sorted out results so that the latest are at the end.
authored
322 fun(#xmlText{value = Value}, Acc) ->
323 lists:append(Value, Acc);
82c7093 @baphled Removed followers functionality and moved it back into twitterl_interfac...
authored
324 (_, Acc) -> Acc
325 end,
326 Result,
327 xmerl_xpath:string(Xpath, Xml)
328 ),
329 format_text(Xml, Tail, Results).
330
c2e5631 @baphled Added source file, will clean up later
authored
331 %% Make a request to an URL.
26606e0 @baphled More housekeeping.
authored
332 request_url(get,Url,nil,nil) ->
5a0a8e9 @baphled Refactored get_xml & request_url so we are able to pass the User
authored
333 check_response(http:request(get, {Url, headers(nil, nil)}, [], []));
c2e5631 @baphled Added source file, will clean up later
authored
334 %% Make an authenticated request to the specified URL.
26606e0 @baphled More housekeeping.
authored
335 request_url(get,Url, Login, Pass) ->
c2e5631 @baphled Added source file, will clean up later
authored
336 check_response(http:request(get, {Url, headers(Login, Pass)}, [], [])).
6f5626c @baphled Added build_keyval & request(post, Url, Login, Pass, Args) need to test
authored
337 request_url(post, Url, Login, Pass,Args) ->
517d888 @baphled Refactored get_xml and handle_status, added new status functionality
authored
338 Body = build_keyval(Args),
6f5626c @baphled Added build_keyval & request(post, Url, Login, Pass, Args) need to test
authored
339 HttpResult = http:request(post, {Url, headers(Login, Pass),
340 "application/x-www-form-urlencoded", Body} , [], []),
517d888 @baphled Refactored get_xml and handle_status, added new status functionality
authored
341 check_response(HttpResult).
6f5626c @baphled Added build_keyval & request(post, Url, Login, Pass, Args) need to test
authored
342
343 build_keyval(Args) ->
344 Body = lists:concat(
345 lists:foldl(
346 fun (Rec, []) -> [Rec]; (Rec, Ac) -> [Rec, "&" | Ac] end,
347 [],
348 [K ++ "=" ++ ibrowse:url_encode(V) || {K, V} <- Args]
349 )
350 ),
351 Body.
c2e5631 @baphled Added source file, will clean up later
authored
352
353 %% Checks out HTTP response, if we get a 200 retrieve
354 %% the response body, otherwise return the status code & message.
355 check_response(Response) ->
356 case Response of
357 {ok, {Status, _Headers, Body}} ->
358 case Status of
359 {_,200,_} ->
55db5aa @baphled Refactored twitterl slightly so that all methods calls to twitter now us...
authored
360 {ok,Body};
c2e5631 @baphled Added source file, will clean up later
authored
361 {_,Code,Msg} ->
362 {error,{Code,Msg}}
363 end;
364 _ -> {error}
365 end.
366
367 headers(nil, nil) -> [{"User-Agent", ?App}];
368 headers(User, Pass) when is_binary(User) ->
369 headers(binary_to_list(User), Pass);
370 headers(User, Pass) when is_binary(Pass) ->
371 headers(User, binary_to_list(Pass));
372 headers(User, Pass) ->
373 Auth = base64:encode(User ++ ":" ++ Pass),
374 Basic = lists:flatten(io_lib:fwrite("Basic ~s", [Auth])),
375 [{"User-Agent", ?App}, {"Authorization", Basic}].
2c938f9 @baphled Refactored to use our user record, with some help from Nick Gerakines
authored
376
Something went wrong with that request. Please try again.