feeds.lua

Fri, 17 Sep 2010 14:43:38 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 17 Sep 2010 14:43:38 +0100
changeset 1
ee74a5e9419b
parent 0
319733864f05
child 3
ab02540afcf3
permissions
-rw-r--r--

demo.lua: Example usage

0
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local io = io;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local http = require "socket.http";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local st = require "stanza";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local new_stream = require "xmppstream".new;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local xmlns_atom = "http://www.w3.org/2005/Atom";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 module "feeds"
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local function new_feed_stream(feed)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local callbacks = {
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 default_ns = xmlns_atom;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 stream_ns = xmlns_atom; stream_tag = "feed";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 streamopened = function (feed, attr)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 feed.notopen = nil;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 end;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 streamclosed = function (feed)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 handlestanza = function (feed, stanza)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 -- Skip tags not in the feed's default namespace
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 if stanza.attr.xmlns ~= nil then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 return;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 if stanza.name == "entry" then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 feed[#feed+1] = stanza;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 else
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 feed[stanza.name] = stanza:get_text();
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 };
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 return new_stream(feed, callbacks);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 function feed_from_string(data)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local feed = {notopen = true};
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local stream = new_feed_stream(feed);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 stream:feed(data);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 return feed;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 function open(url)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 if url:match("^file://") then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 return open_file(url);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 elseif url:match("^https?://") then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 return open_http(url);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 else
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 return false, "Could not understand URL: "..url;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 function open_file(filename)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local file, err = io.open((filename:gsub("^file://", "")));
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 if not file then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 return file, err;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local feed = feed_from_string(file:read("*a"));
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 file:close();
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 return feed;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 function open_http(url)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 local data, err = http.request(url);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 if not data then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 return data, err;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 local feed = feed_from_string(data);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 return feed;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end

mercurial