Jump to content




Get file from GitHub


  • You cannot reply to this topic
6 replies to this topic

#1 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 06 August 2017 - 02:36 PM

I know that you can get the data of a file from pastebin, but i am working with gitHub and want something like an update checker which downloads the new file if there is an update. I don't know how to use the HTTP API, can someone explain me how it works?

#2 CLNinja

  • Members
  • 191 posts

Posted 06 August 2017 - 02:51 PM

This is the simplest way to get data from a URL. If you replace url with the raw for a github file, it returns the contents of the file.
var = http.get(url).readAll()


#3 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 06 August 2017 - 05:27 PM

If http.get fails, that example will throw an error. You should check if http.get worked before using the result.

Edited by KingofGamesYami, 06 August 2017 - 05:28 PM.


#4 Dave-ee Jones

  • Members
  • 456 posts
  • LocationVan Diemen's Land

Posted 07 August 2017 - 12:07 AM

Github has a raw file feature. Go to your Github project, click on the file you want then click on "Raw" in the file editor. This'll give you a link to a webpage that ONLY contains the contents of the file.

The URL will look something like this:
https://raw.githubus...master/test.txt

#5 Jummit

  • Members
  • 306 posts
  • LocationJulfander Squad Studio

Posted 07 August 2017 - 08:56 AM

So here is the code i would use:
local website = http.get(url)
if website then
   github_file = website.readAll()
end
website.close()

Edited by Jummit, 07 August 2017 - 08:58 AM.


#6 The Crazy Phoenix

  • Members
  • 136 posts
  • LocationProbably within 2 metres of my laptop.

Posted 07 August 2017 - 07:04 PM

View PostJummit, on 07 August 2017 - 08:56 AM, said:

So here is the code i would use:
local website = http.get(url)
if website then
   github_file = website.readAll()
end
website.close()

That would still error if website == nil.

local website = http.get(url)
if website then
    github_file = website.readAll()
    website.close()
end


#7 Nex4rius

  • Members
  • 6 posts

Posted 10 August 2017 - 03:32 PM

I'm using this
local function wget(option, url, ziel)
  if type(url) ~= "string" and type(ziel) ~= "string" then
	return
  elseif type(option) == "string" and option ~= "-f" and type(url) == "string" then
	ziel = url
	url = option
  end
  if http.checkURL(url) then
	if fs.exists(ziel) and option ~= "-f" then
	  printError("<Error> Target exists already")
	  return
	else
	  term.write("Downloading ... ")
	  local timer = os.startTimer(60)
	  http.request(url)
	  while true do
		local event, id, data = os.pullEvent()
		if event == "http_success" then
		  print("success")
		  local f = io.open(ziel, "w")
		  f:write(data.readAll())
		  f:close()
		  data:close()
		  print("Saved as " .. ziel)
		  return true
		elseif event == "timer" and timer == id then
		  printError("<Error> Timeout")
		  return
		elseif event == "http_failure" then
		  printError("<Error> Download")
		  os.cancelAlarm(timer)
		  return
		end
	  end
	end
  else
	printError("<Error> URL")
	return
  end
end
It checks for all possible errors, with a 60s timeout and then saves the file with an overwriting option -f.
In addition it also writes what it does to the screen.
wget("http://example.org/", "testing")
wget("-f", "http://example.org/", "testing")
This will download the website example.org and save it as testing.

Edited by Nex4rius, 10 August 2017 - 03:32 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users