Sirshark10

Updated

Jun 18th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 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. local function sync()
  61. modem.transmit(control[3],control[3],"sync")
  62. end
  63.  
  64. local function main()
  65. sync()
  66. while true do
  67. local evnt, side, sChan, rChan, msg, dist = os.pullEvent("modem_message")
  68. local tbl = textutils.unserialize(msg)
  69. if tbl ~= nil and tbl["URL"] ~= nil then
  70. local URL = tbl["URL"]
  71. local Artist = tbl["Artist"]
  72. local Title = tbl["Title"]
  73. if rChan == control[1] and string.find(URL,".dfpwm") then
  74. backSeek()
  75. Write(URL)
  76. play(Artist,Title)
  77. elseif rChan == control[2] and msg == "stop" then
  78. stop()
  79. elseif rChan == control[3] and state[1] ~= 1 then
  80. backSeek()
  81. Write(URL)
  82. play(Artist,Title)
  83. end
  84. end
  85. end
  86. end
  87.  
  88. local function secondary()
  89. while true do
  90. if tape.getPosition() >= endPos and state[1] == 1 and state[2] ~= 1 then
  91. fin()
  92. backSeek()
  93. end
  94. sleep(0.5)
  95. end
  96. end
  97. parallel.waitForAny(main,secondary)
Advertisement
Add Comment
Please, Sign In to add comment