-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
116 lines (109 loc) · 3.07 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
const express = require("express");
const axios = require("axios");
const app = express();
const lyrics_api = require("genius-lyrics-api");
const cors = require("cors");
const e = require("express");
const PORT = process.env.PORT || 3000;
const nq_converter = require("node-quill-converter");
const qs = require("qs");
const https = require("https");
require("dotenv").config();
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
axios.create({
httpsAgent: new https.Agent({ keepAlive: true }),
});
app.get("/", (req, res) => {
res.render("index", { data: [] });
});
app.post("/", async (req, res) => {
const search_input = req.body.search_input;
const results = await axios.get(
encodeURI(`https://rhymepedia-api.herokuapp.com/api/search/${search_input}`)
);
const data = results.data;
res.render("index", { data: data });
});
app.get("/view/:title/:artist", async (req, res) => {
const title = req.params.title;
const artist = req.params.artist;
const result = await axios.get(
encodeURI(
`https://rhymepedia-api.herokuapp.com/api/song/${title}/${artist}`
)
);
if (result.data.status === 0) {
return res.render("view", {
content: "No one has annotated the lyrics of this song. Be the first!",
title: title,
artist: artist,
status: 0,
});
} else {
const song = result.data.song[0];
return res.render("view", {
title: song.title,
artist: song.artist,
lyrics: song.lyrics,
status: 1,
});
}
});
app.get("/create/:title/:artist", async (req, res) => {
const title = req.params.title;
const artist = req.params.artist;
const result = await axios.get(
encodeURI(
`https://rhymepedia-api.herokuapp.com/api/song/${title}/${artist}`
)
);
if (result.data.status === 0) {
const options = {
apiKey: process.env.TOKEN,
title: req.params.title,
artist: req.params.artist,
optimizeQuery: true,
};
try {
const lyrics = await lyrics_api.getSong(options);
res.render("create", {
title: title,
artist: artist,
lyrics: JSON.stringify(
nq_converter.convertTextToDelta(lyrics.lyrics).ops
),
});
} catch (error) {
console.log(error);
}
} else {
const song = result.data.song[0];
return res.render("create", {
title: song.title,
artist: song.artist,
lyrics: song.lyrics,
status: 1,
});
}
});
app.post("/create/:title/:artist", async (req, res) => {
const title = req.params.title;
const artist = req.params.artist;
const lyrics = req.body.lyrics;
const lyrics_text = req.body.lyrics_text;
const data = {
title: title,
artist: artist,
lyrics: lyrics,
lyrics_text: lyrics_text,
};
axios
.post("https://rhymepedia-api.herokuapp.com/api/create", data)
.then((result) => res.redirect(`/view/${title}/${artist}`))
.catch((err) => console.log(err));
});
app.listen(PORT, console.log(`Listening on port ${PORT}`));