G2A Many GEOs
SHARE
TWEET

dld

kssr3951 May 1st, 2018 (edited) 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- dld
  2. -- Simple download program for Computercraft
  3. -- ============================================
  4. -- == config
  5. -- ============================================
  6. URL = "http://zzz.yyy.example.com/ulddld.php"
  7. PASSWORD = "xxxxxx"
  8. -- ============================================
  9. -- == utilities
  10. -- ============================================
  11. local function fileOverwrite(fileName, text)
  12.   local hFile = fs.open(fileName, "w")
  13.   hFile.writeLine(text)
  14.   hFile.close()
  15. end
  16. local function makePostText(tbl)
  17.   local rslt = ""
  18.   for key, val in pairs(tbl) do
  19.     if ("" ~= rslt) then
  20.       rslt = rslt .. "&"
  21.     end
  22.     rslt = rslt ..
  23.       textutils.urlEncode(tostring(key)) ..
  24.       "=" ..
  25.       textutils.urlEncode(tostring(val))
  26.   end
  27.   return rslt
  28. end
  29. local function httpPost(url, data)
  30.   local file = http.post(url, makePostText(data))
  31.   local rslt
  32.   if file ~= nil then
  33.     rslt = file.readAll()
  34.     file.close()
  35.   else
  36.     rslt = ""
  37.   end
  38.   return rslt
  39. end
  40. local function getOneLine(src, start)
  41.   local nl = string.find(src, "\n", start)
  42.   if nil == nl then
  43.     return string.sub(src, start), nil
  44.   else
  45.     local next_pos = nl + 1
  46.     if string.len(src) < next_pos then
  47.       next_pos = nil
  48.           end
  49.           return string.sub(src, start, nl - 1), next_pos
  50.   end
  51. end
  52. -- ============================================
  53. -- == main
  54. -- ============================================
  55. data = {}
  56. data.command = "dld"
  57. data.password = PASSWORD
  58. response = httpPost(URL, data)
  59. -- line 1 : OK or NG
  60. -- line 2 : when OK -> file name
  61. --          when NG -> error message
  62. -- line 3 : when OK -> file data
  63. line1, nxt = getOneLine(response, 1)
  64. line2, nxt = getOneLine(response, nxt)
  65. if ("NG" == line1) then
  66.   print(line2)
  67.   return
  68. elseif ("OK" == line1) then
  69.   fileOverwrite(line2, string.sub(response, nxt))
  70.   print("downloaded " .. line2)
  71. end
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top