Sat, 02 Jan 2010 06:05:26 +0000
Set batch.status = "complete" when all downloads are finished
0 | 1 | local http = require "net.http"; |
2 | local server = require "net.server"; | |
3 | local timer = require "util.timer"; | |
4 | ||
5 | local pairs, ipairs, type, setmetatable = | |
6 | pairs, ipairs, type, setmetatable; | |
7 | ||
8 | module "multihttp" | |
9 | __index = _M; | |
10 | ||
11 | function set_callback(batch, callback) | |
12 | batch.callback = callback; | |
13 | end | |
14 | ||
15 | function set_progress_callback(batch, callback, delay) | |
16 | delay = delay or 1; | |
17 | batch.progress_callback = callback; | |
18 | timer.add_task(delay, function () | |
19 | if batch.progress_callback then | |
20 | batch.progress_callback(batch); | |
21 | if batch.progress_callback then | |
22 | return delay; | |
23 | end | |
24 | end | |
25 | end); | |
26 | end | |
27 | ||
28 | function add_url(batch, url) | |
29 | batch.urls[url] = true; | |
30 | end | |
31 | ||
32 | function remove_url(batch, url) | |
33 | batch.urls[url] = nil; | |
34 | end | |
35 | ||
36 | function download(batch, block) | |
37 | batch.status = "downloading"; | |
38 | local count = 0; | |
39 | for url in pairs(batch.urls) do | |
40 | count = count + 1; | |
41 | batch.urls[url] = | |
42 | http.request(url, nil, function (data, status, request) | |
43 | if batch.callback then | |
44 | batch.downloading_count = batch.downloading_count - 1; | |
45 | batch.callback(url, status, data, request); | |
3
0521ed5b2598
Set batch.status = "complete" when all downloads are finished
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
46 | if batch.downloading_count == 0 then |
0521ed5b2598
Set batch.status = "complete" when all downloads are finished
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
47 | if block then |
0521ed5b2598
Set batch.status = "complete" when all downloads are finished
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
48 | server.setquitting(true); |
0521ed5b2598
Set batch.status = "complete" when all downloads are finished
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
49 | end |
0521ed5b2598
Set batch.status = "complete" when all downloads are finished
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
50 | batch.status = "complete"; |
0 | 51 | end |
52 | end | |
53 | end); | |
54 | end | |
55 | batch.download_count = count; | |
56 | batch.downloading_count = count; | |
57 | if block then | |
58 | server.loop(); | |
59 | server.setquitting(false); | |
60 | end | |
61 | return count; | |
62 | end | |
63 | ||
64 | function progress(batch) | |
65 | local progress = {}; | |
66 | for url, request in pairs(batch.urls) do | |
67 | local p = {}; | |
68 | p.bytes_downloaded = request.havebodylength; | |
69 | p.bytes_total = request.bodylength; | |
70 | if p.bytes_total then | |
71 | p.percent = 100/(p.bytes_total/p.bytes_downloaded); | |
72 | end | |
73 | progress[url] = p; | |
74 | end | |
75 | return progress; | |
76 | end | |
77 | ||
78 | function new(callback, urls) | |
79 | local batch = { callback = callback, urls = {} }; | |
80 | if type(urls) == "table" then | |
81 | for _, url in ipairs(urls) do | |
82 | batch.urls[url] = true; | |
83 | end | |
84 | end | |
85 | return setmetatable(batch, _M); | |
86 | end | |
87 | ||
88 | return _M; |