applications: add freifunk widgets app to customize the index page
[project/luci.git] / applications / luci-freifunk-widgets / luasrc / view / freifunk / widgets / rssfeed / main.htm
1 <%
2 local sys = require "luci.sys"
3 local utl = require "luci.util"
4 local fs = require "luci.fs"
5 local i18n = require "luci.i18n"
6 local url = data.url
7 local title = data.title or i18n.translate("RSS")
8 local max = tonumber(data.max) or 10
9 local rss
10 local pr = data.paddingright or "0"
11 local output = {}
12 local width = data.width or "100%"
13 if type(width) == "number" then
14 width = width .. "px"
15 end
16 local cachetime = tonumber(data.cache) or 3600
17 cachefile = "/tmp/" .. name .. ".cache"
18 %>
19 <div id="<%=name%>" style="width:<%=width%>;float:left">
20 <div style="padding-right: <%=pr%>">
21 <h2><%=title%></h2>
22
23 <% if not url then %>
24 <%:No url found in config%>
25 <% else
26 local mtime = luci.fs.mtime(cachefile) or 0
27 local now = os.time()
28 expire = mtime + cachetime
29
30 if not fs.access(cachefile) or expire < now then
31 rss = sys.httpget(url)
32 if #rss == 0 then
33 %>
34 <%:Could not get rss data from%> <a href="<%=url%>"><%=url%></a>
35 <%
36 else
37 local count = 0
38 for item in string.gmatch(rss, "<item>(.-)</item>") do
39 if count < max then
40 local title = item:match("<title>(.-)</title>")
41 local link = item:match("<link>(.-)</link>")
42 local desc = item:match("<description>(.-)</description>") or ""
43 if title and link then
44 table.insert(output, { title = utl.pcdata(title), link = utl.pcdata(link) })
45 end
46 count = count + 1
47 end
48 end
49 local file = io.open(cachefile, "w")
50 file:write(utl.serialize_data(output))
51 file:close()
52 end
53 else
54 local file = assert(io.open(cachefile))
55 output = utl.restore_data(file:read'*a')
56 end
57 end
58
59 if #output > 0 then
60 %>
61 <ul>
62 <%
63 for k, v in ipairs(output) do
64 %>
65 <li><a href="<%=v.link%>"><%=v.title%></a></li>
66 <% end %>
67 </ul>
68 </div>
69 </div>
70 <%end%>