Commits

afiskon committed 708031c

initial commit

  • Participants

Comments (0)

Files changed (7)

+deps
+ebin
+#!/bin/sh
+
+erl -noshell \
+  -pa ebin ./deps/*/ebin \
+  -s gsearch main $@ \
+  -s init stop
+% требуемая версия Erlang
+{require_otp_vsn, "R14|R15"}.
+
+% куда складывать зависимости
+{lib_dirs, ["deps"]}.
+
+% список зависимостей
+{deps, [
+  {ibrowse, ".*",
+    {git, "git://github.com/cmullaparthi/ibrowse.git", {tag, "v4.0.1"}}}
+]}.

src/gsearch.app.src

+{application, gsearch,
+ [
+  {description, ""},
+  {vsn, "1"},
+  {registered, []},
+  {applications, [
+                  kernel,
+                  stdlib
+                 ]},
+  {mod, { gsearch_app, []}},
+  {env, []}
+ ]}.
+-module(gsearch).
+-export([main/0, main/1]).
+
+-include("deps/ibrowse/include/ibrowse.hrl").
+
+main(List) ->
+  Query = lists:foldl(
+      fun(X, Sum) -> 
+        Sum ++ " " ++ atom_to_list(X)
+             end, "", List ),
+  EncodedQuery = http_uri:encode(Query),
+  SearchUrl = "http://www.google.com/search?q=" ++ EncodedQuery,
+  io:format("Fetching ~s ...~n", [SearchUrl]),
+  ibrowse:start(),
+  case ibrowse:send_req(SearchUrl, [], get) of
+  { ok, "200" , _Headers, Data } ->
+    parse_serp(Data);
+  Rslt ->
+    io:format("Request failed: ~p~n", [Rslt])
+  end.
+main() ->
+  io:format("Usage: gsearch query~n").
+
+parse_serp(Data) ->
+  Regex = "<a href=\"/url\\?q=(.*?)&amp;sa=U",
+  case re:run(Data, Regex, [ global, {capture, [1], list}]) of
+  { match, MatchList } ->
+    lists:foreach(
+      fun ([Url|_]) ->
+        DecodedUrl = http_uri:decode(Url),
+        io:format("~s~n", [DecodedUrl])
+      end,
+      MatchList );
+  NoMatch ->
+    io:format("Nothing found: ~p~n", [NoMatch])
+  end.

src/gsearch_app.erl

+-module(gsearch_app).
+
+-behaviour(application).
+
+%% Application callbacks
+-export([start/2, stop/1]).
+
+%% ===================================================================
+%% Application callbacks
+%% ===================================================================
+
+start(_StartType, _StartArgs) ->
+    gsearch_sup:start_link().
+
+stop(_State) ->
+    ok.

src/gsearch_sup.erl

+
+-module(gsearch_sup).
+
+-behaviour(supervisor).
+
+%% API
+-export([start_link/0]).
+
+%% Supervisor callbacks
+-export([init/1]).
+
+%% Helper macro for declaring children of supervisor
+-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
+
+%% ===================================================================
+%% API functions
+%% ===================================================================
+
+start_link() ->
+    supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+%% ===================================================================
+%% Supervisor callbacks
+%% ===================================================================
+
+init([]) ->
+    {ok, { {one_for_one, 5, 10}, []} }.
+