Mon, 31 Jan 2011 03:27:53 +0100
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 | 7 | local http = require "socket.http"; |
8 | local st = require "stanza"; | |
5 | 9 | local new_stream = require "xmlstream".new; |
0 | 10 | |
11 | local xmlns_atom = "http://www.w3.org/2005/Atom"; | |
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 | 22 | module "feeds" |
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 | 125 | local function new_feed_stream(feed) |
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 | 145 | end; |
146 | ||
147 | streamclosed = function (feed) | |
148 | end; | |
149 | ||
150 | handlestanza = function (feed, stanza) | |
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 | 153 | return; |
154 | end | |
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 | 161 | end; |
162 | }; | |
163 | ||
164 | return new_stream(feed, callbacks); | |
165 | end | |
166 | ||
167 | function feed_from_string(data) | |
168 | local feed = {notopen = true}; | |
169 | ||
170 | local stream = new_feed_stream(feed); | |
171 | stream:feed(data); | |
172 | ||
173 | return feed; | |
174 | end | |
175 | ||
176 | function open(url) | |
177 | if url:match("^file://") then | |
178 | return open_file(url); | |
179 | elseif url:match("^https?://") then | |
180 | return open_http(url); | |
181 | else | |
182 | return false, "Could not understand URL: "..url; | |
183 | end | |
184 | end | |
185 | ||
186 | function open_file(filename) | |
187 | local file, err = io.open((filename:gsub("^file://", ""))); | |
188 | if not file then | |
189 | return file, err; | |
190 | end | |
191 | ||
192 | local feed = feed_from_string(file:read("*a")); | |
193 | ||
194 | file:close(); | |
195 | ||
196 | return feed; | |
197 | end | |
198 | ||
199 | function open_http(url) | |
200 | local data, err = http.request(url); | |
201 | if not data then | |
202 | return data, err; | |
203 | end | |
204 | ||
205 | local feed = feed_from_string(data); | |
206 | ||
207 | return feed; | |
208 | end |