Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local debug = false
- local auth = "<insert token here>"
- if not fs.exists("json") then
- shell.run("pastebin", "get", "4nRg9CHU", "json")
- end
- os.loadAPI("json")
- if not fs.exists("touchpoint") then
- shell.run("pastebin", "get", "pFHeia96", "touchpoint")
- end
- os.loadAPI("touchpoint")
- local playerActions = {
- ["CurrentlyPlaying"] = {
- url = "",
- method = "GET"
- },
- ["Next"] = {
- url = "/next",
- method = "POST"
- },
- ["Pause"] = {
- url = "/pause",
- method = "PUT"
- },
- ["Play"] = {
- url = "/play",
- method = "PUT"
- },
- ["Previous"] = {
- url = "/previous",
- method = "POST"
- },
- ["Repeat"] = {
- url = "/repeat",
- method = "PUT"
- },
- ["Seek"] = {
- url = "/seek",
- method = "PUT"
- },
- ["Shuffle"] = {
- url = "/shuffle",
- method = "PUT"
- },
- ["Volume"] = {
- url = "/volume",
- method = "PUT"
- }
- }
- local headers = {
- ["Authorization"] = "Bearer "..auth,
- ["Content-Length"] = "0"
- }
- function player(action, query)
- local url = "https://api.spotify.com/v1/me/player"..action.url
- if query then
- url = url.."?"..query
- end
- local request = http.request({
- url = url,
- headers = headers,
- method = action.method
- })
- while true do
- local event, url, request = os.pullEvent()
- if event == "http_success" then
- return request.readAll()
- elseif event == "http_failure" then
- print(url)
- printError(request)
- return false
- end
- end
- end
- function getCurrentArtistAndSong()
- local playback = json.decode(player(playerActions.CurrentlyPlaying))
- if playback then
- return playback.item.album.artists[1].name..": "..playback.item.name
- end
- end
- function getCurrentPlaytime(playback)
- if playback then
- local progress = getMinuteFormat(math.floor(playback.progress_ms / 1000))
- local duration = getMinuteFormat(math.floor(playback.item.duration_ms / 1000))
- return progress.." / "..duration
- end
- return "No Song Playing"
- end
- function getMinuteFormat(seconds)
- local minutes = math.floor(seconds / 60)
- local seconds = seconds % 60
- if seconds < 10 then
- seconds = "0"..seconds
- end
- return minutes..":"..seconds
- end
- function getSecondsRemainingInSong()
- local playback = json.decode(player(playerActions.CurrentlyPlaying))
- if playback then
- return math.ceil((playback.item.duration_ms - playback.progress_ms) / 1000)
- end
- return 0
- end
- function isPlaying()
- local playback = json.decode(player(playerActions.CurrentlyPlaying))
- if playback then
- return playback.is_playing
- end
- return false
- end
- function toggleShuffle()
- local playback = json.decode(player(playerActions.CurrentlyPlaying))
- if playback then
- if debug then
- print("Shuffle: "..toString(playback.shuffle_state).." -> "..toString(not playback.shuffle_state))
- end
- return player(playerActions.Shuffle, "state="..tostring(not playback.shuffle_state))
- end
- end
- function seek(seconds)
- local playback = json.decode(player(playerActions.CurrentlyPlaying))
- if playback then
- local positionToSeek = playback.progress_ms + (seconds * 1000)
- if debug then
- print("Seeking from "..math.floor(playback.progress_ms / 1000).."s to "..positionToSeek.."s")
- end
- return player(playerActions.Seek, "position_ms="..positionToSeek)
- end
- end
- function togglePlay()
- local playback = json.decode(player(playerActions.CurrentlyPlaying))
- if playback then
- if debug then
- print("Play Toggle: "..tostring(playback.is_playing).." -> "..tostring(not playback.is_playing))
- end
- if playback.is_playing then
- return player(playerActions.Pause)
- else
- return player(playerActions.Play)
- end
- end
- end
- --print(getCurrentArtistAndSong())
- --print(getSecondsRemainingInSong())
- --toggleShuffle()
- --seek(-10)
- --togglePlay()
- --player(playerActions.CurrentlyPlaying)
- --player(playerActions.Next)
- --player(playerActions.Pause)
- --player(playerActions.Play)
- --player(playerActions.Previous)
- --player(playerActions.Repeat, "state=true")
- --player(playerActions.Seek, "position_ms=25000")
- --player(playerActions.Shuffle, "state=true")
- --player(playerActions.Volume, "volume_percent=50")
- local currentArtistAndSong
- local currentPlayTime
- local secondsTimer
- local t = touchpoint.new("top")
- function render()
- local size = math.floor(term.getSize() / 2)
- local middle = math.floor(size / 2)
- if pocket then
- t:add("<", nil, middle, middle + 10, middle + 4, middle + 12, colors.gray)
- t:add(">", nil, middle + 6, middle + 10, middle + 10, middle + 12, colors.gray)
- t:add("Play", nil, middle, middle + 6, middle + 16, middle + 8, colors.lime, colors.red)
- t:add("+", nil, middle + 12, middle + 10, middle + 16, middle + 10, colors.gray)
- t:add("-", nil, middle + 12, middle + 12, middle + 16, middle + 12, colors.gray)
- t:add("Click Play!", nil, 1, middle, size * 2, middle, colors.gray)
- t:add("No Song Playing", nil, 1, middle + 1, size * 2, middle + 1, colors.gray)
- else
- local monitor = peripheral.wrap("top")
- monitor.setTextScale(.5)
- monitor.clear()
- t:add("Previous", nil, middle + 4, middle + 2, middle + 16, middle + 4, colors.gray)
- t:add("Next", nil, middle + 18, middle + 2, middle + 30, middle + 4, colors.gray)
- t:add("Play", nil, middle + 32, middle + 2, middle + 44, middle + 4, colors.lime, colors.red)
- t:add("Vol +", nil, 4, middle - 2, 10, middle , colors.gray)
- t:add("Vol -", nil, 4, middle + 2, 10, middle + 4, colors.gray)
- t:add("Click Play!", nil, 4, middle - 8, middle + 44, middle - 4, colors.gray)
- t:add("No Song Playing", nil, middle + 4, middle - 2, middle + 44, middle, colors.gray)
- end
- currentArtistAndSong = "Click Play!"
- currentPlayTime = "No Song Playing"
- if isPlaying() then
- t:toggleButton("Play")
- t:rename("Play", "Pause")
- resetSongInfo()
- end
- t:draw()
- local previous, next, volUp, volDown = "Previous", "Next", "Vol +", "Vol -"
- if pocket then
- previous = "<"
- next = ">"
- volUp = "+"
- volDown = "-"
- end
- while true do
- local event, p1 = t:handleEvents()
- if event == "button_click" then
- if (p1 == "Play") then
- if debug then
- print("Play Clicked")
- end
- togglePlay()
- t:toggleButton(p1)
- t:rename("Play", "Pause")
- resetSongInfo()
- elseif (p1 == "Pause") then
- if debug then
- print("Pause Clicked")
- end
- togglePlay()
- t:toggleButton(p1)
- t:rename("Pause", "Play")
- resetSongInfo()
- elseif (p1 == previous) then
- if debug then
- print("Previous Clicked")
- end
- player(playerActions.Previous)
- resetSongInfo()
- elseif (p1 == next) then
- if debug then
- print("Next Clicked")
- end
- player(playerActions.Next)
- resetSongInfo()
- elseif (p1 == volUp) then
- if debug then
- print("Vol + Clicked")
- end
- local playback = json.decode(player(playerActions.CurrentlyPlaying))
- local playbackVolume = playback.device.volume_percent + 5
- if (playbackVolume > 100) then
- playbackVolume = 100
- end
- player(playerActions.Volume, "volume_percent="..playbackVolume)
- resetSongInfo()
- elseif (p1 == volDown) then
- if debug then
- print("Vol - Clicked")
- end
- local playback = json.decode(player(playerActions.CurrentlyPlaying))
- local playbackVolume = playback.device.volume_percent - 5
- if (playbackVolume < 0) then
- playbackVolume = 0
- end
- player(playerActions.Volume, "volume_percent="..playbackVolume)
- resetSongInfo()
- end
- elseif event == "timer" then
- if p1 == secondsTimer then
- resetSongInfo()
- end
- end
- end
- end
- function resetSongInfo()
- local playback = json.decode(player(playerActions.CurrentlyPlaying))
- local newPlayTime = getCurrentPlaytime(playback)
- t:rename(currentPlayTime, newPlayTime)
- currentPlayTime = newPlayTime
- t:rename(currentArtistAndSong, getCurrentArtistAndSong())
- currentArtistAndSong = getCurrentArtistAndSong()
- secondsTimer = os.startTimer(1)
- end
- if (player(playerActions.CurrentlyPlaying)) then
- render()
- else
- if pocket then
- term.write("Enter your Access Token: ")
- else
- local monitor = peripheral.find("monitor")
- monitor.clear()
- monitor.setCursorPos(math.floor(term.getSize() / 4), math.floor(term.getSize() / 4))
- monitor.write("Enter your Access Token: ")
- end
- auth = read("*")
- end
Add Comment
Please, Sign In to add comment