Skip to content

Commit

Permalink
feeds_fetch: switch to erlsom
Browse files Browse the repository at this point in the history
  • Loading branch information
astro committed Jan 10, 2012
1 parent 6dd5bf2 commit 894e499
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
70 changes: 41 additions & 29 deletions apps/feeds/src/feeds_fetch.erl
Expand Up @@ -4,44 +4,56 @@


fetch(Url) ->
Parser = exmpp_xml:start_parser([{max_size, 10 * 1024 * 1024},
{names_as_atom, false},
{engine, expat}]),
http_fold(Url, fun(Chunk, _) ->
R1 = exmpp_xml:parse(Parser, Chunk),
io:format("Parsed: ~p~n",[R1])
end, undefined),
exmpp_xml:parse_final(Parser, <<"">>),
R = exmpp_xml:stop_parser(Parser),
io:format("Downloaded ~s - ~p~n",[Url,R]),
receive
M -> io:format("M=~p~n",[M])
after 1000 ->
ignore
end,
ok.

http_fold(Url, Fold, AccIn) ->
Headers =
[{"User-Agent", "PritTorrent/0.1"}],
{ibrowse_req_id, ReqId} =
ibrowse:send_req(Url, Headers, get, [],
[{stream_to, {self(), once}}], 10000),
http_fold1(ReqId, Fold, AccIn).

http_fold1(ReqId, Fold, AccIn) ->
ok = ibrowse:stream_next(ReqId),
receive
{ibrowse_async_headers, ReqId, [$2, _, _], _Headers} ->
http_fold1(ReqId, Fold, AccIn);
ok;
{ibrowse_async_headers, ReqId, StatusS, _Headers} ->
{Status, _} = string:to_integer(StatusS),
exit({http, Status});
{ibrowse_async_response, ReqId, {error, Err}} ->
exit(Err);
{ibrowse_async_response, ReqId, Data} ->
AccOut = Fold(list_to_binary(Data), AccIn),
http_fold1(ReqId, Fold, AccOut);
{ibrowse_async_response_end, ReqId} ->
AccIn
end.
{ibrowse_async_response, ReqId, {error, Err1}} ->
exit(Err1)
end,

Continuation =
fun(Tail, _) ->
ok = ibrowse:stream_next(ReqId),
receive
{ibrowse_async_response, ReqId, Data} ->
Bin = list_to_binary(Data),
{<<Tail/binary, Bin/binary>>, ok};
{ibrowse_async_response_end, ReqId} ->
{Tail, eof};
{ibrowse_async_response, ReqId, {error, Err2}} ->
exit(Err2)
end
end,

{ok, Result, _TrailingBytes} =
erlsom:parse_sax(<<>>, state, fun sax_callback/2,
[{continuation_function, Continuation, state}]),
{ok, Result}.

sax_callback(startDocument, _) ->
{false, []};
sax_callback({startElement, _, "title", _, _}, {false, Titles}) ->
{"", Titles};
sax_callback({characters, C}, {Title, Titles}) when is_list(Title) ->
{Title ++ C, Titles};
sax_callback({endElement, _, "title", _}, {Title, Titles}) when is_list(Title) ->
{false, [Title | Titles]};
sax_callback(endDocument, State) ->
case State of
{false, Titles} ->
lists:reverse(Titles);
_ ->
[]
end;
sax_callback(_Event, State) ->
State.
7 changes: 3 additions & 4 deletions rebar.config
@@ -1,4 +1,4 @@
{sub_dirs, ["apps/servtorrent", "apps/model", "apps/ui"]}.
{sub_dirs, ["apps/servtorrent", "apps/model", "apps/ui", "apps/feeds"]}.

{deps, [{misultin, "0.*",
{git, "git://github.com/ostinelli/misultin.git", {branch, "master"}}},
Expand All @@ -10,7 +10,6 @@
{git, "git://github.com/tonyg/erlang-rfc4627.git", {branch, "master"}}},
{poolboy, "0.*",
{git, "git://github.com/devinus/poolboy.git", {branch, "master"}}},
%{exmpp, "1.*",
%{git, "git://git.process-one.net/exmpp/mainline.git", {branch, "master"}}}
%{git, "git://github.com/Zert/exmpp.git", {branch, "master"}}}
{erlsom, "1.2.*",
{git, "git://github.com/willemdj/erlsom.git", {branch, "master"}}}
]}.

0 comments on commit 894e499

Please sign in to comment.