PtokaX forum

Archive => Archived 5.0 boards => Finished Scripts => Topic started by: Madman on 16 April, 2006, 21:53:17

Title: ScriptUpdater
Post by: Madman on 16 April, 2006, 21:53:17

-- ScriptUpdater
-- Made by Madman, 16.04.06

--[[--
Very much based on WebBot 1.0 by Mutor

Thanks to jiten and Hungarista for the help =)

A script to use for updateing files in the script folder

Using bluebears httpget-beta1 extension lib for PtokaX.
Get the pre-released beta here:
http://www.thewildplace.dk/downloads/betas/httpget-beta1.zip
--]]--

GetCfg = {
Interval = 500,
Bot = "ScriptUpdater",
Done = 0,
Count = 0,
Time = os.time(),
}


function Main()
libinit = loadlib("luahttpget.dll", "_libinit")
libinit()
end

function ChatArrival(curUser, data)
local data = string.sub(data, 1, -2)
local s,e,cmd = string.find(data, "%b<>%s+[%!%+%#%?](%S+)")
if cmd and curUser.bOperator then
if cmd == "updatescript" then
s,e,Url,WriteFile = string.find(data, "%b<>%s+%S+%s+(%S+)%s+(%S+)")
if Url and WriteFile then
if string.find(Url, "http://") then
AddDL(Url,WriteFile)
SetTimer(GetCfg.Interval)
StartTimer()
else
curUser:SendData(GetCfg.Bot, "Syntax: !updatescript <domain><file> <target> (i.e !updatescript http://www.mydomain.com/Myfile.txt FileOnHardDrive.txt)")
end
else
curUser:SendData(GetCfg.Bot, "Syntax: !updatescript <domain><file> <target> (i.e !updatescript http://www.mydomain.com/Myfile.txt FileOnHardDrive.txt)")
end
return 1
end
end
end

function GetStats(uri)
local Tab = {
[0] = "Pending",
[1] = "Downloading",
[2] = "Download Complete",
[3] = "Download Failed",
}
if uri then
local r,s = HttpDlStats(uri)," = "..uri
r = tonumber(r)
if Tab[r] then
s = Tab[r]..s
else
s = s.." is not in list!"
end
return s,r
end
end

function OnTimer()
if Url ~= "" and WriteFile ~= "" then
local status,code = GetStats(Url)
local file = WriteFile
if code < 2 then
if GetCfg.Count == 0 then
SendToOps(GetCfg.Bot, status)
GetCfg.Count = 1
end
end
if code >= 2 then
StopTimer()
if code == 2 then
local hub,ver = getHubVersion()
local size,kb = Checkfile(file)
kb = string.format ("%-5.2f Kb/s", kb / (os.time() - GetCfg.Time))
local stat = "\r\n\r\n\t"..status.."\r\n"..
"\tDownload Size:\t"..size..
"\r\n\tDownload Time:\t"..
(os.time() - GetCfg.Time).." seconds\r\n"..
"\tTransfer Speed:\t"..kb.." \r\n"..
"\tPtokaX Version:\t"..hub.." "..ver.."\r\n"..
"\tPXLua Version:\t".._VERSION.."\r\n\r\n"
SendToOps(GetCfg.Bot,stat)
end
if GetCfg.Done == 0 then
GetCfg.Done = 1
elseif GetCfg.Done == 1 then
GetCfg.Done = 0
Url,WriteFile = nil,nil
end
end
end
end

function Checkfile(file)
local f,e = io.open(file,"r")
if f then
local current = f:seek()
local size = f:seek("end")
local kb = string.format ("%-5.2f", size / 1024)
kb = tonumber(kb)
f:seek("set", current)
f:close()
if size > 1048576 then
size = string.format ("%-5.2fMb", size / 1048576)
elseif size > 1024 and size < 1048576 then
size = string.format ("%-5.2f Kb", size / 1024)
else
size = string.format ("%-4d Bytes", size)
end
return size,kb
else
return nil
end
end

function AddDL(Url,target)
if Url and target then
local res = HttpDlAdd(Url, target)
if res then
SendToOps(GetCfg.Bot, " Not added")
else
SendToOps(GetCfg.Bot, Url..", Has Been added to download list!")
GetCfg.Time = os.time()
GetCfg.Count = 0
end
SendToOps(GetCfg.Bot, GetStats(Url))
end
end


Made so i could update a freinds hub with new versions of a script easier...
The script can be used to download any file from any site to the script folder...
Title: Re: ScriptUpdater
Post by: Thor on 16 April, 2006, 22:22:03
Nice work! ;) Maybe a frmHub:RestartScripts() would be nice after update.
Title: Re: ScriptUpdater
Post by: Madman on 16 April, 2006, 22:26:44
Maybe...
But sometimes you dl more then 1 script...
So i thought it would be better to restart manually...