-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (51 loc) · 1.92 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
"use strict";
const reddit = require('redwrap');
const ytdl = require('youtube-dl');
const chalk = require('chalk');
const fs = require('fs');
const shell = require('shelljs');
const userHome = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
const musicDir = `${userHome}/Music/`;
const subreddit = process.argv[2];
const targetDir = musicDir + subreddit;
const history = [];
let counter = 0;
console.log(`targetDir: ${targetDir} \n musicDir: ${musicDir}`);
shell.mkdir(targetDir);
reddit.r(`${subreddit}/top`).from('all').sort('top').limit(100).exe(function(err, data, res){
for (let post of data.data.children) {
counter++;
console.log(chalk.blue(`#${counter} ${post.data.title} ${post.data.domain}`));
if(post.data.domain === 'youtube.com') {
console.log(`Fixing to DL ${post.data.title} ${post.data.domain}`);
ytdl.exec(post.data.url, ['-x', '--audio-format', 'mp3'], {}, function(err, output) {
if (err) {
history[post.data.url] = {[post.data.title]: false};
console.log(chalk.red(err));
} else {
console.log(chalk.green(output.join('\n')));
history[post.data.url] = {[post.data.title]: true};
shell.mv('*.mp3', targetDir);
}
});
}
}
writeHistory(targetDir, history);
});
const moveFiles = (targetDir) => {
console.log(chalk.green.bold(`Moving the downloaded files to ${targetDir}`));
const exec = require('child_process').exec;
const currentDirectory = process.cwd();
const cmd = `mv ${currentDirectory}/*.mp3 ${targetDir}`;
console.log(chalk.green.bold(`Executing command: ${cmd}`));
exec(cmd, function(error, stdout, stderr) {
console.log(stdout);
console.log(stderror);
console.log(error);
});
}
const writeHistory = (targetDir, history) => {
fs.writeFile(`${targetDir}/history.json` , JSON.stringify(history), function (err) {
if (err) return console.log(chalk.red.bold(err));
});
}