-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (126 loc) · 4.5 KB
/
index.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
const express = require("express");
const { Translate } = require(`@google-cloud/translate`).v2
const SpotifyWebApi = require('spotify-web-api-node');
const lyricsFinder = require('lyrics-finder');
const bodyParser = require("body-parser");
const cors = require("cors");
const fs = require('fs');
const helmet = require("helmet");
// Import config
require("dotenv").config();
const PORT = process.env.PORT || 8080;
const app = express();
// Your Google Translate Credentials
const CREDENTIALS = JSON.parse(process.env.GOOGLE_APPLICATION_CREDENTIALS);
// Configuration for the client
const translate = new Translate({
credentials: CREDENTIALS,
projectId: CREDENTIALS.project_id
});
// Set Security Configs
app.use(helmet());
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }))
// the function below reads lyrics from the growing request library
const readLyrics = () => {
return JSON.parse(fs.readFileSync("./data/lyrics.json", "utf-8", (err) => {
if (err) {
return;
}
})
);
};
// the function below detects language and translates text from any langugage other than English.
const translateText = async (text) => {
const [detection] = await translate.detect(text);
const detectedLanguage = detection.language;
if (detectedLanguage === 'en') {
return null;
}
try {
const [response] = await translate.translate(text, 'en');
return response;
} catch (err) {
console.log(`Error at translateText --> ${err}`);
return 0;
}
};
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true };
const timing = new Intl.DateTimeFormat('en-US', options);
const timestamp = timing.format(new Date());
// Request to refresh token every 10 minutes
app.post('/refresh', (req, res) => {
const refreshToken = req.body.refreshToken
const spotifyApi = new SpotifyWebApi({
redirectUri: process.env.REDIRECT_URI,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
refreshToken
})
spotifyApi
.refreshAccessToken()
.then(data => {
res.json({
accessToken: data.body.accessToken,
expiresIn: data.body.expiresIn,
})
})
.catch((err) => {
res.sendStatus(400)
})
})
// Spotify login request
app.post('/login', (req, res) => {
const code = req.body.code
const spotifyApi = new SpotifyWebApi({
redirectUri: process.env.REDIRECT_URI,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
})
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)
})
})
// the below get request checks for three items:
// 1) read and post the data from lyrics.json file to the client side.
// 2) if no lyrics found then search the lyrics via lyricfinder and translate lyrics, post both data to client.
// 3) write the lyrics newly searched via lyricfinder and save in lyrics.json file
app.get('/lyrics', async (req, res) => {
const dataLyric = readLyrics();
const findLyrics = dataLyric.find(({ artist, track }) => artist === req.query.artist && track === req.query.track);
const lyrics = await lyricsFinder(req.query.artist, req.query.track) || "Woah! where did you find this song? i am still searching";
const translation = await translateText(lyrics, 'en');
const lyricObject = {
timestamp: timestamp,
artist: req.query.artist,
track: req.query.track,
lyrics: lyrics,
translation: translation
}
if (findLyrics !== undefined) {
return res.json(findLyrics)
} else if (findLyrics === undefined) {
dataLyric.push(lyricObject);
fs.writeFile('./data/lyrics.json', JSON.stringify(dataLyric), (err) => {
if (err) {
console.error(err);
return res.status(500).send("Server Error");
}
return res.json({ "lyrics": lyrics, "translation": translation });
});
}
});
// Server
app.listen(PORT, () => {
console.log(`This app was deployed at timestamp: ${timestamp} on Port #${PORT}`);
});