forked from apumaparna/pennapps-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
132 lines (113 loc) · 3.82 KB
/
server.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// server.js
// where your node app starts
// init project
var express = require("express");
var app = express();
var lyrics = require('genius-lyrics-api');
//import { getLyrics, getSong } from 'genius-lyrics-api';
var Lyricist = require('lyricist');
const lyricist = new Lyricist(process.env.GENIUS_TOKEN)
// http://expressjs.com/en/starter/static-files.html
app.use(express.static("public"));
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname,'public','javascript')));
// app.use(express.static(path.join(__dirname,"public")))
// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function(request, response) {
response.sendFile(__dirname + "/index.html");
});
//-------------------------------------------------------------//
// init Spotify API wrapper
var SpotifyWebApi = require("spotify-web-api-node");
// Replace with your redirect URI, required scopes, and show_dialog preference
var redirectUri = `https://${process.env.PROJECT_DOMAIN}.glitch.me/callback`;
var scopes = ["user-top-read"];
var showDialog = true;
// The API object we'll use to interact with the API
var spotifyApi = new SpotifyWebApi({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
redirectUri: redirectUri
});
app.get("/authorize", function(request, response) {
var authorizeURL = spotifyApi.createAuthorizeURL(scopes, null, showDialog);
console.log(authorizeURL);
response.send(authorizeURL);
});
// Exchange Authorization Code for an Access Token
app.get("/callback", function(request, response) {
var authorizationCode = request.query.code;
spotifyApi.authorizationCodeGrant(authorizationCode).then(
function(data) {
console.log(data);
response.redirect(
`/#access_token=${data.body["access_token"]}&refresh_token=${
data.body["refresh_token"]
}`
);
},
function(err) {
console.log(
"Something went wrong when retrieving the access token!",
err.message
);
}
);
});
app.get("/logout", function(request, response) {
response.redirect("/");
});
app.get("/myendpoint", function(request, response) {
var loggedInSpotifyApi = new SpotifyWebApi();
// console.log("enpoint");
// console.log("request");
console.log(request.headers["authorization"].split(" ")[1]);
loggedInSpotifyApi.setAccessToken(
request.headers["authorization"].split(" ")[1]
);
// Search for a track!
loggedInSpotifyApi.getMyTopTracks().then(
//where is the second )??
function(data) {
console.log(data.body);
response.send(data.body);
},
function(err) {
console.error(err);
}
);
});
//attempting to get the audio features of a track; loop thru each song in toptracks, run this get audio features?
//then add to the list of objects?
app.get("/features", function(request, response) {
console.log("server");
// console.log(request);
console.log(request.query.id);
var loggedInSpotifyApi = new SpotifyWebApi();
// console.log("enpoint");
// console.log("request");
console.log(request.headers["authorization"].split(" ")[1]);
loggedInSpotifyApi.setAccessToken(
request.headers["authorization"].split(" ")[1]
);
loggedInSpotifyApi
.getAudioFeaturesForTrack(request.query.id)
//need to get comma separated list of spotify ids for tracks, max 100
.then(
function(data) {
// console.log("server request");
console.log(data.body);
response.send(data.body);
},
function(err) {
console.log("server request");
console.log(err);
}
);
});
//-------------------------------------------------------------//
// listen for requests :)
var listener = app.listen(process.env.PORT, function() {
console.log("Your app is listening on port " + listener.address().port);
});