Skip to content

Instantly share code, notes, and snippets.

@dnet
Last active September 26, 2015 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnet/1033035 to your computer and use it in GitHub Desktop.
Save dnet/1033035 to your computer and use it in GitHub Desktop.
GitHub module for IRCnet #hspbp channel
% depends: https://github.com/talentdeficit/jsx
-module(github).
-export([ircmain/1, ircproc/1, reload/2]).
query_repo_json(User, Repo) ->
URL = "https://api.github.com/repos/" ++ User ++ "/" ++ Repo,
{ok, {_, _, JSON}} = httpc:request(get,
{URL, [{"User-Agent", "dehat-github"}]}, [], [{body_format, binary}]),
JSON.
get_lang_by_repo(User, Repo) ->
Doc = jsx:decode(query_repo_json(User, Repo), [return_maps]),
binary_to_list(maps:get(<<"language">>, Doc)).
attitude("Python") -> "juhe_python";
attitude("Erlang") -> "juhe_erlang";
attitude("Perl") -> "juhe_perl";
attitude("Java") -> "dehat_java";
attitude("Ruby") -> "dehat_ruby";
attitude("PHP") -> "dehat_php".
respond_to_repo(User, Repo) ->
"$" ++ attitude(get_lang_by_repo(User, Repo)) ++ "++".
recognize_url(Input) ->
{match, [User, Repo]} = re:run(Input,
"github\.com/([^/]+)/([a-zA-Z0-9\-_]+)",
[{capture, all_but_first, list}]),
timer:sleep(random:uniform(6000) + 3500),
respond_to_repo(User, Repo).
ircmain(Contact) ->
Pid = spawn(?MODULE, ircproc, [Contact]),
Contact ! {subscribe, Pid},
Pid.
reload(Contact, Pid) ->
Pid ! reloaded,
ircproc(Contact).
ircproc(Contact) ->
receive
quit -> quit;
{incoming, Data} ->
S = binary_to_list(Data),
case string:str(S, "github") of
0 -> nop;
_ -> spawn(fun() ->
Contact ! {announce, recognize_url(S)} end)
end,
ircproc(Contact);
{ident, Pid} ->
Pid ! {ident, "github"},
ircproc(Contact);
{reload, Pid} ->
?MODULE:reload(Contact, Pid);
_ -> ircproc(Contact)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment