-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylist-download.js
85 lines (75 loc) · 2.51 KB
/
playlist-download.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
const ffmpeg = require('fluent-ffmpeg');
const ytdl = require('ytdl-core');
const search = require("youtube-sr").default;
const fs = require('fs');
const { Collection } = require('@discordjs/collection')
const collection = new Set();
const collection2 = new Collection();
const { google } = require('googleapis');
const path = require('path');
const downloaded = [];
const cannot = [ ]
const oauth2Client = new google.auth.OAuth2(
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URI
);
oauth2Client.setCredentials({ refresh_token: REFRESH_TOKEN });
const drive = google.drive({
version: 'v3',
auth: oauth2Client,
});
const all = [];
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const getAudio = (video) => new Promise((resolve, reject) => {
var stream = ytdl(video?.url, { filter: 'audioonly' });
var file = fs.createWriteStream(`./tmp/${video?.title.split("/").join(" ").split(".").join(" ")}.mp3`);
ffmpeg(stream)
.format('mp3')
.save(file)
.on('end', () => {
collection.add(`${video?.title}`);
resolve(`Done ${video?.title}`);
})
});
async function uploadFile(filePath, name) {
try {
const response = await drive.files.create({
requestBody: {
name: `${name}`,
},
media: {
body: fs.createReadStream(`${path.join(__dirname, filePath)}`),
},
});
return response.data;
} catch (error) {
console.log(error.message);
}
}
async function run() {
const dir = fs.readdirSync('./tmp').filter(file => file.endsWith('.mp3'));
for (i in dir) {
collection.add(dir[i].slice(0, -4));
downloaded.push(dir[i].slice(0, -4));
// await uploadFile(`./tmp/${dir[i]}`, `${dir[i].slice(0, -4)}.mp3`);
}
await wait(5000);
const data = (await search.getPlaylist("PLAYLIST_ID").then(playlist => playlist.fetch()));
if(!Array.isArray(data?.videos)) throw new Error("No videos found");
var videos = data.videos;
for(i in videos) {
all.push(videos[i].title);
collection2.set(videos[i].title, videos[i].url);
if(!collection.has(videos[i]?.title.split("/").join(" ").split(".").join(" "))) {
console.log(await getAudio(videos[i]));
downloaded.push(videos[i]?.title);
console.log(downloaded.length, videos.length, i)
}
}
console.log(all.length, downloaded.length);
};
run();
process.on('uncaughtException', async function (err) {
console.log(err)
});