Debian Codegraph @

vicious/contrib/rss_all.lua

raw symbols SCIP
1 -- contrib/rss_all.lua
2 -- Copyright (C) 2009 olcc
3 -- Copyright (C) 2017 Jörg Thalheim <[email protected]>
4 --
5 -- This file is part of Vicious.
6 --
7 -- Vicious is free software: you can redistribute it and/or modify
8 -- it under the terms of the GNU General Public License as
9 -- published by the Free Software Foundation, either version 2 of the
10 -- License, or (at your option) any later version.
11 --
12 -- Vicious is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 -- GNU General Public License for more details.
16 --
17 -- You should have received a copy of the GNU General Public License
18 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
19
20 ---------------------------------------------------
21 -- This is now a standalone RSS reader for awesome:
22 -- * http://github.com/olcc/aware
23 ---------------------------------------------------
24
25 -- {{{ Grab environment
26 local pairs = pairs
27 local io = { popen = io.popen }
28 local setmetatable = setmetatable
29 -- }}}
30
31
32 -- RSS: provides latest world news
33 -- vicious.contrib.rss
34 local rss_all = {}
35
36
37 -- {{{ RSS widget type
38 local function worker(format, input)
39 -- input: * feed - feed url
40 -- * object - entity to look for (typically: 'item')
41 -- * fields - fields to read (example: 'link', 'title', 'description')
42 -- output: * count - number of entities found
43 -- * one table for each field, containing wanted values
44 local feed = input.feed
45 local object = input.object
46 local fields = input.fields
47
48 -- Initialise tables
49 local out = {}
50
51 for _, v in pairs(fields) do
52 out[v] = {}
53 end
54
55 -- Initialise variables
56 local ob = nil
57 local i,j,k = 1, 1, 0
58 local curl = "curl -A 'Mozilla/4.0' -fsm 5 --connect-timeout 3 "
59
60 -- Get the feed
61 local f = io.popen(curl .. '"' .. feed .. '"')
62 local feed = f:read("*all")
63 f:close()
64
65 while true do
66 i, j, ob = feed.find(feed, "<" .. object .. ">(.-)</" .. object .. ">", i)
67 if not ob then break end
68
69 for _, v in pairs(fields) do
70 out[v][k] = ob:match("<" .. v .. ">(.*)</" .. v .. ">")
71 end
72
73 k = k+1
74 i = j+1
75 end
76
77 -- Update the entity count
78 out.count = k
79
80 return out
81 end
82 -- }}}
83
84 return setmetatable(rss_all, { __call = function(_, ...) return worker(...) end })