-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathytdl.lua
161 lines (142 loc) · 4.91 KB
/
ytdl.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
--This "downloader" was initially
--hacked together by Saphire Lattice
--on BTM16 (January 2nd and 3rd)
local shell = require("shell")
local args, opts = shell.parse(...)
local c = require("component")
local internet = require("internet")
local term = require("term")
local tape
if opts["A"] then
if type(opts["A"]) ~= "string" then
print("Usage of flag A:")
print(" ytdl --A=address ...")
os.exit(1)
end
tape = c.proxy(c.get(opts["A"]))
print("Using tape drive ["..tape.address.."]")
else
tape = c.tape_drive
print("Using tape drive ["..tape.address.."]")
i = 0
for k in c.list("tape_drive") do
i = i + 1
end
if i > 1 then
print("WARNING! More than one tape drive detected! Are you sure you want to continue? [y/N]")
if string.lower(string.sub(io.read(), 1, 1))~="y" then print("Exiting!") os.exit(1) end
end
end
local isCont = opts["c"] or false
local base_site = "http://dfpwm.catgirl.services"
local base_bitrate = 48
local base_conv_url = base_site .. "/aconv"
if not opts["a"] and opts["o"] then
base_conv_url = base_site .. "/conv"
base_bitrate = 32
end
local bitrate = base_bitrate
if opts["b"] or opts["bitrate"] then
if not tonumber(opts["b"] or opts["bitrate"]) then
print("Please set option `b` to the desired bitrate (as a number).")
os.exit(1)
end
bitrate = tonumber(opts["b"] or opts["bitrate"])
local ratio = bitrate / base_bitrate
if ratio < 0.25 or ratio > 2 then
print(string.format("Bitrate cannot be set to thisi value, min %d, max %d.", math.floor(base_bitrate / 4), math.floor(base_bitrate * 2)))
os.exit(1)
end
else
if opts["d"] then
bitrate = bitrate * 2
end
end
if not args[1] then
print("Usage: ytdl - [options: dAscRaot] [list of youtube video IDs or URLs].")
print("Options: d - Use double the default bitrate (96K for v1a or 64K for v1). More quality but bigger size.")
print(" A - Set address of tape to be used. Can be partial.")
print(" s - Skip download of video. Mostly for titling untitled tapes.")
print(" c - Continious write to the tape. Title of last video will be used as title if required.")
print(" a - Use DFPWM 1a. Default, priority over flag o.")
print(" o - Use older DFPWM 1 format, pre-1.8.9. Can be played later, but speed needs to be adjusted")
print(" t/title - DISABLES automatic titling of tapes. Sets title if it's a string.")
print(" b/bitrate - Set bitrate. Negates effects of `d`. Minimum 12 or 8, maximum 96 or 64. Usage: --b=number")
print("Missing argument(s)! No video id/link passed as argument(s).")
return
end
local getId = function(str)
if string.find(str, "youtube.com") then
_a, _b,id = string.find(str, "v=([a-zA-Z0-9_-]+)")
elseif string.find(str, "youtu.be/") then
_a, _b, id = string.find(str, "be/([a-zA-Z0-9_-]+)")
else
id = str
end
return id
end
local function printSize(bytes)
print(string.format("Total downloaded: % 10d bytes", bytes))
end
local function downloadAudio(id, l_bitrate)
local b_size = bitrate * 256
print("Downloading " .. id)
local url = base_conv_url .. (tostring(l_bitrate) or "") .. "/" .. id
local h = internet.request(url)
local size = 0
printSize(size)
local x, y = term.getCursor()
chunk = ""
for a in h do
size = size + #a
chunk = chunk .. a
if #chunk > b_size then
tape.write(chunk)
chunk = ""
end
term.setCursor(x, y-1)
printSize(size)
end
if chunk~="" and #chunk > b_size then
tape.write(chunk)
end
print("Done downloading "..id.."!")
end
local function setLabel(id, needCont)
local needCont = needCont or false
if opts["t"] and type(opts["t"]) == "string" and isCont == needCont then
print("Using option -t value as tape label!")
tape.setLabel(opts["t"])
elseif not opts["t"] and isCont == needCont then
print("Using youtube title as tape label!")
local h = internet.request(base_site .. "/title/" .. id)
local d = ""
for a in h do
d = d .. a
end
local web_title = string.gsub(d,"\n","")
tape.setLabel(web_title.." ["..tostring(bitrate).."K]")
print("New label: " .. web_title)
end
end
tape.stop()
if not isCont then
tape.seek(-math.huge)
end
for k,v in pairs(args) do
local yt_id = getId(v)
if not opts["s"] then
downloadAudio(yt_id, bitrate)
else
print("Skipping download of " .. yt_id .. "!")
end
setLabel(yt_id, false)
print("------")
end
setLabel(getId(args[#args]), true)
tape.setSpeed(bitrate / base_bitrate)
print("Using bitrate "..tostring(bitrate).."K, speed is set to "..tostring(bitrate / base_bitrate))
if not isCont then
tape.seek(-math.huge)
print("Tape rewound.")
end