Merge pull request #278 from nmav/ocserv
[project/luci.git] / applications / luci-freifunk-widgets / luasrc / view / freifunk / widgets / rssfeed / main.htm
1 <%
2 --[[
3 LuCI - Lua Configuration Interface
4
5 Copyright 2012 Manuel Munz <freifunk at somakoma dot de>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 ]]--
14
15 local sys = require "luci.sys"
16 local utl = require "luci.util"
17 local fs = require "luci.fs"
18 local i18n = require "luci.i18n"
19 local url = data.url
20 local title = data.title or i18n.translate("RSS")
21 local max = tonumber(data.max) or 10
22 local rss
23 local pr = data.paddingright or "0"
24 local output = {}
25 local width = data.width or "100%"
26 if type(width) == "number" then
27         width = width .. "px"
28 end
29 local name = data['.name']
30 local cachetime = tonumber(data.cache) or 3600
31 cachefile = "/tmp/" .. name .. ".cache"
32 %>
33 <div id="<%=name%>" style="width:<%=width%>;float:left">
34         <div style="padding-right: <%=pr%>">
35         <h2><%=title%></h2>
36
37         <% if not url then %>
38                 <%:No url found in config%>
39         <% else
40                 local mtime = luci.fs.mtime(cachefile) or 0
41                 local now = os.time()
42                 expire = mtime + cachetime
43
44                 if not fs.access(cachefile) or expire < now then
45                         rss = sys.httpget(url)
46                         if #rss == 0 then
47         %>
48                         <%:Could not get rss data from%> <a href="<%=url%>"><%=url%></a>
49         <%
50                         else
51                                 local count = 0
52                                 for item in string.gmatch(rss, "<item>(.-)</item>") do
53                                         if count < max then
54                                                 local title = item:match("<title>(.-)</title>")
55                                                 local link = item:match("<link>(.-)</link>")
56                                                 local desc = item:match("<description>(.-)</description>") or ""
57                                                 if title and link then
58                                                         table.insert(output, { title = utl.pcdata(title), link = utl.pcdata(link) })
59                                                 end
60                                                 count = count + 1
61                                         end
62                                 end
63                                 if count > 0 then
64                                         local file = io.open(cachefile, "w")
65                                         file:write(utl.serialize_data(output))
66                                         file:close()
67                                 end
68                         end
69                 else
70                         local file = assert(io.open(cachefile))
71                         output = utl.restore_data(file:read'*a')
72                 end
73         end
74
75         if #output > 0 then
76         %>
77                 <ul>
78                 <% for k, v in ipairs(output) do %>
79                         <li><a href="<%=v.link%>"><%=v.title%></a></li>
80                 <% end %>
81                 </ul>
82         <%end%>
83         </div>
84 </div>