From 42269dcb99018075e30caaf41b1e9ca3abad95fd Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Mon, 28 Sep 2009 20:39:49 +0200 Subject: [PATCH] contrib: Imported the rss widget, by olcc It is a bit different from the overall vicious design, for now it is in the contrib branch. We just gained some exposure and I expect more people to send me stuff like this. It is still unclear weather vicious will depend on third party libs (in which case we take an XML parser). If not, maybe we will have a very basic html/xml parser as a helper, or maybe neither and this widget will be introduced to master (with a few more improvements I hope). It is too early to say, we just got some users. --- rss.lua | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 rss.lua diff --git a/rss.lua b/rss.lua new file mode 100644 index 0000000..6fe9e95 --- /dev/null +++ b/rss.lua @@ -0,0 +1,64 @@ +---------------------------------------------------------- +-- Licensed under the GNU General Public License version 2 +-- * Copyright (C) 2009 olcc +---------------------------------------------------------- + +-- {{{ Grab environment +local pairs = pairs +local io = { popen = io.popen } +local setmetatable = setmetatable +-- }}} + + +-- RSS: provides latest world news +module("vicious.rss") + + +-- {{{ RSS widget type +local function worker(format, input) + -- input: * feed - feed url + -- * object - entity to look for (typically: 'item') + -- * fields - fields to read (example: 'link', 'title', 'description') + -- output: * count - number of entities found + -- * one table for each field, containing wanted values + local feed = input.feed + local object = input.object + local fields = input.fields + + -- Initialise tables + local out = {} + + for _, v in pairs(fields) do + out[v] = {} + end + + -- Initialise variables + local ob = nil + local i,j,k = 1, 1, 0 + local curl = "curl -A 'Mozilla/4.0' -fsm 5 --connect-timeout 3 " + + -- Get the feed + local f = io.popen(curl .. '"' .. feed .. '"') + local feed = f:read("*all") + f:close() + + while true do + i, j, ob = feed.find(feed, "<" .. object .. ">(.-)", i) + if not ob then break end + + for _, v in pairs(fields) do + out[v][k] = ob:match("<" .. v .. ">(.*)") + end + + k = k+1 + i = j+1 + end + + -- Update the entity count + out.count = k + + return out +end +-- }}} + +setmetatable(_M, { __call = function(_, ...) return worker(...) end })