comparison mod_pubsub_feeds/mod_pubsub_feeds.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 720ff25d94e6
children bd7744df0b4a
comparison
equal deleted inserted replaced
2131:ba42c8882026 2132:b149ea428b81
1 -- Fetches Atom feeds and publishes to PubSub nodes 1 -- Fetches Atom feeds and publishes to PubSub nodes
2 --
3 -- Depends: http://code.matthewwild.co.uk/lua-feeds
4 -- 2 --
5 -- Config: 3 -- Config:
6 -- Component "pubsub.example.com" "pubsub" 4 -- Component "pubsub.example.com" "pubsub"
7 -- modules_enabled = { 5 -- modules_enabled = {
8 -- "pubsub_feeds"; 6 -- "pubsub_feeds";
19 17
20 local date, time = os.date, os.time; 18 local date, time = os.date, os.time;
21 local dt_parse, dt_datetime = require "util.datetime".parse, require "util.datetime".datetime; 19 local dt_parse, dt_datetime = require "util.datetime".parse, require "util.datetime".datetime;
22 local uuid = require "util.uuid".generate; 20 local uuid = require "util.uuid".generate;
23 local hmac_sha1 = require "util.hashes".hmac_sha1; 21 local hmac_sha1 = require "util.hashes".hmac_sha1;
24 local parse_feed = require "feeds".feed_from_string; 22 local parse_xml = require "uit.xml".parse;
25 local st = require "util.stanza"; 23 local st = require "util.stanza";
26 --local dump = require"util.serialization".serialize; 24 local translate_rss = module:require("feeds").translate_rss;
27 25
28 local xmlns_atom = "http://www.w3.org/2005/Atom"; 26 local xmlns_atom = "http://www.w3.org/2005/Atom";
27
28 local function parse_feed(data)
29 local feed, err = parse_xml(data);
30 if not feed then return feed, err; end
31 if feed.attr.xmlns == xmlns_atom then
32 return feed;
33 elseif feed.attr.xmlns == nil and feed.name == "rss" then
34 return translate_rss(feed);
35 end
36 return nil, "unsupported-format";
37 end
29 38
30 local use_pubsubhubub = module:get_option_boolean("use_pubsubhubub", true); 39 local use_pubsubhubub = module:get_option_boolean("use_pubsubhubub", true);
31 if use_pubsubhubub then 40 if use_pubsubhubub then
32 module:depends"http"; 41 module:depends"http";
33 end 42 end
73 82
74 function update_entry(item) 83 function update_entry(item)
75 local node = item.node; 84 local node = item.node;
76 module:log("debug", "parsing %d bytes of data in node %s", #item.data or 0, node) 85 module:log("debug", "parsing %d bytes of data in node %s", #item.data or 0, node)
77 local feed = parse_feed(item.data); 86 local feed = parse_feed(item.data);
78 for _, entry in ipairs(feed) do 87 for entry in feed:childtags("entry") do
79 entry.attr.xmlns = xmlns_atom; 88 entry.attr.xmlns = xmlns_atom;
80 89
81 local e_published = entry:get_child_text("published"); 90 local e_published = entry:get_child_text("published");
82 e_published = e_published and dt_parse(e_published); 91 e_published = e_published and dt_parse(e_published);
83 local e_updated = entry:get_child_text("updated"); 92 local e_updated = entry:get_child_text("updated");
117 item.subscription = nil; 126 item.subscription = nil;
118 item.lease_expires = nil; 127 item.lease_expires = nil;
119 end 128 end
120 if use_pubsubhubub and not item.subscription then 129 if use_pubsubhubub and not item.subscription then
121 --module:log("debug", "check if %s has a hub", item.node); 130 --module:log("debug", "check if %s has a hub", item.node);
122 local hub = item.hub or feed.links and feed.links.hub; 131 for link in feed:childtags("link") do
123 if hub then 132 if link.attr.rel == "hub" then
124 item.hub = hub; 133 item.hub = link.attr.href;
125 module:log("debug", "%s has a hub: %s", item.node, item.hub); 134 module:log("debug", "Node %s has a hub: %s", item.node, item.hub);
126 subscribe(item); 135 return subscribe(item);
136 end
127 end 137 end
128 end 138 end
129 end 139 end
130 140
131 function fetch(item, callback) -- HTTP Pull 141 function fetch(item, callback) -- HTTP Pull