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

how do i hide a pastebin download?

Started by goldensecret, 17 March 2014 - 01:53 PM
goldensecret #1
Posted 17 March 2014 - 02:53 PM
yo, i am having troubble with my standard turtle startup method i always use, it looks ugly… i want to be able to hid the pastebin get that it does, i display a 'downloaded file' in the code, but i think the standard one just looks ugly, is there a way to get rid of that? heres a screen shot of what it looks like as of current, and the code.

http://pastebin.com/sdmtFLrF
Edited on 17 March 2014 - 01:55 PM
CometWolf #2
Posted 17 March 2014 - 03:00 PM
You could be super lazy and just do

parallel.waitForAll(
  function()
	shell.run"pastebin get whatever derpderp"
  end,
  term.clear
)
However, that's quite a retarded and stupid way of doing it :P/>

The proper way, would require you to setup your own pastebin program. It's quite simple, as you can just copy the parts you need from the pastebin program itself really.
Spoiler

local function printUsage()
	print( "Usages:" )
	print( "pastebin put <filename>" )
	print( "pastebin get <code> <filename>" )
	print( "pastebin run <code> <arguments>" )
end

local tArgs = { ... }
if #tArgs < 2 then
	printUsage()
	return
end

if not http then
	printError( "Pastebin requires http API" )
	printError( "Set enableAPI_http to true in ComputerCraft.cfg" )
	return
end

local function get(paste)
	write( "Connecting to pastebin.com... " )
	local response = http.get(
		"http://pastebin.com/raw.php?i="..textutils.urlEncode( paste )
	)
	  
	if response then
		print( "Success." )
	  
		local sResponse = response.readAll()
		response.close()
		return sResponse
	else
		printError( "Failed." )
	end
end

local sCommand = tArgs[1]
if sCommand == "put" then
	-- Upload a file to pastebin.com
	-- Determine file to upload
	local sFile = tArgs[2]
	local sPath = shell.resolve( sFile )
	if not fs.exists( sPath ) or fs.isDir( sPath ) then
		print( "No such file" )
		return
	end
  
	-- Read in the file
	local sName = fs.getName( sPath )
	local file = fs.open( sPath, "r" )
	local sText = file.readAll()
	file.close()
  
	-- POST the contents to pastebin
	write( "Connecting to pastebin.com... " )
	local key = "0ec2eb25b6166c0c27a394ae118ad829"
	local response = http.post(
		"http://pastebin.com/api/api_post.php",
		"api_option=paste&amp;"..
		"api_dev_key="..key.."&amp;"..
		"api_paste_format=lua&amp;"..
		"api_paste_name="..textutils.urlEncode(sName).."&amp;"..
		"api_paste_code="..textutils.urlEncode(sText)
	)
	  
	if response then
		print( "Success." )
	  
		local sResponse = response.readAll()
		response.close()
			  
		local sCode = string.match( sResponse, "[^/]+$" )
		print( "Uploaded as "..sResponse )
		print( "Run \"pastebin get "..sCode.."\" to download anywhere" )

	else
		print( "Failed." )
	end
  
elseif sCommand == "get" then
	-- Download a file from pastebin.com
	if #tArgs < 3 then
		printUsage()
		return
	end

	-- Determine file to download
	local sCode = tArgs[2]
	local sFile = tArgs[3]
	local sPath = shell.resolve( sFile )
	if fs.exists( sPath ) then
		print( "File already exists" )
		return
	end
  
	-- GET the contents from pastebin
	local res = get(sCode)
	if res then	  
		local file = fs.open( sPath, "w" )
		file.write( res )
		file.close()
	  
		print( "Downloaded as "..sFile )
	end
elseif sCommand == "run" then
	local sCode = tArgs[2]

	local res = get(sCode)
	if res then
		local func, err = loadstring(res)
		if not func then
			printError( err )
			return
		end
		setfenv(func, getfenv())
		local success, msg = pcall(func, unpack(tArgs, 3))
		if not success then
			printError( msg )
		end
	end
else
	printUsage()
	return
end
Edited on 17 March 2014 - 02:00 PM
TheOddByte #3
Posted 17 March 2014 - 03:11 PM
You could just create your own pastebin function

--[[
	@description	"Downloads a file from pastebin"


@param	name,  string
@param	code,  string

@return   nil
--]]
function pastebin_get( name, code )
local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(code))
if response then
   local file = fs.open(name,"w")
   local sCode = response.readAll()
   response.close()
   file.write(sCode)
   file.close()
else
   error("Error getting the file " .. name, 0)
end
end
And use it like this

print( "I'm downloading a file now! :D/>/>")
pastebin_get( "<Input filename>", "<Input pastebin code>" )
print( "Yay! Downloaded <Your file>! :D/>/>" )
Edited on 17 March 2014 - 02:12 PM
Lyqyd #4
Posted 17 March 2014 - 04:52 PM

parallel.waitForAll(
  function()
	shell.run"pastebin get whatever derpderp"
  end,
  term.clear
)

This would clear anything printed before the download starts, but would leave intact the success/failure message.
CometWolf #5
Posted 17 March 2014 - 05:01 PM
Guess he could throw in a clear afterwards aswell, and or maybe use clearLine instead. I didn't put much thought into that idea, cause like i said, it's a stupid one.
TheOddByte #6
Posted 17 March 2014 - 06:32 PM
Guess he could throw in a clear afterwards aswell, and or maybe use clearLine instead. I didn't put much thought into that idea, cause like i said, it's a stupid one.
If it's stupid then why did you post it? It could be a problem if the OP decided to go with that( But I'm pretty sure he won't ) :P/>
goldensecret #7
Posted 18 March 2014 - 01:53 PM
You could just create your own pastebin function

--[[
	@description	"Downloads a file from pastebin"


@param	name,  string
@param	code,  string

@return   nil
--]]
function pastebin_get( name, code )
local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(code))
if response then
   local file = fs.open(name,"w")
   local sCode = response.readAll()
   response.close()
   file.write(sCode)
   file.close()
else
   error("Error getting the file " .. name, 0)
end
end
And use it like this

print( "I'm downloading a file now! :D/>/>/>")
pastebin_get( "<Input filename>", "<Input pastebin code>" )
print( "Yay! Downloaded <Your file>! :D/>/>/>" )

sweet, this looks like the one ill use. now will the top code be in the install, or will i have to have it a separate file? im so confused. i have just started coding, so please eli5 ;)/>
Anavrins #8
Posted 18 March 2014 - 02:32 PM
Simply http.get() from pastebin's raw pages
Example:
local content = http.get("http://pastebin.com/raw.php?i=PaStEiD").readAll()
Edited on 24 March 2014 - 06:55 PM
TheOddByte #9
Posted 18 March 2014 - 04:40 PM
sweet, this looks like the one ill use. now will the top code be in the install, or will i have to have it a separate file? im so confused. i have just started coding, so please eli5 ;)/>
No it doesn't need to be in a separate file, If you would do that you would have to load it with os.loadAPI

--[[
    Let's say you've put the code in a file called
    potatoes, Then you would have to use it like
    this
--]]
os.loadAPI( "potatoes" ) -- Loading the API
potatoes.pastebin_get( "<Filename>", "<Code>" )
I mentioned above that it was a function, If you're unsure what that is I suggest you go and check out this site http://www.lua.org/pil/contents.html#P1 and learn what you need to learn
or you can just google it, Google is your best friend ;)/>
The code that I posted checks if the website is responding and if it isn't it will error with the error I inputted( Error getting the file <Filename> )
goldensecret #10
Posted 24 March 2014 - 06:05 PM
brilliant! worked a treat. thank you!