comparison mod_pubsub_feeds/feeds.lib.lua @ 2132:b149ea428b81

mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
author Kim Alvefur <zash@zash.se>
date Sun, 20 Mar 2016 12:32:45 +0100
parents
children 85762420a2c0
comparison
equal deleted inserted replaced
2131:ba42c8882026 2132:b149ea428b81
1 local st = require "util.stanza";
2 -- RSS->Atom translator
3 -- http://code.matthewwild.co.uk/lua-feeds/
4
5 -- Helpers to translate item child elements
6 local rss2atom = {};
7 function rss2atom.title(atom_entry, tag)
8 atom_entry:tag("title"):text(tag:get_text()):up();
9 end
10
11 function rss2atom.link(atom_entry, tag)
12 atom_entry:tag("link", { href = tag:get_text() }):up();
13 end
14
15 function rss2atom.author(atom_entry, tag)
16 atom_entry:tag("author")
17 :tag("email"):text(tag:get_text()):up()
18 :up();
19 end
20
21 function rss2atom.guid(atom_entry, tag)
22 atom_entry:tag("id"):text(tag:get_text()):up();
23 end
24
25 function rss2atom.category(atom_entry, tag)
26 atom_entry:tag("category", { term = tag:get_text(), scheme = tag.attr.domain }):up();
27 end
28
29 function rss2atom.description(atom_entry, tag)
30 atom_entry:tag("summary"):text(tag:get_text()):up();
31 end
32
33 local months = {
34 jan = "01", feb = "02", mar = "03", apr = "04", may = "05", jun = "06";
35 jul = "07", aug = "08", sep = "09", oct = "10", nov = "11", dec = "12";
36 };
37
38 function rss2atom.pubDate(atom_entry, tag)
39 local pubdate = tag:get_text():gsub("^%a+,", ""):gsub("^%s*", "");
40 local date, month, year, hour, minute, second, zone =
41 pubdate:match("^(%d%d?) (%a+) (%d+) (%d+):(%d+):?(%d*) ?(.*)$");
42 if not date then return; end
43 if #date == 1 then
44 date = "0"..date;
45 end
46 month = months[month:sub(1,3):lower()];
47 if #year == 2 then -- GAH!
48 if tonumber(year) > 80 then
49 year = "19"..year;
50 else
51 year = "20"..year;
52 end
53 end
54 if zone == "UT" or zone == "GMT" then zone = "Z"; end
55 if #second == 0 then
56 second = "00";
57 end
58 local date_string = string.format("%s-%s-%sT%s:%s:%s%s", year, month, date, hour, minute, second, zone);
59 atom_entry:tag("published"):text(date_string):up();
60 end
61
62 -- Translate a single item to atom
63 local function translate_rss(rss_feed)
64 local feed = st.stanza("feed", { xmlns = "http://www.w3.org/2005/Atom" });
65 local channel = rss_feed:get_child("channel");
66 -- TODO channel properties
67 feed:tag("entry");
68 for item in channel:childtags("item") do
69 for tag in rss_item:childtags() do
70 local translator = rss2atom[tag.name];
71 if translator then
72 translator(feed, tag);
73 end
74 end
75 end
76 feed:reset();
77 return feed;
78 end
79
80 return { translate_rss = translate_rss }