Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(galdl).
- -export([main/1]).
- dlurl() ->
- receive
- {job, [Url, Num, PPID]} ->
- RealUrl = try lists:flatten(io_lib:format(Url, [Num]))
- catch
- error:_ ->
- io:format("An error occured. Check your URL parameter.~n"),
- halt(1)
- end,
- io:format("Downloading ~ts~n", [RealUrl]),
- Basename = filename:basename(RealUrl),
- case httpc:request(get, {RealUrl, []}, [], [{stream, Basename}]) of
- {ok, saved_to_file} ->
- io:format("Saved file ~ts~n", [Basename]),
- PPID ! {done, {self(), Num}};
- {ok, {{_HTTP, Code, _Msg}, _Headers, _Body}} when Code > 499 ->
- PPID ! {temperror, {self(), Num}};
- {ok, {{_HTTP, Code, _Msg}, _Headers, _Body}} when Code > 399, Code < 500 ->
- PPID ! {permerror, {self(), Num}};
- {error, Error} ->
- io:format("A network error occured: ~p~n", [Error]),
- PPID ! {temperror, {self(), Num}}
- end
- end,
- dlurl().
- loop(Url, From, To, TLimit, Proclist, Busylist) ->
- if
- length(Busylist) == 0, From > To ->
- done;
- length(Busylist) < TLimit, From =< To ->
- PID = lists:nth(1, Proclist -- Busylist),
- io:format("Setting the task #~w for the process ~w, Busylist: ~w~n", [From, PID, Busylist]),
- PID ! {job, [Url, From, self()]},
- loop(Url, From + 1, To, TLimit, Proclist, Busylist ++ [PID]);
- true ->
- NewBusylist = receive
- {done, {PID, Num}} ->
- io:format("Thread ~w finished #~w~n", [PID, Num]),
- Busylist -- [PID];
- {permerror, {PID, Num}} ->
- io:format("Thread ~w got a permanent error retrieving #~w, skipping~n", [PID, Num]),
- Busylist -- [PID];
- {temperror, {PID, Num}} ->
- io:format("Thread ~w haven't got ~w, retrying~n", [PID, Num]),
- PID ! {job, [Url, Num, self()]},
- Busylist
- end,
- loop(Url, From, To, TLimit, Proclist, NewBusylist)
- end.
- main([Url, From, To, TLimit]) ->
- inets:start(),
- %%inets:start(httpc, [{profile, myprofile}]),
- %%httpc:set_options([{verbose, debug}]),
- io:format("Module name: ~w PID: ~w~n", [?MODULE, self()]),
- FromI = list_to_integer(From),
- ToI = list_to_integer(To),
- TLimitI = list_to_integer(TLimit),
- loop(Url, FromI, ToI, TLimitI, [spawn(fun() -> dlurl() end) || _ <- lists:seq(1, TLimitI)], []),
- io:format("Done!~n"),
- halt(0);
- main([Url, From, To]) ->
- main([Url, From, To, "5"]);
- main(_) ->
- io:format("Usage: <url> <from> <to> [process_count]~nurl should be formatted like http://example.net/path/to/gallery/~~3..0w.jpg for files like 001.jpg, 002.jpg etc.~n"),
- halt(0).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement