-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
139 lines (117 loc) · 4.34 KB
/
app.js
File metadata and controls
139 lines (117 loc) · 4.34 KB
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
133
134
135
136
137
138
139
require('dotenv').config()
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const SpotifyWebApi = require('spotify-web-api-node');
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static("public"));
const scopes = [
'user-top-read',
];
let token = undefined;
let username = "";
var spotifyApi = new SpotifyWebApi({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
redirectUri: "https://crescendo-music.onrender.com/callback"
});
app.get("/", function (req, res) {
res.render("home");
})
app.get('/login', function (req, res) {
console.log(spotifyApi.createAuthorizeURL(scopes));
res.redirect(spotifyApi.createAuthorizeURL(scopes));
});
app.get("/callback", function (req, res) {
const error = req.query.error;
const code = req.query.code;
const state = req.query.state;
if (error) {
console.error('Callback Error:', error);
res.send(`Callback Error: ${error}`);
return;
}
spotifyApi
.authorizationCodeGrant(code)
.then(data => {
const access_token = data.body['access_token'];
const refresh_token = data.body['refresh_token'];
const expires_in = data.body['expires_in'];
spotifyApi.setAccessToken(access_token);
spotifyApi.setRefreshToken(refresh_token);
console.log('access_token:', access_token);
token = access_token;
console.log('refresh_token:', refresh_token);
console.log(
`Sucessfully retreived access token. Expires in ${expires_in} s.`
);
spotifyApi.setAccessToken(token);
res.redirect("/warrant");
setInterval(async () => {
const data = await spotifyApi.refreshAccessToken();
const access_token = data.body['access_token'];
console.log('The access token has been refreshed!');
console.log('access_token:', access_token);
spotifyApi.setAccessToken(access_token);
}, expires_in / 2 * 1000);
})
.catch(error => {
console.error('Error getting Tokens:', error);
res.send(`Error getting Tokens: ${error}`);
});
});
app.get("/warrant", function (req, res) {
if (!token) {
res.redirect("/");
}
const spotifyApi = new SpotifyWebApi();
spotifyApi.setAccessToken(token);
(async () => {
const me = await spotifyApi.getMe();
const user_id = me.body.id;
username = me.body.display_name;
console.log(me);
console.log(user_id);
})().catch(e => {
console.error(e);
});
res.render("options");
});
app.get("/artists", function (req, res) {
if (!token) {
res.redirect("/");
}
spotifyApi.getMyTopArtists()
.then(function (data) {
let topArtists = data.body.items;
console.log(new Date().getTime().toLocaleString('en-US', {}));
let time = new Date().toLocaleString('en-in', { hour: '2-digit', minute: 'numeric', hour12: true })
let date = new Date().toLocaleString('en-in', { month: 'short', day: 'numeric' })
let year = new Date().toLocaleString('en-in', { year: 'numeric' })
res.render("artists", { artists: topArtists, displayName: username, time, date, year });
}, function (err) {
console.log('Something went wrong!', err);
});
});
app.get("/tracks", function (req, res) {
if (!token) {
res.redirect("/");
}
spotifyApi.getMyTopTracks({ time_range: 'medium_term', limit: 4 })
.then(function (data) {
let topTracks = data.body.items;
let time = new Date().toLocaleString('en-in', { hour: '2-digit', minute: 'numeric', hour12: true })
let date = new Date().toLocaleString('en-in', { month: 'short', day: 'numeric' })
let year = new Date().toLocaleString('en-in', { year: 'numeric' })
res.render("tracks", { songsList: topTracks, displayName: username, time, date, year });
}, function (err) {
console.log('Something went wrong!', err);
});
});
app.listen(process.env.PORT, function () {
console.log("Server started at port 3000");
});