forked from theseeker99/WhatsBot-by-Seeker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsong.js
98 lines (88 loc) · 3.17 KB
/
song.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
//jshint esversion:8
const axios = require("axios");
const fs = require('fs');
const path = require('path');
async function search(query) {
try {
const response = (
await axios.get(`https://jiosaavn-api.vercel.app/search?query=${query}`)
).data;
if (response.result === "false") {
throw "not-found";
} else {
let content = `*Results for* _'${query}'_\n\n`;
let songarray = [];
for (let i = 0; i < response.length; i++) {
content += `*${i + 1}.* ${response[i].title} - ${response[i].more_info.singers}\n`;
songarray.push({ key: i + 1, id: response[i].id });
}
content += `\nReply this message with \`\`\`!dldsong [number]\`\`\` to download !\n*Ex.* !dldsong 1`;
return { status: true, content, songarray };
}
} catch (error) {
return {
status: false,
content: `🙇♂️ *Error*\n\n` + "```Result not found for " + query + ", Please try again with different keyword!```",
songarray: []
};
}
}
async function download(songkey, id) {
let pretifiedsongkey = Number(songkey.trim());
try {
let saveddata = JSON.parse(fs.readFileSync(path.join(__dirname,`../cache/song~${id}.json`), "utf8"));
let song = saveddata.find(d => d.key === pretifiedsongkey);
if (song) {
try {
let data = (
await axios.get(`https://jiosaavn-api.vercel.app/song?id=${song.id}`)
).data;
return {
status: true,
content: {
text: `🎶 *${data.song}* _(${data.year})_\n\n📀 *Artist :* ` + "```" + data.singers + "```\n📚 *Album :* " + "```" + data.album + "```" + `\n\n*Download Url* 👇\nhttps://musicder-prod.vercel.app/download/${data.id}`,
image: await image(data.image)
}
};
} catch (w) {
return {
status: false,
content: `🙇♂️ *Error*\n\n` + "```Something went wrong while fetching this song.```",
};
}
}
else {
return {
status: false,
content: `🙇♂️ *Error*\n\n` + "```This song key is invalid please send the correct song key.\nEx. !dldsong 1```",
};
}
} catch (error) {
console.log(error);
return {
status: false,
content: `🙇♂️ *Error*\n\n` + "```Cache not found please search the song again```",
};
}
}
async function image(link) {
try {
let respoimage = await axios.get(link, { responseType: 'arraybuffer' });
return ({
mimetype: "image/jpeg",
data: Buffer.from(respoimage.data).toString('base64'),
filename: "jiosaavn"
});
} catch (error) {
console.log(error);
return {
mimetype: "image/jpeg",
data: '',
filename: "jiosaavn"
};
}
}
module.exports = {
search,
download
};