-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
193 lines (174 loc) · 4.91 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import {
processPlaylist,
makePlaylist,
getPlaylistItems,
getPlaylistName,
getUser,
generateRandomString,
getAccessToken,
} from "./functions.js";
import axios from "axios";
import express from "express";
import queryString from "query-string";
import cookieSession from "cookie-session";
import "dotenv/config";
//Get from .env file
const PORT = process.env.PORT || 3000;
const clientId = process.env.CLIENT_ID;
const clientSecret = process.env.CLIENT_SECRET;
const rapidApiKey = process.env.RAPID_API_KEY;
const rapidApiHost = process.env.RAPID_API_HOST;
const key1 = process.env.COOKIE_KEY1;
const key2 = process.env.COOKIE_KEY2;
const redirectUri = process.env.REDIRECT_URI;
//Configure app
const app = express();
app.set("view engine", "ejs");
//Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static("styles"));
app.use(
cookieSession({
name: "session",
keys: [key1, key2],
maxAge: 60 * 60 * 1000, // one hour
})
);
app.use("/home", function (req, res, next) {
// console.log(req.session);
if (
req.session.access_token === null ||
req.session.access_token === undefined
) {
return res.render("pages/login.ejs");
} else {
next();
}
});
//Routes
app.get("/login", function (req, res) {
// console.log("for /login- ");
// console.log(req.session);
let state = generateRandomString(16);
let scope =
"user-read-private user-read-email playlist-read-private ugc-image-upload playlist-modify-private playlist-modify-public";
res.redirect(
"https://accounts.spotify.com/authorize?" +
queryString.stringify({
response_type: "code",
client_id: clientId,
scope: scope,
redirect_uri: redirectUri,
state: state,
})
);
});
app.get("/callback", async function (req, res) {
// console.log("callback called" + req.session);
let code = req.query.code || null;
let state = req.query.state || null;
if (state === null) {
res.send("state mismatch");
} else {
let access_token = await getAccessToken(
clientId,
clientSecret,
code,
redirectUri
);
// console.log(access_token);
req.session.access_token = access_token;
res.redirect("/home");
}
});
app.get("/", async function (req, res) {
res.render("pages/login.ejs");
});
app.get("/home", async function (req, res) {
console.log("for home - ");
let token = req.session.access_token;
let user = await getUser(token);
return res.render("pages/index.ejs", { user: user });
});
app.get("/logout", function (req, res) {
req.session.access_token = null;
req.session = null;
res.redirect("/");
});
app.get("/error", function (req, res) {
return res.send("error");
});
app.post("/make-playlist", async function (req, res) {
// console.log("for mp- " + req.session);
try {
if (req.session.access_token === null) {
return res.redirect("/login");
}
const link = req.body.link;
let token = req.session.access_token;
const reg = /.*(?:youtube|youtu\.be)\.com\/.*list=([A-Za-z0-9_-]*)/gm;
const playlistId = reg.exec(link)[1];
let name = await getPlaylistName(playlistId, rapidApiKey, rapidApiHost);
if (name === null) name = "My crossify playlist";
const playlistItems = await getPlaylistItems(
playlistId,
rapidApiKey,
rapidApiHost
);
if (!playlistItems) {
return res.send("Cannot get playlist items");
}
let user = await getUser(token);
const spotifyURI = await processPlaylist(token, playlistItems);
const spotifyPlaylist = await makePlaylist(token, user, playlistId, name);
if (!spotifyPlaylist) {
return res.render("pages/playlist.ejs", {
status: false,
});
}
async function addItemsToPlaylist(spotifyPlaylist, spotifyURI) {
try {
const response = await axios.post(
"https://api.spotify.com/v1/playlists/" +
spotifyPlaylist.id +
"/tracks",
{ uris: spotifyURI, position: 0 },
{
headers: {
Authorization: "Bearer " + token,
},
}
);
return response;
} catch (error) {
throw error;
}
}
const addItems = await addItemsToPlaylist(spotifyPlaylist, spotifyURI);
if (addItems.status === 201) {
return res.render("pages/playlist.ejs", {
status: true,
user: user,
playlist_url: spotifyPlaylist.external_urls.spotify,
playlist_name: name,
});
} else {
// console.log(addItems);
return res.render("pages/playlist.ejs", {
status: false,
user: user,
playlist_name: "Failed to make playlist",
});
}
} catch (error) {
console.log(error);
return res.render("pages/playlist.ejs", {
status: false,
playlist_name: "Failed to make playlist",
});
}
});
app.listen(PORT, () => {
console.log("server started on port " + PORT);
});