2018-02-13 11:59:23 +08:00
|
|
|
---------------------------- Begin of From Downloader
|
|
|
|
|
2017-10-25 15:32:22 +08:00
|
|
|
local component=require("component")
|
|
|
|
|
2018-02-13 11:59:23 +08:00
|
|
|
local function doRealDownload(url)
|
2018-03-13 08:56:39 +08:00
|
|
|
if(component.internet==nil) then
|
2017-10-25 15:32:22 +08:00
|
|
|
error("The downloader requires an Internet card.")
|
|
|
|
end
|
|
|
|
|
|
|
|
local handle=component.internet.request(url)
|
2018-02-13 11:59:23 +08:00
|
|
|
|
2018-03-13 08:56:39 +08:00
|
|
|
while true do
|
|
|
|
local ret,err=handle.finishConnect()
|
|
|
|
if(ret==nil) then
|
|
|
|
return false,err
|
|
|
|
elseif(ret==true) then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
--os.sleep(0.1)
|
|
|
|
end
|
|
|
|
|
|
|
|
local response_code=handle.response()
|
|
|
|
|
2017-10-25 15:32:22 +08:00
|
|
|
local ans=""
|
|
|
|
while true do
|
|
|
|
local tmp=handle.read()
|
|
|
|
if(tmp==nil) then break end
|
|
|
|
ans=ans .. tmp
|
|
|
|
end
|
|
|
|
handle.close()
|
|
|
|
|
2018-03-13 08:56:39 +08:00
|
|
|
return true,ans,response_code
|
2017-10-25 15:32:22 +08:00
|
|
|
end
|
|
|
|
|
2018-02-13 11:59:23 +08:00
|
|
|
function DownloadFromGitHub(RepoName,Branch,FileAddress)
|
|
|
|
local url="https://raw.githubusercontent.com/" .. RepoName .. "/" .. Branch .. "/" .. FileAddress
|
|
|
|
return doRealDownload(url)
|
|
|
|
end
|
|
|
|
|
2017-10-25 15:32:22 +08:00
|
|
|
function DownloadFromOCS(FileAddress)
|
|
|
|
return DownloadFromGitHub("Kiritow/OpenComputerScripts","master",FileAddress)
|
|
|
|
end
|
|
|
|
|
|
|
|
function WriteStringToFile(StringValue,FileName,IsAppend)
|
|
|
|
if(IsAppend==nil) then IsAppend=false end
|
|
|
|
local handle,err
|
|
|
|
if(IsAppend) then
|
|
|
|
handle,err=io.open(FileName,"a")
|
|
|
|
else
|
|
|
|
handle,err=io.open(FileName,"w")
|
|
|
|
end
|
|
|
|
if(handle==nil) then return false,err end
|
|
|
|
|
|
|
|
handle:write(StringValue)
|
|
|
|
handle:close()
|
|
|
|
|
|
|
|
return true,"Success"
|
|
|
|
end
|
|
|
|
|
2018-02-13 11:59:23 +08:00
|
|
|
----------------------------- End of From Downloader
|
2018-03-13 08:56:39 +08:00
|
|
|
local shell=require("shell")
|
|
|
|
local args=shell.parse(...)
|
|
|
|
local argc=#args
|
2017-10-23 21:27:48 +08:00
|
|
|
|
2018-03-13 08:56:39 +08:00
|
|
|
local file_lst={}
|
|
|
|
|
|
|
|
if(argc<1) then
|
|
|
|
file_lst=
|
2017-10-23 21:27:48 +08:00
|
|
|
{
|
2018-03-13 08:56:39 +08:00
|
|
|
"SignReader.lua",
|
|
|
|
"checkarg.lua",
|
|
|
|
"class.lua",
|
|
|
|
"downloader.lua",
|
|
|
|
"libevent.lua",
|
|
|
|
"queue.lua",
|
|
|
|
"util.lua",
|
|
|
|
"vector.lua",
|
|
|
|
"LICENSE"
|
2017-10-23 21:27:48 +08:00
|
|
|
}
|
2018-03-13 08:56:39 +08:00
|
|
|
else
|
|
|
|
for i=1,argc,1 do
|
|
|
|
table.insert(file_lst,args[i])
|
|
|
|
end
|
|
|
|
end
|
2017-10-23 21:27:48 +08:00
|
|
|
|
|
|
|
local cnt_all=0
|
2018-03-13 08:56:39 +08:00
|
|
|
for k,v in pairs(file_lst) do
|
2017-10-23 21:27:48 +08:00
|
|
|
cnt_all=cnt_all+1
|
|
|
|
end
|
|
|
|
|
|
|
|
local cnt_now=1
|
|
|
|
|
|
|
|
-- Download from the list
|
2018-06-19 17:08:34 +08:00
|
|
|
for k,v in pairs(file_lst) do
|
2018-03-13 08:56:39 +08:00
|
|
|
io.write("Updating (" .. cnt_now .. "/" .. cnt_all .. "): " .. v .. " ")
|
|
|
|
local flag,x,y=DownloadFromOCS(v)
|
|
|
|
if((not flag) or (y~=200) ) then
|
|
|
|
print("[Download Failed]")
|
2018-06-19 17:08:34 +08:00
|
|
|
-- Stop if download failed.
|
|
|
|
break
|
2018-03-13 08:56:39 +08:00
|
|
|
else
|
|
|
|
local ret=WriteStringToFile(x,v)
|
|
|
|
if(not ret) then
|
|
|
|
print("[Write Failed]")
|
|
|
|
else
|
|
|
|
print("[OK]")
|
|
|
|
end
|
|
|
|
cnt_now = cnt_now + 1
|
|
|
|
end
|
2017-10-23 21:27:48 +08:00
|
|
|
end
|