Last active
February 11, 2019 02:05
-
-
Save ashleyng/4c5985be7edf504bac9bb60cd8de5683 to your computer and use it in GitHub Desktop.
Spotify of Things: Use IoT button to add songs to Spotify playlists using AWS Lambdas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https') | |
module.exports = { | |
request : function() { | |
return new Promise(function(resolve, reject) { | |
var options = { | |
host: 'api.spotify.com', | |
path: '/v1/me/player/currently-playing', | |
method: 'GET', | |
headers: { | |
'Authorization': 'Bearer ' + process.env.ACCESS_TOKEN | |
} | |
} | |
const req = https.request(options, (res) => { | |
let body = ''; | |
// console.log('Status:', res.statusCode); | |
// console.log('Headers:', JSON.stringify(res.headers)); | |
res.setEncoding('utf8'); | |
res.on('data', (chunk) => body += chunk); | |
res.on('end', () => { | |
// console.log('Successfully processed HTTPS response'); | |
body = JSON.parse(body); | |
resolve(body.item.uri); | |
}); | |
}); | |
req.on('error', function(err) { | |
reject(err) | |
}); | |
req.end(); | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const refreshToken= require('./RefreshToken') | |
const getPlayingSong = require('./GetPlayingSong') | |
const postToPlaylist = require('./PostToPlaylist') | |
exports.handler = (event, context, callback) => { | |
refreshToken.request() | |
.then(function(body) { | |
process.env.ACCESS_TOKEN = body.access_token | |
return getPlayingSong.request() | |
}) | |
.then(function(body) { | |
let playlist = event.clickType === 'SINGLE' ? process.env.LISTEN_TO_LATER_ID : process.env.NEW_PLAYLIST_ID | |
return postToPlaylist.request(playlist, body) | |
}) | |
.then(function(body) { | |
callback(null, body) | |
}) | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https') | |
module.exports = { | |
request : function(playlistId, spotifyUris) { | |
return new Promise(function(resolve, reject) { | |
var options = { | |
host: 'api.spotify.com', | |
path: '/v1/playlists/' + playlistId + '/tracks', | |
method: 'POST', | |
headers: { | |
'Authorization': 'Bearer ' + process.env.ACCESS_TOKEN | |
} | |
} | |
var reqBody = JSON.stringify({ | |
uris: [spotifyUris] | |
}) | |
const req = https.request(options, (res) => { | |
let body = ''; | |
// console.log('Status:', res.statusCode); | |
// console.log('Headers:', JSON.stringify(res.headers)); | |
res.setEncoding('utf8'); | |
res.on('data', (chunk) => body += chunk); | |
res.on('end', () => { | |
// console.log('Successfully processed HTTPS response'); | |
// If we know it's JSON, parse it | |
if (res.headers['content-type'] === 'application/json') { | |
body = JSON.parse(body); | |
} | |
resolve(body); | |
}); | |
}); | |
req.on('error', function(err) { | |
reject(err) | |
}); | |
req.write(reqBody); | |
req.end(); | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https') | |
module.exports = { | |
request : function() { | |
return new Promise(function(resolve, reject) { | |
var options = { | |
host: 'accounts.spotify.com', | |
path: '/api/token', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Authorization': process.env.ENCODED_CLIENT | |
} | |
} | |
var reqBody = 'grant_type=refresh_token&refresh_token=' + process.env.REFRESH_TOKEN | |
const req = https.request(options, (res) => { | |
let body = ''; | |
// console.log('Status:', res.statusCode); | |
// console.log('Headers:', JSON.stringify(res.headers)); | |
res.setEncoding('utf8'); | |
res.on('data', (chunk) => body += chunk); | |
res.on('end', () => { | |
// console.log('Successfully processed HTTPS response'); | |
if (res.headers['content-type'] === 'application/json') { | |
body = JSON.parse(body); | |
} | |
resolve(body); | |
}); | |
}); | |
req.on('error', function(err) { | |
reject(err) | |
}); | |
req.write(reqBody); | |
req.end(); | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment