annotate mod_pubsub_feeds/feeds.lib.lua @ 2463:e2d955f6167f

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