|
1 | 1 | --[[
|
2 |
| -_ _ _ _ _ _ ___ ____ ____ |
3 |
| -|\ | | | |\/| |__] |___ |__/ |
4 |
| -| \| |__| | | |__] |___ | \ |
5 |
| -____ ____ ____ _ _ ____ ___ ___ ____ ____ |
6 |
| -|___ | | |__/ |\/| |__| | | |___ |__/ |
7 |
| -| |__| | \ | | | | | | |___ | \ |
8 |
| -]] |
9 |
| - |
10 |
| -local NumberFormatter = {} |
11 |
| --- https://devforum.roblox.com/t/how-can-i-turn-a-number-to-a-shorter-number-i-dont-know-how-to-explain-click-to-understand-3/649496/3 |
12 |
| - |
13 |
| -local Suffixes = { "k", "M", "B", "T", "qd", "Qn", "sx", "Sp", "O", "N", "de", "Ud", "DD", "tdD", "qdD", "QnD", "sxD", |
14 |
| - "SpD", "OcD", "NvD", "Vgn", "UVg", "DVg", "TVg", "qtV", "QnV", "SeV", "SPG", "OVG", "NVG", "TGN", "UTG", "DTG", |
15 |
| - "tsTG", "qtTG", "QnTG", "ssTG", "SpTG", "OcTG", "NoTG", "QdDR", "uQDR", "dQDR", "tQDR", "qdQDR", "QnQDR", "sxQDR", |
16 |
| - "SpQDR", "OQDDr", "NQDDr", "qQGNT", "uQGNT", "dQGNT", "tQGNT", "qdQGNT", "QnQGNT", "sxQGNT", "SpQGNT", "OQQGNT", |
17 |
| - "NQQGNT", "SXGNTL" } |
18 |
| - |
19 |
| -function NumberFormatter.compact(number) |
20 |
| - local Negative = number < 0 |
21 |
| - number = math.abs(number) |
22 |
| - |
23 |
| - local Paired = false |
24 |
| - for i in pairs(Suffixes) do |
25 |
| - if not (number >= 10 ^ (3 * i)) then |
26 |
| - number = number / 10 ^ (3 * (i - 1)) |
27 |
| - local isComplex = string.find(tostring(number), ".") and string.sub(tostring(number), 4, 4) ~= "." |
28 |
| - number = string.sub(tostring(number), 1, isComplex and 4 or 3) .. (Suffixes[i - 1] or "") |
29 |
| - Paired = true |
30 |
| - break |
31 |
| - end |
32 |
| - end |
33 |
| - if not Paired then |
34 |
| - local Rounded = math.floor(number) |
35 |
| - number = tostring(Rounded) |
36 |
| - end |
37 |
| - if Negative then |
38 |
| - return "-" .. number |
39 |
| - end |
40 |
| - return number -- returns 1.0k for example |
41 |
| -end |
| 2 | +_ _ ____ _ _ ____ _ _ ___ ____ |
| 3 | + \_/ | | | | | | | |__] |___ |
| 4 | + | |__| |__| |___ |__| |__] |___ |
42 | 5 |
|
43 |
| -function NumberFormatter.abbreviate(number) |
44 |
| - local left, num, right = string.match(number, '^([^%d]*%d)(%d*)(.-)$') |
45 |
| - return left .. num:reverse():gsub('(%d%d%d)', '%1,'):reverse() .. right -- returns for example 1,000, it gets every 3 zeros and adds a comma |
46 |
| -end |
47 |
| - |
48 |
| ---[[ |
49 |
| -_ _ ____ _ _ ____ _ _ ___ ____ ____ ___ _ |
50 |
| - \_/ | | | | | | | |__] |___ |__| |__] | |
51 |
| - | |__| |__| |___ |__| |__] |___ | | | | |
| 6 | +Github Repository: https://github.com/Commandcracker/YouCube |
| 7 | +License: GPL-3.0 |
| 8 | +Client Version: 0.0.poc0 |
52 | 9 | ]]
|
53 | 10 |
|
54 |
| -local YouCubeAPI = {} |
55 |
| - |
56 |
| -function YouCubeAPI.new(websocket) |
57 |
| - return setmetatable({ |
58 |
| - websocket = websocket, |
59 |
| - }, { __index = YouCubeAPI }) |
60 |
| -end |
61 |
| - |
62 |
| -local servers = { |
63 |
| - "ws://localhost:5000", |
64 |
| - "ws://oxygen.knijn.one:5000", -- By EmmaKnijn, Contact EmmaKnijn#0043 on Discord if this doesn't work |
65 |
| - "wss://youcube.onrender.com" -- By Commandcracker |
66 |
| -} |
| 11 | +-- Libraries - OpenLibrarieLoader v1.0.0 -- |
67 | 12 |
|
68 |
| -if settings then |
69 |
| - local server = settings.get("youcube.server") |
70 |
| - if server then |
71 |
| - table.insert(servers, 1, server) |
| 13 | +local function is_lib(Table, Item) |
| 14 | + for key, value in ipairs(Table) do |
| 15 | + if value == Item or value .. ".lua" == Item then |
| 16 | + return true, value |
| 17 | + end |
72 | 18 | end
|
| 19 | + return false |
73 | 20 | end
|
74 | 21 |
|
75 |
| -function YouCubeAPI:detect_bestest_server() |
76 |
| - for i, server in pairs(servers) do |
77 |
| - local websocket, websocket_error = http.websocket(server) |
78 |
| - |
79 |
| - if websocket ~= false then |
80 |
| - term.write("Using the YouCube server: ") |
81 |
| - term.setTextColor(colors.blue) |
82 |
| - print(server) |
83 |
| - term.setTextColor(colors.white) |
84 |
| - self.websocket = websocket |
85 |
| - break |
86 |
| - elseif i == #servers then |
87 |
| - error(websocket_error) |
| 22 | +local libs = { "youcubeapi", "numberformatter" } |
| 23 | +local lib_paths = { ".", "./lib", "./apis", "./modules", "/", "/lib", "/apis", "/modules" } |
| 24 | + |
| 25 | +for i, path in pairs(lib_paths) do |
| 26 | + if fs.exists(path) then |
| 27 | + for _i, file_name in pairs(fs.list(path)) do |
| 28 | + local found, lib = is_lib(libs, file_name) |
| 29 | + if found and libs[lib] == nil then |
| 30 | + if require then |
| 31 | + libs[lib] = require(path .. "/" .. file_name:gsub(".lua", "")) |
| 32 | + else |
| 33 | + libs[lib] = dofile(path .. "/" .. file_name) |
| 34 | + end |
| 35 | + end |
88 | 36 | end
|
89 |
| - |
90 | 37 | end
|
91 | 38 | end
|
92 | 39 |
|
93 |
| -function YouCubeAPI:get_chunk(chunkindex, id) |
94 |
| - self.websocket.send(textutils.serialiseJSON({ |
95 |
| - ["action"] = "get_chunk", |
96 |
| - ["chunkindex"] = chunkindex, |
97 |
| - ["id"] = id |
98 |
| - })) |
99 |
| -end |
100 |
| - |
101 |
| -function YouCubeAPI:request_media(url) |
102 |
| - --local status, retval = pcall(self.websocket.send, textutils.serialiseJSON({ |
103 |
| - self.websocket.send(textutils.serialiseJSON({ |
104 |
| - ["action"] = "request_media", |
105 |
| - ["url"] = url |
106 |
| - })) |
107 |
| - |
108 |
| - --if not status then |
109 |
| - -- print("Lost connection to server -> Reconnection ...") |
110 |
| - -- self:detect_bestest_server() |
111 |
| - -- self:request_media(url) |
112 |
| - --end |
| 40 | +for key, lib in ipairs(libs) do |
| 41 | + if libs[lib] == nil then |
| 42 | + error("Library \"" .. lib .. "\" not found") |
| 43 | + end |
113 | 44 | end
|
114 | 45 |
|
115 |
| ---[[ |
116 |
| -_ _ ____ _ _ _ ____ _ _ |
117 |
| -|\/| |__| | |\ | | | | |
118 |
| -| | | | | | \| |___ |___ | |
119 |
| -]] |
| 46 | +-- CraftOS-PC support -- |
120 | 47 |
|
121 |
| -if periphemu then -- CraftOS-PC |
| 48 | +if periphemu then |
122 | 49 | periphemu.create("top", "speaker")
|
123 | 50 | end
|
124 | 51 |
|
| 52 | +-- main -- |
| 53 | + |
125 | 54 | local speaker = peripheral.find("speaker")
|
126 | 55 | local tape = peripheral.find("tape_drive")
|
127 | 56 |
|
128 | 57 | if speaker == nil and tape == nil then
|
129 | 58 | error("You need a tapedrive or speaker in order to use YouCube!")
|
130 | 59 | end
|
131 | 60 |
|
132 |
| -local youcubeapi = YouCubeAPI.new() |
| 61 | +local youcubeapi = libs.youcubeapi.new() |
133 | 62 | youcubeapi:detect_bestest_server()
|
134 | 63 |
|
135 | 64 | -------------------------------
|
@@ -174,11 +103,11 @@ local function run(url, no_close)
|
174 | 103 | term.setTextColor(colors.white)
|
175 | 104 |
|
176 | 105 | if data.like_count then
|
177 |
| - print("Likes: " .. NumberFormatter.compact(data.like_count)) |
| 106 | + print("Likes: " .. libs.numberformatter.compact(data.like_count)) |
178 | 107 | end
|
179 | 108 |
|
180 | 109 | if data.view_count then
|
181 |
| - print("Views: " .. NumberFormatter.compact(data.view_count)) |
| 110 | + print("Views: " .. libs.numberformatter.compact(data.view_count)) |
182 | 111 | end
|
183 | 112 |
|
184 | 113 | local x, y = term.getCursorPos()
|
|
0 commit comments