-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenius.js
117 lines (98 loc) · 3.29 KB
/
genius.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
import http from 'http';
import fs from 'fs';
import qs from 'querystring';
import Genius from 'genius-lyrics';
import Mongo from 'mongodb';
const Client = new Genius.Client("o19mtznUl0mu0kk0DZxZgATNB-2Cw0ihj5ybfCdOpd-pz25oY7A4J5BgGQEoqDGb");
const MongoClient = Mongo.MongoClient;
const url = "mongodb+srv://aliulex:[email protected]/myFirstDatabase?retryWrites=true&w=majority";
var info;
var query;
var lyrics;
var port = process.env.PORT || 3000;
http.createServer(function (req, res)
{
if (req.url == "/")
{
var file = 'index.html';
fs.readFile(file, function(err, txt)
{
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(txt);
res.end();
});
}
else if (req.url == "/lyrics")
{
var file = 'lyrics.html';
fs.readFile(file, function(err, txt)
{
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(txt);
});
info = "";
query = "";
req.on('data', data => {
info += data.toString();
});
req.on('end', async () => {
info = qs.parse(info);
if (info['song'])
{
query = info['song'];
}
const searches = await Client.songs.search(query);
const firstSong = searches[0];
lyrics = await firstSong.lyrics();
res.write("<h2>Lyrics</h2>");
res.write(lyrics);
res.end();
});
}
else if (req.url == "/favorites")
{
var file = 'favorites.html';
fs.readFile(file, function(err, txt)
{
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(txt);
});
// res.writeHead(200, {'Content-Type': 'text/html'});
MongoClient.connect(url, { useUnifiedTopology: true }, function(err, db) {
if(err) { return console.log(err); return;}
var dbo = db.db("favorites");
var collection = dbo.collection('songs');
var myobj = {song: query};
collection.insertOne(myobj, function(err, result) {
if(err) { console.log("query err: " + err); return; }
console.log("new document inserted");
});
collection.find({"song":{$ne:null}}).toArray(function(err, items) {
res.write("<h2>Your Favorite Songs:</h2>");
if(items.length == 0) {
res.write("No favorites");
}
// else if {
// for (let i = 0; i < items.length; i++) {
// for (let j = i + 1; j<items.length; j++) {
// if (items[i].song == items[j].song) {
// dbo.collection('songs').deleteOne(query, function(err, obj) {
// if (err) throw err;
// console.log(db.result.n + " document(s) deleted");
// db.close();
// });
// }
// }
// }
// }
else {
for (let i=0; i<items.length; i++)
{
res.write(items[i].song + "<br><br>");
}
res.end();
}
});
});
}
}).listen(port);