-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
112 lines (91 loc) · 2.79 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
const fetch = require("node-fetch");
const express = require('express')
const bodyParser = require("body-parser");
const app = express()
var cors = require('cors')
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({
extended: true
}));
var port = process.env.PORT || 3000;
app.use(express.static('public'));
app.use(cors())
// frontend backend connection
app.get("/", (req, res) => {
songs=""
res.render("home",{songs:songs});
});
// app.get("/favicon.ico", (req, res) => {
// songs=""
// res.render("home",{songs:songs});
// });
app.post("/", (req, res) => {
let song = req.body.song;
if (song === "") res.redirect("/");
else res.redirect("/songname/"+song);
})
app.post("/:song", (req, res) => {
let song = req.body.song;
res.redirect("/songname/"+song);
})
app.get("/songname/:song", (req, res) => {
var name = req.params.song;
// console.log(typeof(name))
console.log(name)
grab(name);
async function grab(name){
var songs= await songgrabber(name)
for (let s=0;s<songs.length;s++) {
if(songs[s]!=undefined)
console.log(songs[s].song);}
res.render("home",{songs:songs});
}
async function songgrabber(songname) {
try{
let url = "https://www.jiosaavn.com/api.php?__call=autocomplete.get&_format=json&_marker=0&cc=in&includeMetaTags=1&query=" + songname;
var response = await fetch(url) //RUN RUN RUN
var resdata = await response.json();
var songdata = resdata['songs']['data']
var songids = []
var songs=[]
for (let index = 0; index < songdata.length; index++) {
const id = songdata[index]['id'];
songids.push(id)
}
// console.log(songids)
for(var i=0;i<songids.length;i++){
let song= await singleidurl(songids[i])
if(song!=undefined)
songs.push(song);
}
return songs
}catch (error) {}
}
async function singleidurl(id){
try{
let url = "https://www.jiosaavn.com/api.php?__call=song.getDetails&cc=in&_marker=0%3F_marker%3D0&_format=json&pids=" + id
let response = await fetch(url)
let resdata = await response.json()
return helper(resdata[id])
}catch (error) {}
}
function helper(data){
try{
if('media_preview_url' in data){
var url = data['media_preview_url'];
url = url.replace('preview', 'aac');
}
if(data['320kbps'] == 'true'){
url = url.replace("_96_p.mp4", "_320.mp4")
}
else{
url = url.replace("_96_p.mp4", "_160.mp4")
}
var dict = { 'song': data['song'], 'singers': data['singers'],'thumbnail':data['image'] ,'url': url}
return dict
}catch (error) {}
}
})
app.listen(port, () => {
console.log(`Server Started at http://localhost:${port}`)
})