Skip to content

Instantly share code, notes, and snippets.

@mallipeddi
Created June 30, 2009 14:49
Show Gist options
  • Save mallipeddi/138193 to your computer and use it in GitHub Desktop.
Save mallipeddi/138193 to your computer and use it in GitHub Desktop.
-module(tweetbed).
-author("Harish Mallipeddi <harish.mallipeddi@gmail.com>").
-behaviour(mattress).
-export([map/0, reduce/2]).
-define(TWITTER_BASE_URL, "http://twitter.com").
-define(TWITTER_USER, "INSERT TWITTER USERID HERE").
-define(TWITTER_PASS, "INSERT PASSWORD HERE").
map() ->
twitter_client:start(),
twitter_client:add_session(?TWITTER_USER, ?TWITTER_PASS),
{Followers,_} = iter_while(
fun ({Followers, Page}) ->
More = twitter_client:call(?TWITTER_USER, user_followers, [{"page", integer_to_list(Page)}]),
ShouldContinue = if
length(More) > 0 -> true;
true -> false
end,
{ShouldContinue, {lists:flatten(Followers, More), Page+1}}
end, {[],1}),
lists:map(fun(F) -> {element(4, F), F} end, Followers).
reduce(reduce, KeyValues) ->
lists:foldl(
fun ({_Username, Follower}, CountsByLoc) ->
Loc = element(21, Follower),
dict:update_counter(Loc, 1, CountsByLoc)
end, dict:new(), KeyValues);
reduce(rereduce, Reds) ->
lists:foldl(
fun (CountsByLoc, FinalCounts) ->
dict:merge(
fun (_Key, Value1, Value2) -> Value1+Value2 end,
CountsByLoc, FinalCounts)
end, dict:new(), Reds).
iter_while(Fun, Acc) ->
{Continue, Acc1} = Fun(Acc),
case Continue of
true -> iter_while(Fun, Acc1);
false -> Acc1
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment