Advertisement
nagoL2015

ytdl

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