Advertisement
deseven

medianew-sql.lua

Jun 12th, 2011
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.88 KB | None | 0 0
  1. -- send new releases from magneto to PtokaX
  2. -- by deseven, 2010-2011
  3.  
  4. require "luasql.mysql"
  5.  
  6. BotName = "Magneto"
  7. PtokaXCodepage = "cp1251"
  8. BaseLink = "http://magneto.somesite.com/topic.php?type=show&code="
  9. MysqlHost = "localhost"
  10. MysqlUser = "magneto"
  11. MysqlPass = "magnetopwd"
  12. MysqlDB = "magnetodb"
  13. IDFile = "/srv/ptokax/scripts/medianew.id"
  14. CheckMediaTimer = 600000
  15.  
  16. function OnStartup()
  17.     env = assert(luasql.mysql())
  18.    
  19.     -- setting timer
  20.     MediaTimer = TmrMan.AddTimer(CheckMediaTimer,"CheckRel")
  21.    
  22.     -- reading file to get LastRelID after restart (or first start)
  23.     local IDFileR = assert(io.open(IDFile,"r"))
  24.     LastRelID = IDFileR:read("*line")
  25.     IDFileR:close()
  26.     CheckRel()
  27. end
  28.  
  29. CheckRel = function()
  30.     if LastRelID == nil then else
  31.         local Query = "select convert(tbl_kat.NAZV using "..PtokaXCodepage..") as cat,convert(tbl_base.NAZV using "..PtokaXCodepage..") as name,tbl_base.CODE as id from tbl_base join tbl_kat on tbl_kat.CODE = tbl_base.KAT where tbl_base.CODE > "..LastRelID.." order by tbl_base.CODE asc"
  32.         local con = assert(env:connect(MysqlDB,MysqlUser,MysqlPass,MysqlHost))
  33.         local res = assert(con:execute(Query))
  34.         if res == nil then
  35.             Core.SendToOps("<"..BotName.."> medianew: mysql-ошибка при получении новых раздач")
  36.         else
  37.             local row = res:fetch({},"a")
  38.             if row == nil then else
  39.                 Core.SendToAll("<"..BotName.."> Новое в Magneto:")
  40.                 while row do
  41.                     LastRelID = row.id
  42.                     Core.SendToAll("<"..BotName.."> "..row.cat..": "..row.name:gsub("|","&#124;").." ( "..BaseLink..row.id.." )")
  43.                     row = res:fetch(row,"a")
  44.                 end
  45.             end
  46.         end
  47.         res:close()
  48.         con:close()
  49.     end
  50. end
  51.  
  52. function OnExit()
  53.     -- writing LastRelID to file
  54.     if LastRelID == nil then else
  55.         local IDFileW = assert(io.open(IDFile,"w"))
  56.         IDFileW:write(LastRelID)
  57.         IDFileW:close()
  58.     end
  59.     env:close()
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement