Skip to content

Instantly share code, notes, and snippets.

@dnet
Created October 23, 2013 17:14
Show Gist options
  • Save dnet/7122722 to your computer and use it in GitHub Desktop.
Save dnet/7122722 to your computer and use it in GitHub Desktop.
Searx module for dnet's fork of jimm-erlang-bot
-module(searx).
-export([ircmain/1, ircproc/1, reload/2]).
ircmain(Contact) ->
Pid = spawn(?MODULE, ircproc, [Contact]),
Contact ! {subscribe, Pid},
ssl:start(),
Pid.
reload(Contact, Pid) ->
Pid ! reloaded,
ircproc(Contact).
ircproc(Contact) ->
receive
quit -> quit;
{incoming, Data} ->
case binary:match(Data, <<":-searx ">>) of
nomatch -> nop;
{Pos, Len} -> spawn(fun() ->
Contact ! {announce, searx(binary:part(
Data, Pos + Len, size(Data) - Pos - Len - 2))} end)
end,
ircproc(Contact);
{ident, Pid} ->
Pid ! {ident, "searx"},
ircproc(Contact);
{reload, Pid} ->
?MODULE:reload(Contact, Pid);
_ -> ircproc(Contact)
end.
searx(BinTerm) ->
Results = get_searx_results(binary_to_list(BinTerm)),
"[searx] " ++ case Results of
[FirstResult | _] ->
{ok, URL} = rfc4627:get_field(FirstResult, "url"),
{ok, Summary} = rfc4627:get_field(FirstResult, "content"),
binary_to_list(<<URL/binary, " -> ", Summary/binary>>);
[] -> "no results";
_ -> "error"
end.
get_searx_results(Term) ->
JSON = query_searx_json(Term),
{ok, Doc, _} = rfc4627:decode(JSON),
{ok, Snippets} = rfc4627:get_field(Doc, "results"),
Snippets.
query_searx_json(Term) ->
URL = "http://searx.0x2a.tk/",
{ok, {_, _, JSON}} = httpc:request(post,
{URL, [{"User-Agent", "juhe-searx"}, {"Accept-Encoding", "gzip"}], "application/x-www-form-urlencoded",
"format=json&q=" ++ edoc_lib:escape_uri(Term)}, [], [{body_format, binary}]),
zlib:gunzip(JSON).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment