Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. (function(...)
  2. local shell = require("shell")
  3. local args, opts = shell.parse(...)
  4. local c = require("component")
  5. local internet = require("internet")
  6. local term = require("term")
  7. local tape
  8.  
  9. if opts["A"] then
  10. tape = c.proxy(c.get(opts["A"]))
  11. print("Using tape drive ["..tape.address.."]")
  12. else
  13. tape = c.tape_drive
  14. print("Using tape drive ["..tape.address.."]")
  15. i = 0
  16. for k in c.list("tape_drive") do
  17. i = i + 1
  18. end
  19. if i > 1 then
  20. print("WARNING! More than one tape drive detected! Are you sure you want to continue? [y/N]")
  21. if string.lower(string.sub(io.read(), 1, 1))~="y" then print("Exiting!") return end
  22. end
  23. end
  24.  
  25. local b_size = 8 * 1024-- size of block/chunk that's written to tape
  26. local base_bitrate = 32
  27. local base_conv_url = "http://dfpwm.magik6k.net/conv"
  28.  
  29. if opts["a"] then
  30. base_conv_url = "http://dfpwm.magik6k.net/aconv"
  31. b_size = 12 * 1024
  32. base_bitrate = 48
  33. end
  34.  
  35. local bitrate = base_bitrate
  36.  
  37. if opts["b"] then
  38. if not tonumber(opts["b"]) then
  39. print("Please set option `b` to the desired bitrate (as a number).")
  40. return
  41. end
  42. bitrate = tonumber(opts["b"])
  43. else
  44. if opts["d"] then
  45. bitrate = bitrate * 2
  46. end
  47. end
  48.  
  49. if not args[1] then
  50. print("Usage: ytdl - [options: dAtsca] [list of youtube video IDs or URLs].")
  51. print("Options: d - Use double (64K or 96K) bitrate.")
  52. print(" A - Set address of tape to be used.")
  53. print(" b - Set bitrate. Negates effects of `d`.")
  54. print(" t - DISABLES automatic titling of tapes. Sets title if it's a string.")
  55. print(" s - Skip download of video. Mostly for titling untitled tapes.")
  56. print(" c - Continious write to the tape. Title of last video will be used as title if required.")
  57. print(" a - Use DFPWM 1a.")
  58. print("Missing argument(s)! No video id/link passed as argument(s). I can't read minds, you know?")
  59. return
  60. end
  61.  
  62. local getId = function(str)
  63. if string.find(str, "youtube.com") then
  64. _a, _b,id = string.find(str, "v=([a-zA-Z0-9_-]+)")
  65. elseif string.find(str, "youtu.be") then
  66. _a, _b, id = string.find(str, "be/([a-zA-Z0-9_-]+)")
  67. else
  68. id = str
  69. end
  70. return id
  71. end
  72.  
  73. local convertWrite = function(id, l_bitrate)
  74. print("Downloading " .. id)
  75. tape.seek(-tape.getSize())
  76. local url = base_conv_url .. (tostring(l_bitrate) or "") .. "/" .. id
  77. local h = internet.request(url)
  78. local size = 0
  79. print("Total downloaded: " .. string.rep(" ", 10 - #tostring(size)) .. "0" .. " bytes " )
  80. local x, y = term.getCursor()
  81. chunk = ""
  82. for a in h do
  83. size = size + #a
  84. chunk = chunk .. a
  85. if #chunk > b_size then
  86. tape.write(chunk)
  87. chunk = ""
  88. end
  89. term.setCursor(x, y-1)
  90. print("Total downloaded: "..string.rep(" ",10-#tostring(size))..tostring(size).." bytes ")
  91. end
  92. if chunk~="" and #chunk > b_size then
  93. tape.write(chunk)
  94. end
  95. print("Done downloading "..id.."!")
  96. end
  97.  
  98. tape.stop()
  99. if not opts["c"] then
  100. tape.seek(tape.getSize())
  101. end
  102. for k,v in pairs(args) do
  103. local yt_id = getId(v)
  104. if not opts["s"] then
  105. convertWrite(yt_id, bitrate)
  106. else
  107. print("Skipping download of " .. yt_id .. "!")
  108. end
  109.  
  110. if opts["t"] and type(opts["t"]) == "string" and not opts["c"] then
  111. print("Using option -t value as tape label!")
  112. tape.setLabel(opts["t"])
  113. elseif not opts["t"] and not opts["c"] then
  114. print("Using youtube title as tape label!")
  115. local h = internet.request("http://dfpwm.magik6k.net/title/" .. yt_id)
  116. local d = ""
  117. for a in h do
  118. d = d .. a
  119. end
  120. local web_title = string.gsub(d,"\n","")
  121. tape.setLabel(web_title.." ["..tostring(bitrate).."K]")
  122. print("New label: " .. web_title)
  123. end
  124. print("------")
  125. end
  126.  
  127. if not opts["t"] and opts["c"] then
  128. print("Using last youtube title as tape label.")
  129. local h = internet.request("http://dfpwm.magik6k.net/title/"..getId(args[#args]))
  130. local d = ""
  131. for a in h do
  132. d = d..a
  133. end
  134. local web_title = string.gsub(d,"\n","")
  135. tape.setLabel(web_title.." ["..tostring(bitrate).."K]")
  136. elseif opts["t"] and type(opts["t"]) == "string" and opts["c"] then
  137. print("Using option -t value as tape label!")
  138. tape.setLabel(opts["t"])
  139. end
  140.  
  141. tape.setSpeed(bitrate/base_bitrate)
  142. print("Using bitrate "..tostring(bitrate).."K, speed is set to "..tostring(bitrate/base_bitrate))
  143.  
  144. tape.seek(-tape.getSize())
  145. print("Tape rewound.")
  146. end)(...)
  147. print("Exiting `ytdl`")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement