Skip to content

Instantly share code, notes, and snippets.

@mallendeo
Last active November 16, 2017 15:50
Show Gist options
  • Save mallendeo/34b5c24dd3e838aad890 to your computer and use it in GitHub Desktop.
Save mallendeo/34b5c24dd3e838aad890 to your computer and use it in GitHub Desktop.
Spotify API for built-in server
'use strict'
const axios = require('axios')
const PORT = 4380
const DEFAULT_RETURN_ON = ['login', 'logout', 'play', 'pause', 'error', 'ap']
// Must use capitalized header
const ORIGIN_HEADER = { 'Origin': 'https://open.spotify.com' }
// ------------------
// Helpers
// ------------------
const getJson = async (url, params = {}, headers = {}) => {
const { data } = await axios.get(url, { params, headers })
return data
}
const url = uri => `http://localhost:${PORT}${uri}`
const spotilocal = async () => {
const getCsrfToken = async () => {
const { token } = await getJson(url('/simplecsrf/token.json'), null, ORIGIN_HEADER)
return token
}
const getOauthToken = async () => {
const { t } = await getJson('https://open.spotify.com/token')
return t
}
const getTokens = async () => {
const [csrfToken, oauthToken] = await Promise.all([
getCsrfToken(),
getOauthToken()
])
return { csrfToken, oauthToken }
}
const getVersion = () =>
getJson(
url('/service/version.json'),
{ service: 'remote' },
ORIGIN_HEADER
)
const openSpotifyClient = async () =>
getJson(url('/remote/open.json'), null, ORIGIN_HEADER)
await openSpotifyClient()
const { csrfToken, oauthToken } = await getTokens()
const getStatus = async (returnAfter = 59, returnOn = DEFAULT_RETURN_ON) =>
getJson(url('/remote/status.json'), {
oauth: oauthToken,
csrf: csrfToken,
returnafter: returnAfter,
returnon: returnOn.join(',')
}, ORIGIN_HEADER)
const pause = async (pause = true) =>
getJson(url('/remote/pause.json'), {
oauth: oauthToken,
csrf: csrfToken,
pause
}, ORIGIN_HEADER)
const unpause = async pause => pause(false)
const play = async spotifyUri =>
getJson(url('/remote/play.json'), {
'oauth': oauthToken,
'csrf': csrfToken,
'uri': spotifyUri,
'context': spotifyUri
}, ORIGIN_HEADER)
return {
getVersion,
getStatus,
openSpotifyClient,
pause,
unpause,
play
}
}
module.exports = spotilocal
// ------------------
// Example
// ------------------
const onLoad = async spotify => {
let paused = false
false && setInterval(() => {
paused = !paused
spotify.pause(paused)
}, 150)
const songs = [
'spotify:track:5nAFaDOTL6fkvtlklzviHD',
'spotify:track:7CpHzlFSAAFegZveUFNHzH',
'spotify:track:6TUPCXGsj3HQAKn29KcrL4',
'spotify:track:0t2myRMEMCi2SRyg5TEFLp',
'spotify:track:6sqo5lYZ3yJRv0auIWBNrm',
'spotify:track:2w7pHPE7Ak0zc4keLZ2clK',
'spotify:track:0Z21JPecgmeXFYTzxgdmPq'
]
const playSongs = async (index = 0, timeout = 2000) => {
if (!songs[index]) index = 0
try {
const { track } = await spotify.play(songs[index])
console.log(track.track_resource.name)
} catch (e) {
console.error('ERROR:', e.message)
} finally {
setTimeout(() => playSongs(++index), timeout)
}
}
playSongs()
process.on('SIGINT', async () => {
await spotify.pause()
process.exit()
})
}
;(async () => {
const spotify = await spotilocal()
onLoad(spotify)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment