Advertisement
Guest User

nRec.lua

a guest
Jun 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.95 KB | None | 0 0
  1. local modem = peripheral.find("modem")
  2. local tape = peripheral.find("tape_drive")
  3. local endPos = 1
  4. local control = {1200,1500,1800}
  5. local blockSize = 32768
  6. local state = {0,0}
  7. for k,v in pairs(control) do
  8.   modem.open(v)
  9. end
  10.  
  11. local function backSeek()
  12.     tape.seek(-tape.getPosition())
  13. end
  14.  
  15.  
  16.  
  17. local function Write(url)
  18.     state[2] = 1
  19.     backSeek()
  20.     local reply = http.get(url, nil, true)
  21.     repeat
  22.         local bytes = {}
  23.         for i=1, blockSize do
  24.             local byte = reply.read()
  25.             if not byte then
  26.                 break
  27.             else
  28.                 bytes[#bytes+1] = byte
  29.             end
  30.         end
  31.         if #bytes > 0  then
  32.             for i=1, #bytes do
  33.                 tape.write(bytes[i])
  34.             end
  35.             sleep(0)
  36.         end
  37.     until not bytes or #bytes <= 0
  38.     endPos = tape.getPosition()
  39.     backSeek()
  40.     state[2] = 0
  41. end
  42.  
  43. local function stop()
  44.   print("Stopping")
  45.   tape.stop()
  46. end
  47.  
  48. local function play(A,T)
  49.   print(T.." - "..A)
  50.   tape.play()
  51.   state[1] = 1
  52. end
  53.  
  54. local function fin()
  55.   print("Awaiting song..")
  56.   tape.stop()
  57.   modem.transmit(control[3],control[3],"ready")
  58. end
  59.  
  60.  
  61. local function main()
  62.     fin()
  63.   while true do
  64.     local evnt, side, sChan, rChan, msg, dist = os.pullEvent("modem_message")
  65.     local tbl = textutils.unserialize(msg)
  66.     if tbl ~= nil and tbl["URL"] ~= nil then
  67.       local URL = tbl["URL"]
  68.       local Artist = tbl["Artist"]
  69.       local Title = tbl["Title"]
  70.       if rChan == control[1] and string.find(URL,".dfpwm") then
  71.         backSeek()
  72.         Write(URL)
  73.         play(Artist,Title)
  74.       elseif rChan == control[2] and msg == "stop" then
  75.         stop()
  76.         end
  77.       end
  78.     end
  79. end
  80.  
  81. local function secondary()
  82.   while true do
  83.     if tape.getPosition() >= endPos and state[1] == 1 and state[2] ~= 1 then
  84.       fin()
  85.       backSeek()
  86.     end
  87.     sleep(0.5)
  88.   end
  89. end
  90. parallel.waitForAny(main,secondary)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement