-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
91 lines (82 loc) · 3.15 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var dotenv = require('dotenv');
var SpotifyWebApi = require('spotify-web-api-node');
dotenv.load();
var spotifyApi = new SpotifyWebApi({
clientId : process.env.SPOTIFY_KEY,
clientSecret : process.env.SPOTIFY_SECRET,
redirectUri : process.env.SPOTIFY_REDIRECT_URI
});
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/', function(req, res) {
if (spotifyApi.getAccessToken()) {
res.redirect('' + process.env.BRICKHACK_SLACK_URI + '');
}
return res.send('<a href="/authorise">Authorise</a>');
});
app.get('/authorise', function(req, res) {
var scopes = ['playlist-modify-public', 'playlist-modify-private'];
var state = new Date().getTime();
var authoriseURL = spotifyApi.createAuthorizeURL(scopes, state);
res.redirect(authoriseURL);
});
app.get('/callback', function(req, res) {
spotifyApi.authorizationCodeGrant(req.query.code)
.then(function(data) {
spotifyApi.setAccessToken(data.body['access_token']);
spotifyApi.setRefreshToken(data.body['refresh_token']);
return res.redirect('/');
}, function(err) {
return res.send(err);
});
});
app.use('/store', function(req, res, next) {
if (req.body.token !== process.env.SLACK_TOKEN) {
return res.status(500).send('Cross site request forgerizzle!');
}
next();
});
app.post('/store', function(req, res) {
spotifyApi.refreshAccessToken()
.then(function(data) {
spotifyApi.setAccessToken(data.body['access_token']);
if (data.body['refresh_token']) {
spotifyApi.setRefreshToken(data.body['refresh_token']);
}
if (req.body.text.trim().length === 0 || req.body.text.trim() === 'help') {
return res.send('Enter the name of a song and the name of the artist, separated by a "-"\nExample: Blue (Da Ba Dee) - Eiffel 65');
}
if (req.body.text.indexOf(' - ') === -1) {
var query = 'track:' + req.body.text;
} else {
var pieces = req.body.text.split(' - ');
var query = 'artist:' + pieces[0].trim() + ' track:' + pieces[1].trim();
}
spotifyApi.searchTracks(query)
.then(function(data) {
var results = data.body.tracks.items;
if (results.length === 0) {
return res.send('Could not find that track.');
}
var track = results[0];
spotifyApi.addTracksToPlaylist(process.env.SPOTIFY_USERNAME, process.env.SPOTIFY_PLAYLIST_ID, ['spotify:track:' + track.id])
.then(function(data) {
return res.send({'response_type': 'in_channel','text': 'Track added: *<' + track.name + '>* by *' + track.artists[0].name + '*'});
}, function(err) {
return res.send(err.message);
});
}, function(err) {
return res.send(err.message);
});
}, function(err) {
return res.send('Could not refresh access token. You probably need to re-authorise yourself from your app\'s homepage.');
});
});
app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port'));