Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local request
- local data
- local speed = 1
- local args = { ... }
- local tape = peripheral.find("tape_drive")
- if not tape then
- print("This program requires a tape drive to run.")
- return
- end
- local function helpText()
- print("Usage:")
- print(" - 'yt-tape' to display this message")
- print(" - 'yt-tape [youtube link]'")
- print(" - 'yt-tape [youtube link] [1 or 2]' to increase sample rate from 48k to 96k")
- end
- local function writeTape(data)
- tape.stop()
- tape.seek(-tape.getSize())
- local filesize = #data
- print("Filesize: ", filesize)
- if filesize > tape.getSize() then
- printError("Error: File too large, truncating")
- filesize = tape.getSize()
- end
- print("Writing...")
- local block = 8192 -- How much to read at a time
- local bytery = 0 -- For the progress indicator
- local _, y = term.getCursorPos()
- repeat
- local bytes = {}
- for i=1, block do
- local byte = string.byte(data, i + bytery)
- if not byte then break end
- bytes[#bytes + 1] = byte
- end
- if #bytes > 0 then
- if not tape.isReady() then
- io.stderr:write("\nError: Tape was removed during writing.\n")
- return
- end
- term.setCursorPos(1, y)
- bytery = bytery + #bytes
- term.write("Wrote " .. tostring(math.min(bytery, filesize)) .. " of " .. tostring(filesize) .. " bytes...")
- for i=1, #bytes do
- tape.write(bytes[i])
- end
- sleep(0)
- end
- until #bytes <= 0 or bytery >= filesize
- tape.stop()
- tape.seek(-tape.getSize())
- tape.setSpeed(speed)
- print("\nDone.")
- end
- if args[1] == nil then
- helpText()
- else
- local url = "https://sus-server.eu/yt-tape?yt-link=" .. args[1]
- local arg2 = args[2]
- if arg2 ~= nil then
- local speedValue = tonumber(arg2)
- if speedValue then
- url = url .. "&qual=" .. arg2
- speed = speedValue
- else
- print(arg2 .. " is not a valid quality. Using default quality.")
- url = url .. "&qual=1"
- end
- else
- url = url .. "&qual=1"
- end
- print("Converting to dfpwm... please wait")
- request = http.get(url, nil, true)
- if not request then
- printError("Failed to get data from the server.")
- return
- end
- data = request.readAll()
- request.close()
- if not data then
- printError("Failed to read data.")
- return
- end
- writeTape(data)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement