Advertisement
xX-AAAAAAAAAA-Xx

Untitled

Sep 1st, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. local request
  2. local data
  3. local speed = 1
  4.  
  5. local args = { ... }
  6.  
  7. local tape = peripheral.find("tape_drive")
  8. if not tape then
  9. print("This program requires a tape drive to run.")
  10. return
  11. end
  12.  
  13. local function helpText()
  14. print("Usage:")
  15. print(" - 'yt-tape' to display this message")
  16. print(" - 'yt-tape [youtube link]'")
  17. print(" - 'yt-tape [youtube link] [1 or 2]' to increase sample rate from 48k to 96k")
  18. end
  19.  
  20. local function writeTape(data)
  21. tape.stop()
  22. tape.seek(-tape.getSize())
  23.  
  24. local filesize = #data
  25. print("Filesize: ", filesize)
  26.  
  27. if filesize > tape.getSize() then
  28. printError("Error: File too large, truncating")
  29. filesize = tape.getSize()
  30. end
  31.  
  32. print("Writing...")
  33.  
  34. local block = 8192 -- How much to read at a time
  35. local bytery = 0 -- For the progress indicator
  36. local _, y = term.getCursorPos()
  37.  
  38. repeat
  39. local bytes = {}
  40. for i=1, block do
  41. local byte = string.byte(data, i + bytery)
  42. if not byte then break end
  43. bytes[#bytes + 1] = byte
  44. end
  45.  
  46. if #bytes > 0 then
  47. if not tape.isReady() then
  48. io.stderr:write("\nError: Tape was removed during writing.\n")
  49. return
  50. end
  51.  
  52. term.setCursorPos(1, y)
  53. bytery = bytery + #bytes
  54. term.write("Wrote " .. tostring(math.min(bytery, filesize)) .. " of " .. tostring(filesize) .. " bytes...")
  55.  
  56. for i=1, #bytes do
  57. tape.write(bytes[i])
  58. end
  59. sleep(0)
  60. end
  61. until #bytes <= 0 or bytery >= filesize
  62.  
  63. tape.stop()
  64. tape.seek(-tape.getSize())
  65. tape.setSpeed(speed)
  66. print("\nDone.")
  67. end
  68.  
  69. if args[1] == nil then
  70. helpText()
  71. else
  72. local url = "https://sus-server.eu/yt-tape?yt-link=" .. args[1]
  73. local arg2 = args[2]
  74.  
  75. if arg2 ~= nil then
  76. local speedValue = tonumber(arg2)
  77. if speedValue then
  78. url = url .. "&qual=" .. arg2
  79. speed = speedValue
  80. else
  81. print(arg2 .. " is not a valid quality. Using default quality.")
  82. url = url .. "&qual=1"
  83. end
  84. else
  85. url = url .. "&qual=1"
  86. end
  87.  
  88. print("Converting to dfpwm... please wait")
  89.  
  90. request = http.get(url, nil, true)
  91. if not request then
  92. printError("Failed to get data from the server.")
  93. return
  94. end
  95.  
  96. data = request.readAll()
  97. request.close()
  98.  
  99. if not data then
  100. printError("Failed to read data.")
  101. return
  102. end
  103.  
  104. writeTape(data)
  105. end
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement