PancakePhD

Spotify

Dec 19th, 2020 (edited)
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.58 KB | None | 0 0
  1. local debug = false
  2. local auth = "<insert token here>"
  3.  
  4. if not fs.exists("json") then
  5.     shell.run("pastebin", "get", "4nRg9CHU", "json")
  6. end
  7.  
  8. os.loadAPI("json")
  9.  
  10. if not fs.exists("touchpoint") then
  11.     shell.run("pastebin", "get", "pFHeia96", "touchpoint")
  12. end
  13.  
  14. os.loadAPI("touchpoint")
  15.  
  16. local playerActions = {
  17.     ["CurrentlyPlaying"] = {
  18.         url = "",
  19.         method = "GET"
  20.     },
  21.     ["Next"] = {
  22.         url = "/next",
  23.         method = "POST"
  24.     },
  25.     ["Pause"] = {
  26.         url = "/pause",
  27.         method = "PUT"
  28.     },
  29.     ["Play"] = {
  30.         url = "/play",
  31.         method = "PUT"
  32.     },
  33.     ["Previous"] = {
  34.         url = "/previous",
  35.         method = "POST"
  36.     },
  37.     ["Repeat"] = {
  38.         url = "/repeat",
  39.         method = "PUT"
  40.     },
  41.     ["Seek"] = {
  42.         url = "/seek",
  43.         method = "PUT"
  44.     },
  45.     ["Shuffle"] = {
  46.         url = "/shuffle",
  47.         method = "PUT"
  48.     },
  49.     ["Volume"] = {
  50.         url = "/volume",
  51.         method = "PUT"
  52.     }
  53. }
  54.  
  55. local headers = {
  56.     ["Authorization"] = "Bearer "..auth,
  57.     ["Content-Length"] = "0"
  58. }
  59.  
  60. function player(action, query)
  61.     local url = "https://api.spotify.com/v1/me/player"..action.url
  62.  
  63.     if query then
  64.         url = url.."?"..query
  65.     end
  66.  
  67.     local request = http.request({
  68.         url = url,
  69.         headers = headers,
  70.         method = action.method
  71.     })
  72.  
  73.     while true do    
  74.         local event, url, request = os.pullEvent()
  75.         if event == "http_success" then
  76.             return request.readAll()
  77.         elseif event == "http_failure" then
  78.             print(url)
  79.             printError(request)
  80.             return false
  81.         end
  82.     end
  83. end
  84.  
  85. function getCurrentArtistAndSong()
  86.     local playback = json.decode(player(playerActions.CurrentlyPlaying))
  87.    
  88.     if playback then
  89.         return playback.item.album.artists[1].name..": "..playback.item.name
  90.     end
  91. end
  92.  
  93. function getCurrentPlaytime(playback)
  94.     if playback then
  95.         local progress = getMinuteFormat(math.floor(playback.progress_ms / 1000))
  96.         local duration = getMinuteFormat(math.floor(playback.item.duration_ms / 1000))
  97.         return progress.." / "..duration
  98.     end
  99.  
  100.     return "No Song Playing"
  101. end
  102.  
  103. function getMinuteFormat(seconds)
  104.     local minutes = math.floor(seconds / 60)
  105.     local seconds = seconds % 60
  106.  
  107.     if seconds < 10 then
  108.         seconds = "0"..seconds
  109.     end
  110.  
  111.     return minutes..":"..seconds
  112. end
  113.  
  114. function getSecondsRemainingInSong()
  115.     local playback = json.decode(player(playerActions.CurrentlyPlaying))
  116.  
  117.     if playback then
  118.         return math.ceil((playback.item.duration_ms - playback.progress_ms) / 1000)
  119.     end
  120.  
  121.     return 0
  122. end
  123.  
  124. function isPlaying()
  125.     local playback = json.decode(player(playerActions.CurrentlyPlaying))
  126.  
  127.     if playback then
  128.         return playback.is_playing
  129.     end
  130.  
  131.     return false
  132. end
  133.  
  134. function toggleShuffle()
  135.     local playback = json.decode(player(playerActions.CurrentlyPlaying))
  136.  
  137.     if playback then
  138.         if debug then
  139.             print("Shuffle: "..toString(playback.shuffle_state).." -> "..toString(not playback.shuffle_state))
  140.         end
  141.  
  142.         return player(playerActions.Shuffle, "state="..tostring(not playback.shuffle_state))
  143.     end
  144. end
  145.  
  146. function seek(seconds)
  147.     local playback = json.decode(player(playerActions.CurrentlyPlaying))
  148.    
  149.     if playback then
  150.         local positionToSeek = playback.progress_ms + (seconds * 1000)
  151.  
  152.         if debug then
  153.             print("Seeking from "..math.floor(playback.progress_ms / 1000).."s to "..positionToSeek.."s")
  154.         end
  155.  
  156.         return player(playerActions.Seek, "position_ms="..positionToSeek)
  157.     end
  158. end
  159.  
  160. function togglePlay()
  161.     local playback = json.decode(player(playerActions.CurrentlyPlaying))
  162.    
  163.     if playback then
  164.         if debug then
  165.             print("Play Toggle: "..tostring(playback.is_playing).." -> "..tostring(not playback.is_playing))
  166.         end
  167.  
  168.         if playback.is_playing then
  169.             return player(playerActions.Pause)
  170.         else
  171.             return player(playerActions.Play)
  172.         end
  173.     end
  174. end
  175.  
  176. --print(getCurrentArtistAndSong())
  177. --print(getSecondsRemainingInSong())
  178. --toggleShuffle()
  179. --seek(-10)
  180. --togglePlay()
  181.  
  182. --player(playerActions.CurrentlyPlaying)
  183. --player(playerActions.Next)
  184. --player(playerActions.Pause)
  185. --player(playerActions.Play)
  186. --player(playerActions.Previous)
  187. --player(playerActions.Repeat, "state=true")
  188. --player(playerActions.Seek, "position_ms=25000")
  189. --player(playerActions.Shuffle, "state=true")
  190. --player(playerActions.Volume, "volume_percent=50")
  191.  
  192. local currentArtistAndSong
  193. local currentPlayTime
  194.  
  195. local secondsTimer
  196.  
  197. local t = touchpoint.new("top")
  198.  
  199. function render()
  200.     local size = math.floor(term.getSize() / 2)
  201.     local middle = math.floor(size / 2)
  202.  
  203.     if pocket then
  204.         t:add("<", nil, middle, middle + 10, middle + 4, middle + 12, colors.gray)
  205.         t:add(">", nil, middle + 6, middle + 10, middle + 10, middle + 12, colors.gray)
  206.         t:add("Play", nil, middle, middle + 6, middle + 16, middle + 8, colors.lime, colors.red)
  207.         t:add("+", nil, middle + 12, middle + 10, middle + 16, middle + 10, colors.gray)
  208.         t:add("-", nil, middle + 12, middle + 12, middle + 16, middle + 12, colors.gray)
  209.         t:add("Click Play!", nil, 1, middle, size * 2, middle, colors.gray)
  210.         t:add("No Song Playing", nil, 1, middle + 1, size * 2, middle + 1, colors.gray)
  211.     else
  212.         local monitor = peripheral.wrap("top")
  213.  
  214.         monitor.setTextScale(.5)
  215.         monitor.clear()
  216.  
  217.         t:add("Previous", nil, middle + 4, middle + 2, middle + 16, middle + 4, colors.gray)
  218.         t:add("Next", nil, middle + 18, middle + 2, middle + 30, middle + 4, colors.gray)
  219.         t:add("Play", nil, middle + 32, middle + 2, middle + 44, middle + 4, colors.lime, colors.red)
  220.         t:add("Vol +", nil, 4, middle - 2, 10, middle , colors.gray)
  221.         t:add("Vol -", nil, 4, middle + 2, 10, middle + 4, colors.gray)
  222.         t:add("Click Play!", nil, 4, middle - 8, middle + 44, middle - 4, colors.gray)
  223.         t:add("No Song Playing", nil, middle + 4, middle - 2, middle + 44, middle, colors.gray)
  224.     end
  225.  
  226.         currentArtistAndSong = "Click Play!"
  227.         currentPlayTime = "No Song Playing"
  228.  
  229.  
  230.     if isPlaying() then
  231.         t:toggleButton("Play")
  232.         t:rename("Play", "Pause")
  233.  
  234.         resetSongInfo()
  235.     end
  236.  
  237.     t:draw()
  238.  
  239.     local previous, next, volUp, volDown = "Previous", "Next", "Vol +", "Vol -"
  240.  
  241.     if pocket then
  242.         previous = "<"
  243.         next = ">"
  244.         volUp = "+"
  245.         volDown = "-"
  246.     end
  247.  
  248.     while true do
  249.         local event, p1 = t:handleEvents()
  250.  
  251.         if event == "button_click" then
  252.             if (p1 == "Play") then
  253.                 if debug then
  254.                     print("Play Clicked")
  255.                 end
  256.  
  257.                 togglePlay()
  258.                 t:toggleButton(p1)
  259.                 t:rename("Play", "Pause")
  260.                
  261.                 resetSongInfo()
  262.             elseif (p1 == "Pause") then
  263.                 if debug then
  264.                     print("Pause Clicked")
  265.                 end
  266.  
  267.                 togglePlay()
  268.                 t:toggleButton(p1)
  269.                 t:rename("Pause", "Play")
  270.  
  271.                 resetSongInfo()
  272.             elseif (p1 == previous) then
  273.                 if debug then
  274.                     print("Previous Clicked")
  275.                 end
  276.  
  277.                 player(playerActions.Previous)
  278.  
  279.                 resetSongInfo()
  280.             elseif (p1 == next) then
  281.                 if debug then
  282.                     print("Next Clicked")
  283.                 end
  284.  
  285.                 player(playerActions.Next)
  286.  
  287.                 resetSongInfo()
  288.             elseif (p1 == volUp) then
  289.                 if debug then
  290.                     print("Vol + Clicked")
  291.                 end
  292.  
  293.                 local playback = json.decode(player(playerActions.CurrentlyPlaying))
  294.  
  295.                 local playbackVolume = playback.device.volume_percent + 5
  296.  
  297.                 if (playbackVolume > 100) then
  298.                     playbackVolume = 100
  299.                 end
  300.  
  301.                 player(playerActions.Volume, "volume_percent="..playbackVolume)
  302.  
  303.                 resetSongInfo()
  304.             elseif (p1 == volDown) then
  305.                 if debug then
  306.                     print("Vol - Clicked")
  307.                 end
  308.  
  309.                 local playback = json.decode(player(playerActions.CurrentlyPlaying))
  310.  
  311.                 local playbackVolume = playback.device.volume_percent - 5
  312.  
  313.                 if (playbackVolume < 0) then
  314.                     playbackVolume = 0
  315.                 end
  316.  
  317.                 player(playerActions.Volume, "volume_percent="..playbackVolume)
  318.  
  319.                 resetSongInfo()
  320.             end
  321.         elseif event == "timer" then
  322.                 if p1 == secondsTimer then
  323.                     resetSongInfo()
  324.                 end
  325.         end
  326.     end
  327. end
  328.  
  329. function resetSongInfo()
  330.     local playback = json.decode(player(playerActions.CurrentlyPlaying))
  331.  
  332.     local newPlayTime = getCurrentPlaytime(playback)
  333.  
  334.     t:rename(currentPlayTime, newPlayTime)
  335.     currentPlayTime = newPlayTime
  336.  
  337.     t:rename(currentArtistAndSong, getCurrentArtistAndSong())
  338.     currentArtistAndSong = getCurrentArtistAndSong()
  339.  
  340.     secondsTimer = os.startTimer(1)
  341. end
  342.  
  343. if (player(playerActions.CurrentlyPlaying)) then
  344.     render()
  345. else
  346.     if pocket then
  347.         term.write("Enter your Access Token: ")
  348.     else
  349.         local monitor = peripheral.find("monitor")
  350.  
  351.         monitor.clear()
  352.         monitor.setCursorPos(math.floor(term.getSize() / 4), math.floor(term.getSize() / 4))
  353.         monitor.write("Enter your Access Token: ")
  354.     end
  355.  
  356.     auth = read("*")
  357. end
Add Comment
Please, Sign In to add comment