feeds.lua

Mon, 31 Jan 2011 03:27:53 +0100

author
Kim Alvefur <zash@zash.se>
date
Mon, 31 Jan 2011 03:27:53 +0100
changeset 6
4afd0e6206b2
parent 5
58d3cecc68b7
permissions
-rw-r--r--

Add special handling of author and link elements, and a fluffy bunny

3
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
1 local io, string = io, string;
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
2 local error = error;
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
3 local print = print;
6
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
4 local t_insert = table.insert;
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
5 local ipairs = ipairs;
3
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
6
0
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local http = require "socket.http";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local st = require "stanza";
5
58d3cecc68b7 xmppstream -> xmlstream
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
9 local new_stream = require "xmlstream".new;
0
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local xmlns_atom = "http://www.w3.org/2005/Atom";
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
6
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
13 --[[
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
14 By popular request; a fluffy bunny:
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
15
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
16 (\ /)
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
17 (. .)
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
18 c(")(")
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
19
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
20 --]]
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
21
0
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 module "feeds"
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
3
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
24 local translate_entry = {};
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
25
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
26 function translate_entry.atom(feed, stanza)
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
27 if stanza.name == "entry" then
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
28 feed[#feed+1] = stanza;
6
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
29 elseif stanza.name == "author" then
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
30 feed.author = {};
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
31 for i,a in ipairs(stanza) do
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
32 if a.name and a[1] then
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
33 feed.author[a.name] = a[1];
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
34 end
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
35 end
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
36 elseif stanza.name == "link" then
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
37 if not feed.links then feed.links = {}; end
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
38 if stanza.attr.href then
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
39 if stanza.attr.rel then
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
40 feed.links[stanza.attr.rel] = stanza.attr.href;
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
41 else
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
42 t_insert(feed.links, stanza.attr.href)
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
43 end
4afd0e6206b2 Add special handling of author and link elements, and a fluffy bunny
Kim Alvefur <zash@zash.se>
parents: 5
diff changeset
44 end
3
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
45 else
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
46 feed[stanza.name] = stanza:get_text();
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
47 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
48 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
49
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
50 -- RSS->Atom translator
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
51
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
52 -- Helpers to translate item child elements
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
53 local rss2atom = {};
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
54 function rss2atom.title(atom_entry, tag)
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
55 atom_entry:tag("title"):text(tag:get_text()):up();
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
56 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
57
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
58 function rss2atom.link(atom_entry, tag)
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
59 atom_entry:tag("link", { href = tag:get_text() }):up();
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
60 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
61
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
62 function rss2atom.author(atom_entry, tag)
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
63 atom_entry:tag("author")
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
64 :tag("email"):text(tag:get_text()):up()
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
65 :up();
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
66 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
67
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
68 function rss2atom.guid(atom_entry, tag)
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
69 atom_entry:tag("id"):text(tag:get_text()):up();
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
70 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
71
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
72 function rss2atom.category(atom_entry, tag)
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
73 atom_entry:tag("category", { term = tag:get_text(), scheme = tag.attr.domain }):up();
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
74 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
75
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
76 function rss2atom.description(atom_entry, tag)
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
77 atom_entry:tag("summary"):text(tag:get_text()):up();
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
78 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
79
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
80 local months = {
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
81 jan = "01", feb = "02", mar = "03", apr = "04", may = "05", jun = "06";
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
82 jul = "07", aug = "08", sep = "09", oct = "10", nov = "11", dec = "12";
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
83 };
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
84
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
85 function rss2atom.pubDate(atom_entry, tag)
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
86 local pubdate = tag:get_text():gsub("^%a+,", ""):gsub("^%s*", "");
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
87 local date, month, year, hour, minute, second, zone =
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
88 pubdate:match("^(%d%d?) (%a+) (%d+) (%d+):(%d+):?(%d*) ?(.*)$");
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
89 if not date then return; end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
90 if #date == 1 then
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
91 date = "0"..date;
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
92 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
93 month = months[month:sub(1,3):lower()];
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
94 if #year == 2 then -- GAH!
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
95 if tonumber(year) > 80 then
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
96 year = "19"..year;
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
97 else
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
98 year = "20"..year;
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
99 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
100 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
101 if zone == "UT" or zone == "GMT" then zone = "Z"; end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
102 if #second == 0 then
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
103 second = "00";
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
104 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
105 local date_string = string.format("%s-%s-%sT%s:%s:%s%s", year, month, date, hour, minute, second, zone);
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
106 atom_entry:tag("published"):text(date_string):up();
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
107 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
108
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
109 -- Translate a single item to atom
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
110 function translate_entry.rss(feed, stanza)
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
111 if stanza.name == "item" then
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
112 local atom_entry = st.stanza("entry", { xmlns = xmlns_atom });
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
113 for tag in stanza:childtags() do
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
114 local translator = rss2atom[tag.name];
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
115 if translator then
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
116 translator(atom_entry, tag);
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
117 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
118 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
119 translate_entry.atom(feed, atom_entry:reset());
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
120 else
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
121 translate_entry.atom(feed, stanza);
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
122 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
123 end
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
124
0
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 local function new_feed_stream(feed)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 local callbacks = {
3
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
127 streamopened = function (feed, attr, name)
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
128 if name == "feed" and attr.xmlns == xmlns_atom then
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
129 feed.type = "atom";
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
130 feed.xmlns = xmlns_atom;
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
131 feed.notopen = nil;
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
132 elseif name == "rss" and attr.xmlns == "" then
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
133 feed.type = "rss";
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
134 feed.xmlns = "";
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
135 -- Don't open until channel
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
136 elseif feed.type == "rss" and name == "channel" and attr.xmlns == feed.xmlns then
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
137 feed.notopen = nil;
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
138 else
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
139 error("Unsupported feed type: <"
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
140 ..name
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
141 ..(attr.xmlns and (" xmlns='"..attr.xmlns.."'") or "")
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
142 ..">"
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
143 );
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
144 end
0
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 end;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 streamclosed = function (feed)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 end;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 handlestanza = function (feed, stanza)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 -- Skip tags not in the feed's default namespace
3
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
152 if stanza.attr.xmlns ~= feed.xmlns then
0
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 return;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155
3
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
156 translate_entry[feed.type](feed, stanza);
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
157 end;
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
158
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
159 error = function (feed, err, d)
ab02540afcf3 A few changes to support RSS (converts to Atom \o/)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
160 error(err..": "..d);
0
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 end;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 };
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 return new_stream(feed, callbacks);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 function feed_from_string(data)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 local feed = {notopen = true};
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 local stream = new_feed_stream(feed);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 stream:feed(data);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 return feed;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 function open(url)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 if url:match("^file://") then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 return open_file(url);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 elseif url:match("^https?://") then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 return open_http(url);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 else
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 return false, "Could not understand URL: "..url;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 function open_file(filename)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 local file, err = io.open((filename:gsub("^file://", "")));
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 if not file then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 return file, err;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 local feed = feed_from_string(file:read("*a"));
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 file:close();
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 return feed;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 function open_http(url)
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 local data, err = http.request(url);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 if not data then
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 return data, err;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 end
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 local feed = feed_from_string(data);
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 return feed;
319733864f05 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 end

mercurial