-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
59 lines (54 loc) · 1.71 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
const fetch = require("node-fetch");
const API = require("./constants")
function getID(query,cb){
fetch(API.API_FIRST+query, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}}).then(response =>response.text()).then(response => {
response=response.slice(response.indexOf('{'))
response=JSON.parse(response)
cb(false,response.albums.data)
}).catch(err => {
cb(true,null)
console.error(err)
});
}
function getUrl(id,cb){
fetch(API.API_SECOND+id, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}}).then(response =>response.text()).then(response => {
response=response.slice(response.indexOf('{'))
response=JSON.parse(response)
//console.log(response.songs[0].media_preview_url)
cb(false,response.songs[0].media_preview_url)
}).catch(err => {
cb(true,null)
//console.error(err)
});
}
function getLink(query,cb){
getID(query,(err,data)=>{
if(err){
//console.log(err)
}
else{
//console.log(data[0].id)
getUrl(data[0].id,(err,data)=>{
if(err){
cb(true,null)
}
else{
data = data.replace("preview", "aac");
data = data.replace("96_p.mp4", "320.mp4");
cb(false,data)
}
})
}
})
}
module.exports={
getLink : getLink
}