1
0
Fork 0
cc-stuff/tapeget.lua

51 lines
1000 B
Lua
Raw Normal View History

2023-10-03 18:19:15 +03:00
local args = { ... }
local tape = peripheral.find("tape_drive")
if not tape then
print("tape where")
return
end
if not http then
print("no http, check config")
return
end
tape.stop()
tape.seek(-tape.getSize())
tape.stop()
2023-10-17 16:03:16 +03:00
local req, err = http.get(args[1], {}, true)
2023-10-03 18:19:15 +03:00
if not req then
2023-10-17 16:03:16 +03:00
print("oopsie: "..err)
2023-10-03 18:19:15 +03:00
return
end
2023-10-03 19:33:02 +03:00
local headers = req.getResponseHeaders()
local length = headers["content-length"]
2023-10-03 19:46:25 +03:00
local function n_to_kib(value)
return string.format("%6.1f kiB", value / 1024)
end
local written = 1
local _, y = term.getCursorPos()
2023-10-03 18:19:15 +03:00
while true do
2023-10-17 16:03:16 +03:00
local chunk = req.read(256)
2023-10-03 18:19:15 +03:00
if not chunk then
print("EOF")
2023-10-03 18:45:22 +03:00
break
2023-10-03 18:19:15 +03:00
end
2023-10-03 18:25:40 +03:00
tape.write(chunk)
2023-10-03 19:46:25 +03:00
written = written + 1
if (written % 8192) == 0 then
2023-10-03 19:33:02 +03:00
os.sleep(0.01)
2023-10-03 19:46:25 +03:00
term.setCursorPos(1, y)
term.write(n_to_kib(written) .. " / " .. n_to_kib(length) .. string.format(" %7.3f%%", 100 * written / length))
2023-10-03 19:33:02 +03:00
end
2023-10-03 18:19:15 +03:00
end
2023-10-03 18:45:22 +03:00
tape.stop()
tape.seek(-tape.getSize())
tape.stop()