Skip to content

Instantly share code, notes, and snippets.

@MatthewStanciu
Created September 8, 2018 04:13
Show Gist options
  • Save MatthewStanciu/293f0ce383db124fa308760f6297c59b to your computer and use it in GitHub Desktop.
Save MatthewStanciu/293f0ce383db124fa308760f6297c59b to your computer and use it in GitHub Desktop.
Collaborative Spotify Playlist
var express = require('express');
var app = express();
var http = require('http').Server(app);
var bodyParser = require('body-parser');
var request = require('request');
var refresh = require('spotify-refresh');
require('dotenv').config();
var redirect_uri = 'http://localhost:3000/callback'
function getIDfromUrl(url) {
var split1 = url.split('/');
var split2 = split1[4].split('?');
return split2[0];
}
app.use('/public', express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post('/song', function(req, res) {
refresh(process.env.REFRESH_TOKEN, process.env.CLIENT_ID, process.env.CLIENT_SECRET, function(err, res, body) {
if (err) return err;
request.post({
url: 'https://api.spotify.com/v1/playlists/your-playlist-id/tracks?uris=spotify%3Atrack%3A' +
getIDfromUrl(req.body.submiturl),
headers: {
'Authorization': 'Bearer ' + body.access_token,
'Host': 'api.spotify.com',
'Accept': 'application/json',
'Content-Type': 'application/json'
},
json: true
});
});
console.log("added " + req.body.submiturl + " to the playlist");
res.redirect("/added");
})
app.get("/", function(err, res) {
res.sendFile(__dirname + "/index.html");
})
app.get("/added", function(err, res) {
res.sendFile(__dirname + "/added.html");
})
http.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment