This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
BabyCodder's profile picture

[LUA] Downloading files with HTTP

Started by BabyCodder, 04 April 2013 - 04:44 AM
BabyCodder #1
Posted 04 April 2013 - 06:44 AM
Hi, I am creating a OS but the way I would like to do it is have a startup that downloads separate files to build a OS computer instead of one program. I have made the startup program below but it does not write the files.

Spoiler

home = fs.open("home", "w")
login = fs.open("login", "w")
new = fs.open("new", "w")
function getn()
    http.request("http://pastebin.com/raw.php?i=7Ksbviwh")
    requ = true
	    while requ do
		    local event, url, scourceText = os.pullEvent()
		    if event == "http_success" then
			    newf = scourceText.readAll()
			    new.write(newf)
			    print("Downloaded New")
			    getl()
		    else
			    print("Failed To Download New")
			    sleep(2)
			    os.reboot()
		    end
	    end
end
function getl()
    http.request("http://pastebin.com/raw.php?i=cs5C13Hg")
    requ = true
	    while requ do
		    local event, url, scourceText = os.pullEvent()
		    if event == "http_success" then
			    loginf = scourceText.readAll()
			    login.write(loginf)
			    print("Downloaded Login")
			    geth()
		    else
			    print("Failed To Download New")
			    sleep(2)
			    os.reboot()
		    end
	    end
end
function geth()
    http.request("http://pastebin.com/raw.php?i=FUB6hDDB")
    requ = true
	    while requ do
		    local event, url, scourceText = os.pullEvent()
		    if event == "http_success" then
			    homef = scourceText.readAll()
			    home.write(homef)
			    print("Downloaded Home")
			    shell.run("new")
		    else
			    print("Failed To Download Home")
			    sleep(2)
			    os.reboot()
		    end
	    end
end
function is()
    if not fs.isDir("user_stats") then
	    getn()
	    shell.run("new")
    else
	    shell.run("login")
    end
end
is()
Telokis #2
Posted 04 April 2013 - 06:48 AM
Why don't you use the pastebin program ?

Take a look at /rom/programs/http/pastebin ! ;)/>
BabyCodder #3
Posted 04 April 2013 - 06:49 AM
Why don't you use the pastebin program ?
I don't want the people to have to pastebin multiple programs. I want them to pastebin one and that gets the rest.
Telokis #4
Posted 04 April 2013 - 06:50 AM
You want your program to install files you already posted on pastebin isn't it ?
BabyCodder #5
Posted 04 April 2013 - 06:51 AM
You want your program to install files you already posted on pastebin isn't it ?
Yes.
Telokis #6
Posted 04 April 2013 - 06:53 AM
I think you can try something like that :


shell.run("pastebin", "get", "7Ksbviwh", "new")

It is an example. This will run the pastebin program but from your own program. User only launch the program you wrote in first post.
Sammich Lord #7
Posted 04 April 2013 - 06:54 AM
Here is a basic script that should work:

function download(url, file)
  local content = http.get(url).readAll()
  if not content then
    error("Could not connect to website")
  end
  f = fs.open(file, "w")
  f.write(content)
  f.close()
end
That should work. Wrote it in under a minute.
theoriginalbit #8
Posted 04 April 2013 - 06:56 AM
Here is a basic script that should work:

function download(url, file)
  local content = http.get(url).readAll()
  if not content then
	error("Could not connect to website")
  end
  f = fs.open(file, "w")
  f.write(content)
  f.close()
end
That should work. Wrote it in under a minute.
*cough* forgetting to close the http file handle, and no localized file handle *cough*
PixelToast #9
Posted 04 April 2013 - 07:03 AM
you dont need to close the http handle, its not a real file handle