/
twitterl.erl
243 lines (220 loc) · 6.81 KB
/
twitterl.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
%%%-------------------------------------------------------------------
%%% File : twitterl.erl
%%% Author : Yomi (baphled boodah) Akindayini <yomi@boodah.net>
%%% Description : Basic Twitter API, used to interact with twitter.
%%%
%%% twitterl is free software: you can redistribute it and/or modify
%%% it under the terms of the GNU Lesser General Public License as
%%% published by the Free Software Foundation, either version 3 of
%%% the License, or (at your option) any later version.
%%%
%%% twitterl is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%%% GNU Lesser General Public License for more details.
%%%
%%% You should have received a copy of the GNU Lesser General Public
%%% License along with json_parser. If not, see
%%% <http://www.gnu.org/licenses/>.
%%%
%%% Created : 29 Nov 2008 by Yomi Akindayini <yomi@boodah.net>
%%%-------------------------------------------------------------------
-module(twitterl).
-author("Yomi (baphled) Akindayini").
-vsn(01).
-purpose("Twitter client to help me get used to Erlang").
%% Will be useful for getting RSS feedsparsing JSON
-import(json_parser).
%-compile(export_all).
-export([start/0,stop/0]).
%% Search methods
-export([auth_user/2,trends/0,tweets/2,term/1]).
%% Twitter specific methods
-export([user_timeline/1,public_timeline/0,followers/2]).
-include_lib("xmerl/include/xmerl.hrl").
-define(App, "TwitterlClient/0.1").
%% Urls used to make our queries, as our api is twitter centric
%% it makes sense to define them constant initially.
-define(TwitUrl, "http://twitter.com").
-define(SearchUrl, "http://search.twitter.com").
-define(PubTimeUrl, ?TwitUrl"/statuses/public_timeline.rss").
-define(UserTimeUrl, ?TwitUrl"/statuses/user_timeline/").
-define(FollowersUrl, ?TwitUrl"/statuses/followers.rss").
-define(SearchHashUrl, ?SearchUrl"/search.rss?q=").
-define(SearchTrendsUrl, ?SearchUrl"/trends.json").
-define(VerifyUrl, ?TwitUrl"/account/verify_credentials.xml").
start() ->
inets:start().
stop() ->
inets:stop().
%% Retrieve the top 10 trends, only available under JSON atm.
%% Seems to be a bug in the parsing, sometimes we get a mismatch
%% causing an error.
trends() ->
case request_url(?SearchTrendsUrl) of
{ok,Body} ->
Json = json_parser:dvm_parser(list_to_binary(Body)),
{ok,{struct,Reply},_} = Json,
[H|T] = Reply,
case H of
{_,Result} ->
case is_binary(Result) of
false ->
loop_json([],Result);
true ->
%{error,'Unable to retrieve trends'}
trends()
end;
_ ->
{error,'Can not retrieve trends.'}
end;
{error,Error} ->
{error,Error}
end.
%% Loop through our JSON result retrieving each result & turning
%% into the desired format.
loop_json(List,[H|T]) ->
{_,Json} = H,
[{_,Title},{_,Value}] = Json,
Data = [[binary_to_list(Title),binary_to_list(Value)]|List],
loop_json(Data,T);
loop_json(List,[]) ->
list_to_tuple(List).
%% Used to print out tweets to or from a specific user.
tweets(Type,User) ->
case from =:= Type orelse to =:= Type andalso is_atom(Type) of
false ->
{error,"Wrong type"};
true ->
case user_exists(User) of
{true,_} ->
get_twitters(?SearchHashUrl++atom_to_list(Type)++"%3A"++User);
{false,Error} ->
{error,Error}
end
end.
%% Gets twitters with related to term.
%%
term(Term) ->
get_twitters(?SearchHashUrl++"%23"++Term).
%% Used to determine whether a twitters user exists or not.
%%
%% If an error message is found then we know that the user doesnt exist.
%%
user_exists(User) ->
case get_xml(?UserTimeUrl ++ User ++ ".rss") of
{error,Error} ->
{error, Error};
{ok, Xml} ->
case xmerl_xpath:string("//hash/error/text()", Xml) of
[{xmlText, _, _, _, Error, text}] ->
{false, User++": "++Error};
_ ->
{true, User++": found!"}
end
end.
%% Retrieves a users followers.
%%
%% Needs to be worked on
%%
followers(User, Pass) ->
case auth_user(User, Pass) of
true ->
case request_url(?FollowersUrl, User, Pass) of
{ok,Body} ->
Body;
{error,Error} ->
{error,Error}
end;
false ->
{error}
end.
%% Get a specific user's twitters.
%% Don't really need any more, seeing as tweets will do the same thing.
user_timeline(User) ->
case user_exists(User) of
{false,Error} ->
{error, Error};
_ ->
get_twitters(?UserTimeUrl ++ User ++ ".rss")
end.
%% Retrieve the public timeline.
public_timeline() ->
get_twitters(?PubTimeUrl).
%% Get the actual XML response.
get_xml(Url) ->
case request_url(Url) of
{ok,Body} ->
{Xml, _Rest} = xmerl_scan:string(Body),
{ok,Xml};
{error,Error} ->
{error,Error}
end.
%% Get the the twitters in XML format.
get_twitters(Url) ->
case get_xml(Url) of
{ok,Xml} ->
parse_xml(Xml);
{error,Error} ->
{error,Error}
end.
%% Parses our XML sending each tweet to parse_twitters
parse_xml(Xml) ->
Twitters = xmerl_xpath:string("//item/title/text()", Xml),
case 0 =:= length(Twitters) of
false ->
parse_twitters([],Twitters);
_ ->
{error,"Unable to find twitters."}
end.
%% Loops through each of the XML twitter list and prints them out.
parse_twitters(Tweets,[Tweet|Twitters]) ->
case Tweet of
{_,_,_,_,Title,_} ->
%io:format("~s~n", [[Title]]),
MergedList = [Title| Tweets],
parse_twitters(MergedList,Twitters);
_ ->
{error, "Unable to read twitter"}
end;
parse_twitters(List,[]) ->
{ok,List}.
%% Checks to see if the user can actually log in.
auth_user(Login, Password) ->
case request_url(?VerifyUrl, Login, Password) of
{ok,Body} ->
case Body of
"<authorized>true</authorized>" -> true;
_ -> false
end;
{error,Error} ->
{error,Error}
end.
%% Make a request to an URL.
request_url(Url) ->
check_response(http:request(get, {Url, headers(nil, nil)}, [], [])).
%% Make an authenticated request to the specified URL.
request_url(Url, Login, Pass) ->
check_response(http:request(get, {Url, headers(Login, Pass)}, [], [])).
%% Checks out HTTP response, if we get a 200 retrieve
%% the response body, otherwise return the status code & message.
check_response(Response) ->
case Response of
{ok, {Status, _Headers, Body}} ->
case Status of
{_,200,_} ->
{ok,Body};
{_,Code,Msg} ->
{error,{Code,Msg}}
end;
_ -> {error}
end.
headers(nil, nil) -> [{"User-Agent", ?App}];
headers(User, Pass) when is_binary(User) ->
headers(binary_to_list(User), Pass);
headers(User, Pass) when is_binary(Pass) ->
headers(User, binary_to_list(Pass));
headers(User, Pass) ->
Auth = base64:encode(User ++ ":" ++ Pass),
Basic = lists:flatten(io_lib:fwrite("Basic ~s", [Auth])),
[{"User-Agent", ?App}, {"Authorization", Basic}].