vax

Mudlet Iron Realms Live Feed Script

vax
May 15th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.37 KB | None | 0 0
  1. local url = "http://www.imperian.com/sites/all/modules/gamefeed/gamefeed-update.php"
  2.  
  3. if feed == nil then
  4.     feed = {
  5.         person = "0", -- set this to your person number
  6.         game = "imp", -- set this to your game (imp, aet, etc)
  7.         since = nil
  8.     }
  9. end
  10.  
  11. function feed:downloadFile()
  12.     if feed.since ~= nil then
  13.         url = "http://www.imperian.com/sites/all/modules/gamefeed/gamefeed-update.php"
  14.         url = url .. "?since="..feed.since.."&game="..feed.game.."&person="..feed.person
  15.     end
  16.     --echo("URL:"..url.."\n")
  17.  
  18.     downloadFile(getMudletHomeDir().."/feed.json", url)
  19. end
  20.  
  21. local function get_timezone_offset(ts)
  22.     local utcdate   = os.date("!*t", ts)
  23.     local localdate = os.date("*t", ts)
  24.     --localdate.isdst = false -- this is the trick
  25.     return os.difftime(os.time(localdate), os.time(utcdate))
  26. end
  27.  
  28. local function spairs(t, order)
  29.     -- collect the keys
  30.     local keys = {}
  31.     for k in pairs(t) do keys[#keys+1] = k end
  32.  
  33.     -- if order function given, sort by it by passing the table and keys a, b,
  34.     -- otherwise just sort the keys
  35.     if order then
  36.         table.sort(keys, function(a,b) return order(t, a, b) end)
  37.     else
  38.         table.sort(keys)
  39.     end
  40.  
  41.     -- return the iterator function
  42.     local i = 0
  43.     return function()
  44.         i = i + 1
  45.         if keys[i] then
  46.             return keys[i], t[keys[i]]
  47.         end
  48.     end
  49. end
  50.  
  51. function DataFeed(_, filename)
  52.     if not filename:match("feed.json", 1, true) then return end
  53.     local file = io.open(filename)
  54.     local s = file:read("*all")
  55.     file:close()
  56.     os.remove(filename)
  57.  
  58.     local contents = yajl.to_value(s)
  59.     local k, v
  60.     local beginID
  61.     if not feed.since then beginID = 0 else beginID = tonumber(feed.since) end
  62.  
  63.     for k,v in spairs(contents) do
  64.         if tostring(v.gameid) == feed.game and tonumber(v.id) > beginID then
  65.             local luaDate = datetime:parse(tostring(v.date),"%Y-%m-%d %H:%M:%S",true)
  66.             luaDate = luaDate + get_timezone_offset(luaDate)
  67.  
  68.             cecho("<dark_slate_gray>[<white>" .. os.date("%Y-%m-%d %H:%M:%S",luaDate) .. "<dark_slate_gray>]<reset>\n")
  69.             echo(tostring(v.description) .. "\n")
  70.            
  71.         end
  72.         if feed.since ~= nil then
  73.             if tonumber(v.id) > tonumber(feed.since) then
  74.                 feed.since = v.id
  75.             end
  76.         else
  77.             feed.since = v.id
  78.         end
  79.     end
  80.     contents = nil
  81.     tempTimer(60,[[feed:downloadFile()]])
  82.  
  83. end
  84.  
  85. registerAnonymousEventHandler("sysDownloadDone", "DataFeed")
  86. feed:downloadFile()
Add Comment
Please, Sign In to add comment