main.htm 2.1 KB

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