libs/multihttp.lua

Sat, 02 Jan 2010 06:05:26 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 02 Jan 2010 06:05:26 +0000
changeset 3
0521ed5b2598
parent 0
6e60da4625db
child 4
a20981a6d88b
permissions
-rw-r--r--

Set batch.status = "complete" when all downloads are finished

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

mercurial