-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrouter.js
163 lines (145 loc) · 5.04 KB
/
router.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
const express = require('express');
const fs = require("fs");
const router = express.Router();
const search = require('./search');
// Define a route for the home page
router.get('/', (req, res) => {
const htmlData = readHtml('./html/welcome.html');
res.send(htmlData);
});
router.post('/search', (req, res) => {
const { title, name } = req.body;
search(title).then(data => {
const result = data.map(item => {
const titulo = item.title;
const id = item.id.videoId;
return '<button data-title="' + titulo + '" data-name="' + name + '" id="' + id + '" onclick="add(event)"> ' +
titulo +
'</button>';
});
let htmlData = readHtml('./html/searchresults.html');
htmlData = htmlData.replace('{{videos}}', result.join('<br>'));
htmlData = htmlData.replace('{{singer}}', name);
res.send(htmlData);
})
});
router.post('/lista-canciones', (req, res) => {
const lista = readList();
res.send(lista);
});
router.get('/play', (req, res) => {
const { t } = req.query;
// si te no esta definida, debe volver a cargar /play pero pasando el parametro t con un timestamp
if (!t) {
res.redirect('/play?t=' + new Date().getTime());
return;
}
const { id, title, singer } = nextSong();
if (!id) {
let htmlData = readHtml('./html/no-song-to-play.html');
res.send(htmlData);
return;
}
let htmlData = readHtml('./html/player.html');
// sustituir el id del video en la url de youtube
htmlData = htmlData.replace('{{video_id}}', id);
htmlData = htmlData.replace(/{{textToSpeak}}/g, 'Cantante: ' + singer + '. Canción: ' + title);
res.send(htmlData);
});
router.post('/add', (req, res) => {
console.log('get add', req.body);
const resultWrite = addToList(req.body.name, req.body.video_id, req.body.title);
if (resultWrite) {
const result = { 'ok': true, 'reason': 'video added to list' };
res.send(JSON.stringify(result));
} else {
const result = { 'ok': false, 'reason': 'error adding video to list' };
res.send(JSON.stringify(result));
}
});
router.get('/canciones-pedidas', (req, res) => {
let htmlData = readHtml('./html/canciones-pedidas.html');
let result = "";
const list = readList();
list.forEach((item, indice) => {
if (item === '') return;
i = item.split('\t');
result += `<div class="cancion" id="div-cancion_${indice}">
<p style="padding-top:0"><strong><u> Cantante:</u> </strong>
<button class="btn-delete-song" onclick="eliminarCancion(${indice})" id="btn-delete-song_${indice}">x</button><br>${i[1]}
</p>
<p style="margin-top: 0; padding-top:0"><strong><u>Canción:</u></strong><br>${i[3]}</p>
</div>`;
});
htmlData = htmlData.replace('{{canciones_pedidas}}', result);
res.send(htmlData);
});
router.post('/delsong', (req, res) => {
const cancion = req.body;
try {
const list = readList();
list.splice(cancion.index, 1);
fs.writeFileSync('list.txt', list.join('\n'));
res.status(200).send({ message: 'Canción eliminada exitosamente.' });
} catch (error) {
console.log('error Delete song', error);
return null;
}
});
router.get('/next', (req, res) => {
const result = nextSong();
res.header('Content-Type', 'application/json')
res.send(result);
});
const readHtml = (htmlFileName) => {
const css = fs.readFileSync('./html/styles.css', 'utf8');
const menu = fs.readFileSync('./html/menu.html', 'utf8');
let data = fs.readFileSync(htmlFileName, 'utf8');
data = data.replace('{{stylesheet}}', css);
data = data.replace('{{menu}}', menu);
return data;
};
const addToList = (name, video_id, titulo) => {
const date = new Date();
try {
fs.writeFileSync('list.txt', date.toISOString() + '\t' + name + '\t' + video_id + '\t' + titulo + '\n',
{
flag: 'a',
encoding: 'utf8'
}
);
return true
} catch (error) {
console.log('error', error);
return false;
}
};
const readList = () => {
try {
const data = fs.readFileSync('list.txt', 'utf8').split('\n');
return data;
} catch (error) {
console.log('error', error);
return [];
}
};
/**
* devuelve el primer id de la lista de reproducción y borra la linea del fichero list.txt
*/
const nextSong = () => {
try {
const list = readList();
if (list.length === 0) return null;
const nextSong = list.shift();
const id = nextSong.split('\t')[2];
const title = nextSong.split('\t')[3];
const singer = nextSong.split('\t')[1];
fs.writeFileSync('list.txt', list.join('\n'));
console.log('nextSong', id)
return { id, title, singer };
} catch (error) {
console.log('error nextSong', error);
return null;
}
};
module.exports = router;