-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (113 loc) · 3.56 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
const express = require('express');
const bodyParser = require('body-parser');
const NodeID3 = require('node-id3');
const SoundCloud = require("soundcloud-scraper");
const client = new SoundCloud.Client();
const fs = require("fs");
const path = require('path');
const request = require('request'); //pobieranie jpg
const app = express();
const port = 3000;
//set templating engine
app.set('view engine', 'ejs');
//dodanie folderu publicznego z js, css itd.
app.use("/public", express.static('public'));
const urlencodedParser = bodyParser.urlencoded({ extended: false });
async function getSongs(arrSongs, arrSourcelinks) {
for await (const [i, sourcelink] of arrSourcelinks.entries()) {
await client.getSongInfo(sourcelink)
.then(async songInfo => {
await arrSongs.push(
{
artist: songInfo.author.name,
title: songInfo.title,
album: songInfo.author.url,
composer: "",
genre: "",
subtitle: songInfo.url,
image: songInfo.thumbnail,
})
console.log(i+1 + "/" + arrSourcelinks.length)
})
.catch(console.error);
}
}
async function updateSongsTags(arrSongs, arrEdited) {
if (typeof arrEdited.artist === "array") {
for await (const [i, song] of arrSongs.entries()) {
song.artist = arrEdited.artist[i];
song.title = arrEdited.title[i];
song.album = arrEdited.soundcloud[i];
song.composer = arrEdited.spotify[i];
song.genre = arrEdited.youtube[i];
song.subtitle = arrEdited.sourcelink[i];
}
}
else {
arrSongs.artist = arrEdited.artist;
arrSongs.title = arrEdited.title;
arrSongs.album = arrEdited.soundcloud;
arrSongs.composer = arrEdited.spotify;
arrSongs.genre = arrEdited.youtube;
arrSongs.subtitle = arrEdited.sourcelink;
}
}
let songsTags = new Array();
//get
app.get('/', (req, res) => {
res.render('index');
});
app.get('/queue', (req, res) => {
res.render('queue');
});
let sourcelinks;
//post
app.post('/queue', urlencodedParser, (req, res) => {
if(typeof req.body.sourcelink === "string") {
sourcelinks = new Array(req.body.sourcelink);
}
else {
sourcelinks = req.body.sourcelink;
}
async function rend() {
await getSongs(songsTags, sourcelinks);
res.render('queue', {songs: songsTags});
}
rend();
});
async function downloadSong(arrSourcelinks) {
for await (const [i, sourcelink] of arrSourcelinks.entries()) {
client.getSongInfo(sourcelink)
.then(async songInfo => {
const filepath = `${songsTags[i].artist} - ${songsTags[i].title}`
const stream = await songInfo.downloadProgressive();
const writer = stream.pipe(fs.createWriteStream("./music/" + filepath + ".mp3"));
writer.on("finish", () => {
console.log("save song number: " + (i+1))
download(songsTags[i].image, "./music/temp/" + filepath + ".jpg", () => {
songsTags[i].image = "./music/temp/" + filepath + ".jpg";
NodeID3.write(songsTags[i], "./music/" + filepath + ".mp3", function(err) { }); //wywolanie funkcji zapisu z node-id3
console.log("tags song: " + (i+1))
})
});
})
.catch(console.error);
}
}
const download = (url, path, callback) => {
request.head(url, (err, res, body) => {
request(url)
.pipe(fs.createWriteStream(path))
.on("close", callback)
})
}
app.post('/download', urlencodedParser, (req, res) => {
async function rend() {
await updateSongsTags(songsTags, req.body)
await downloadSong(sourcelinks);
res.json(req.body)
console.log("complete!");
}
rend();
});
app.listen(port);