Advertisement
Asioron

player

Mar 24th, 2017
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.77 KB | None | 0 0
  1. local fl = require("filesystem")
  2. local component = require("component")
  3. local term = require("term")
  4. local tape = component.tape_drive
  5. local a={}
  6. local list = {}
  7. local vr='0'
  8.  
  9. print(fl.remove('/home/index.html')) os.execute('/bin/wget.lua https://www.dropbox.com/sh/2ki7ijnha2b24t7/AACjJL9V-UFa6giHOjjzf7U1a?dl=0 /home/index.html')
  10. local file = io.open('/home/index.html', 'r')
  11.  
  12. local function write(path)
  13.   local file, msg, _, y
  14.   local block = 2048 --How much to read at a time
  15.   tape.stop()
  16.   tape.seek(-tape.getSize())
  17.   tape.stop() --Just making sure
  18.   local bytery = 0 --For the progress indicator
  19.   local filesize = tape.getSize()
  20.   if string.match(path, "https?://.+") then
  21.     if not component.isAvailable("internet") then
  22.       io.stderr:write("This command requires an internet card to run.")
  23.       return false
  24.     end
  25.     local internet = component.internet
  26.     local function setupConnection(url)
  27.       local file, reason = internet.request(url)
  28.       if not file then
  29.         io.stderr:write("error requesting data from URL: " .. reason .. "\n")
  30.         return false
  31.       end
  32.       local connected, reason = false, ""
  33.       local timeout = 50
  34.       for i = 1, timeout do
  35.         connected, reason = file.finishConnect()
  36.         os.sleep(.1)
  37.         if connected or connected == nil then
  38.           break
  39.         end
  40.       end
  41.       if connected == nil then
  42.         io.stderr:write("Could not connect to server: " .. reason)
  43.         return false
  44.       end
  45.       local status, message, header = file.response()
  46.       if status then
  47.         status = string.format("%d", status)
  48.         if status:sub(1,1) == "2" then
  49.           return true, {
  50.             close = function(self, ...) return file.close(...) end,
  51.             read = function(self, ...) return file.read(...) end,
  52.           }, header
  53.         end
  54.         return false
  55.       end
  56.       io.stderr:write("no valid HTTP response - no response")
  57.       return false
  58.     end
  59.     local success, header
  60.     success, file, header = setupConnection(path)
  61.     if not success then
  62.       if file then
  63.         file:close()
  64.       end
  65.       return
  66.     end
  67.     print("Writing...")
  68.     _, y = term.getCursor()
  69.     if header and header["Content-Length"] and header["Content-Length"][1] then
  70.       filesize = tonumber(header["Content-Length"][1])
  71.     end
  72.   else
  73.     local path = shell.resolve(path)
  74.     filesize = fs.size(path)
  75.     file, msg = io.open(path, "rb")
  76.     if not file then
  77.       io.stderr:write("Error: " .. msg)
  78.       return
  79.     end
  80.     print("Writing...")
  81.     _, y = term.getCursor()
  82.   end
  83.   if filesize > tape.getSize() then
  84.     term.setCursor(1, y)
  85.     io.stderr:write("Warning: File is too large for tape, shortening file\n")
  86.     _, y = term.getCursor()
  87.     filesize = tape.getSize()
  88.   end
  89.   --Displays long numbers with commas
  90.   local function fancyNumber(n)
  91.     return tostring(math.floor(n)):reverse():gsub("(%d%d%d)", "%1,"):gsub("%D$", ""):reverse()
  92.   end
  93.   repeat
  94.     local bytes = file:read(block)
  95.     if bytes and #bytes > 0 then
  96.       if not tape.isReady() then
  97.         io.stderr:write("\nError: Tape was removed during writing.\n")
  98.         file:close()
  99.         return
  100.       end
  101.       term.setCursor(1, y)
  102.       bytery = bytery + #bytes
  103.       local displaySize = math.min(bytery, filesize)
  104.       term.write(string.format("Write %s of %s bytes... (%.2f %%)", fancyNumber(displaySize), fancyNumber(filesize), 100 * displaySize / filesize))
  105.       tape.write(bytes)
  106.     end
  107.   until not bytes or bytery > filesize
  108.   file:close()
  109.   tape.stop()
  110.   tape.seek(-tape.getSize())
  111.   tape.stop() --Just making sure
  112.   print("\nDone.")
  113. end
  114.  
  115. local function analuze()
  116.   while #a~=0 do
  117.     if string.find(a[1],'href')==nil then
  118.       table.remove(a,1)
  119.     else
  120.       b = string.sub(a[1],string.find(a[1],'href'),string.len(a[1]))
  121.       for i=2,#a do
  122.         b=b..a[i]
  123.         a[i]=''
  124.       end
  125.       a={}
  126.       break
  127.     end
  128.   end
  129. end
  130.  
  131. local function analuze2()
  132.   while #b~=0 do
  133.     if string.find(b,'href')~=nil then
  134.       b = string.sub(b,string.find(b,'href')+8,string.len(b))
  135.       list[#list+1]= string.sub(b,1,string.find(b,'"')-1)
  136.       b = string.sub(b, string.find(b,'"'), string.len(b))
  137.     else
  138.       break
  139.     end
  140.   end
  141. end
  142.  
  143. while vr~='' do
  144.   vr = file:read(100)
  145.   if string.find(vr,'InitReact')~=nil then break end
  146. end
  147.  
  148. while vr~='' do
  149.   vr = file:read(100)
  150.   if string.find(vr,'\n')~=nil then break end
  151.   a[#a+1] = vr
  152. end
  153. file:close()
  154.  
  155. analuze()
  156. analuze2()
  157. print('найдено '..#list..' песен')
  158. print('введите любой номер песни')
  159. n = tonumber(io.read())
  160. print(list[n])
  161. tape.seek(-tape.getSize())
  162. write((list[n]):gsub("www%.dropbox%.com","dl.dropboxusercontent.com"):gsub("%?dl=%d$", ""))
  163. tape.setSpeed(2)
  164. tape.play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement