-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (76 loc) · 2.66 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
process.env.NODE_ENV = process.env.NODE_ENV || 'dev';
const r = require('dotenv').config({ path: '.env.'+ process.env.NODE_ENV});
if(r.error){
console.log(r);
}
const conf = require('./conf');
const express = require('express');
const bodyParser = require('body-parser');
const alltomp3 = require('alltomp3');
const app = express();
const router = express.Router();
const fs = require('fs-extra')
var path = require('path');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const api = require('./api');
router.post('/suggestion', async (req, res) => {
try {
let q = req.body.query;
let limit = req.body.limit || 5;
res.json(await api.searchDeezerForSongs(q,limit));
} catch (error) {
res.status(500).json(error);
}
})
router.post('/searchTube',async (req,res)=>{
try {
res.json(await api.searchYouTube(req.body.artistName,req.body.title));
} catch (error) {
res.status(500).json(error);
}
})
router.post('/downloadTube',async (req,res)=>{
try {
let d = await api.downloadFromYouTube(req.body.url,__dirname+'/downloads');
if(d.error != ''){
throw d.error;
}
res.sendFile(__dirname+'/'+d.fileName);
} catch (error) {
res.status(500).json(error);
}
})
/**
{
"title": "La Isla Bonita",
"artistName": "Madonna",
"duration": 327,
"cover": "https://e-cdns-images.dzcdn.net/images/cover/43a8e2b1b035391f58b0927cf1040bc4/250x250-000000-80-0-0.jpg",
"deezerId": 678372
}
*/
router.post('/download',async (req,res)=>{
try {
let dir = path.join(conf.downloadDir,req.body.artistName);
let videos = await api.searchYouTube(req.body.artistName,req.body.title);
if(videos.length>0){ //prevent trash dirs
fs.ensureDirSync(dir);
}
for (const video of videos) {
let d = await api.downloadFromYouTube(video.url,dir); //TODO: use caching to prevent duplicate downloads
if(d.error == ''){
return res.sendFile(path.join(dir,d.fileName));
}
}
throw 'nothing could be downloaded :(';
} catch (error) {
res.status(500).json(error);
}
})
app.use('/api', router);
app.set('port', process.env.PORT || 2999);
const server = app.listen(app.get('port'), () => {
//logger.info('running in %s mode \n %j', process.env.NODE_ENV,conf);
console.log(`Express running → PORT ${server.address().port} node:${process.versions.node}`);
});