-- ocv_get.lua — скачивает видео OCV5 с HTTP-сервера на диск OpenComputers. -- -- Использование: ocv_get [локальная_папка] -- Пример: ocv_get http://192.168.1.103/video/ video local component = require("component") local computer = require("computer") local fs = require("filesystem") local shell = require("shell") local term = require("term") if not component.isAvailable("internet") then io.stderr:write("ocv_get: нужна интернет-карта\n") return end local internet = require("internet") local args = shell.parse(...) local base, dir = args[1], args[2] or "video" if not base then print("usage: ocv_get [folder]") return end if base:sub(-1) ~= "/" then base = base .. "/" end dir = shell.resolve(dir) local function human(n) if n >= 1048576 then return string.format("%.2f MB", n / 1048576) end if n >= 1024 then return string.format("%.1f KB", n / 1024) end return n .. " B" end local function status(text) local _, y = term.getCursor() term.setCursor(1, y) term.clearLine() io.write(text) end local function download(name, label) local path = fs.concat(dir, name) local file = assert(io.open(path, "wb")) local size, t0, lastDraw = 0, computer.uptime(), 0 local ok, err = pcall(function() for chunk in internet.request(base .. name) do file:write(chunk) size = size + #chunk local now = computer.uptime() if now - lastDraw >= 0.25 then lastDraw = now status(label .. " " .. human(size)) end end end) file:close() if not ok or size == 0 then fs.remove(path) error(name .. ": " .. tostring(err or "пустой ответ"), 0) end status(string.format("%s %s %.1fs\n", label, human(size), computer.uptime() - t0)) end local ok, err = pcall(function() fs.makeDirectory(dir) download("index.ocv", "index.ocv") local f = assert(io.open(fs.concat(dir, "index.ocv"), "rb")) local h = f:read(64) f:close() if not h or #h < 64 or h:sub(1, 4) ~= "OCV5" then error("index.ocv: не OCV5", 0) end local parts = h:byte(15) * 256 + h:byte(16) for i = 1, parts do download(string.format("part%04d.ocv", i), string.format("[%d/%d]", i, parts)) end print("Готово: ocv " .. dir) end) if not ok then io.stderr:write("\nocv_get: " .. tostring(err) .. "\n") end