dracoix

ytdl

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