Sirshark10

MetaParseRadio

Jun 18th, 2018
155
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. print("Seeking to start")
  13. tape.seek(-tape.getPosition())
  14. end
  15.  
  16.  
  17.  
  18. local function write(url)
  19. state[2] = 1
  20. backSeek()
  21. print("Writing: "..url)
  22. local reply = http.get(url, nil, true)
  23. repeat
  24. local bytes = {}
  25. for i=1, blockSize do
  26. local byte = reply.read()
  27. if not byte then
  28. break
  29. else
  30. bytes[#bytes+1] = byte
  31. end
  32. end
  33. if #bytes > 0 then
  34. for i=1, #bytes do
  35. tape.write(bytes[i])
  36. end
  37. sleep(0)
  38. end
  39. until not bytes or #bytes <= 0
  40. endPos = tape.getPosition()
  41. print("End: "..endPos)
  42. backSeek()
  43. print("End of Write")
  44. state[2] = 0
  45. end
  46.  
  47. local function stop()
  48. print("Stopping")
  49. tape.stop()
  50. end
  51.  
  52. local function play(A,T)
  53. print(T.." - "..A)
  54. tape.play()
  55. state[1] = 1
  56. end
  57.  
  58. local function fin()
  59. print("Awaiting song..")
  60. tape.stop()
  61. modem.transmit(control[3],control[3],"ready")
  62. end
  63.  
  64.  
  65. local function main()
  66. fin()
  67. while true do
  68. local evnt, side, sChan, rChan, msg, dist = os.pullEvent("modem_message")
  69. local tbl = textutils.unserialize(msg)
  70. if tbl["URL"] ~= nil then
  71. local URL = tbl["URL"]
  72. local Artist = tbl["Artist"]
  73. local Title = tbl["Title"]
  74. if rChan == control[1] and string.find(URL,".dfpwm") then
  75. backSeek()
  76. write(URL)
  77. play(Artist,Title)
  78. elseif rChan == control[2] and msg == "stop" then
  79. stop()
  80. end
  81. end
  82. end
  83. end
  84.  
  85. local function secondary()
  86. print(state[1])
  87. print(state[2])
  88. print(endPos)
  89. print(tape.getPosition())
  90. while true do
  91. if tape.getPosition() >= endPos and state[1] == 1 and state[2] ~= 1 then
  92. print("End Of Song")
  93. fin()
  94. backSeek()
  95. end
  96. sleep(0.5)
  97. end
  98. end
  99. parallel.waitForAny(main,secondary)
Advertisement
Add Comment
Please, Sign In to add comment