-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.js
More file actions
109 lines (86 loc) · 2.79 KB
/
Copy pathserver.js
File metadata and controls
109 lines (86 loc) · 2.79 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
require("dotenv").config();
const express = require("express");
const cors = require("cors");
var port = process.env.PORT || 3001;
const bodyParser = require("body-parser");
const lyricsFinder = require("lyrics-finder");
const ytdl = require("ytdl-core");
const YoutubeMusicApi = require("youtube-music-api");
const SpotifyWebApi = require("spotify-web-api-node");
const SpotifyToYoutube = require("spotify-to-youtube");
const app = express();
app.use(cors());
/* Sets the default json spaces to 4, which will beautify the JSON response sent through .json() method. */
app.set('json spaces', 4);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const spotifyApi = new SpotifyWebApi({
redirectUri: process.env.REDIRECT_URI,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
});
app.post("/refresh", (req, res) => {
const refreshToken = req.body.refreshToken;
// clientId, clientSecret and refreshToken has been set on the api object previous to this call.
spotifyApi
.refreshAccessToken()
.then((data) => {
res.json({
accessToken: data.body.accessToken,
expiresIn: data.body.expiresIn,
});
// Save the access token so that it's used in future calls
spotifyApi.setAccessToken(data.body["access_token"]);
})
.catch((err) => {
console.log(err);
res.sendStatus(400);
});
});
app.post("/login", (req, res) => {
const code = req.body.code;
spotifyApi
.authorizationCodeGrant(code)
.then((data) => {
res.json({
accessToken: data.body.access_token,
refreshToken: data.body.refresh_token,
expiresIn: data.body.expires_in,
});
})
.catch((err) => {
res.sendStatus(400);
console.log(err);
});
});
app.get("/lyrics", async (req, res) => {
const lyrics =
(await lyricsFinder(req.query.artist, req.query.track)) ||
"No Lyrics Found! :( ";
res.json({ lyrics });
});
app.post("/song", async (req, res) => {
const { song } = req.body;
const api = new YoutubeMusicApi();
async function getStreamUrl(songId) {
let info = await ytdl.getInfo(songId);
let format = ytdl.chooseFormat(info.formats, { quality: "140" });
// console.log(format.url);
return format.url;
}
await api.initalize();
const result = await api.search(
`${song.title} ${song.artist}, ' topic ' `
);
const videoId = result.content[0].videoId;
const playerUri = await getStreamUrl(videoId);
res.status(200).send(playerUri);
});
// app.get("/song", async (req, res) => {
// const spotifyToYoutube = SpotifyToYoutube(spotifyApi);
// const id = await spotifyToYoutube(req.query.trackUri);
// console.log(id); // J7_bMdYfSws
// });
app.listen(port, () => {
console.log(`LogiMusic Server up on Port ${port} Enjoy! :) `);
});