-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·139 lines (114 loc) · 3.52 KB
/
cli.js
File metadata and controls
executable file
·139 lines (114 loc) · 3.52 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env node
'use strict';
const dns = require('dns');
const isUrl = require('is-url');
const got = require('got');
const ora = require('ora');
const jsonFile = require('jsonfile');
const chalk = require('chalk');
const cheerio = require('cheerio');
const logUpdate = require('log-update');
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
updateNotifier({pkg}).notify();
const spinner = ora();
const arg = process.argv[2];
const inf = process.argv[3];
const val = process.argv[4];
const setName = `${Math.random().toString(16).substr(10)}.json`;
let rad = `${process.argv[5]}.json` || setName;
if (rad === 'undefined.json') {
rad = setName;
}
const log = console.log;
const end = process.exit;
const url = 'https://youtube.com/watch?v=';
const checkConnection = () => {
dns.lookup('youtube.com', err => {
if (err) {
logUpdate(`\n${chalk.red(' ✖ ')} ${chalk.dim('Please check your internet connection!')}\n`);
end(1);
} else {
logUpdate();
spinner.text = 'Puffing...';
spinner.start();
}
});
};
if (!arg || arg === '-h' || arg === '--help') {
log(`
${chalk.keyword('orange')('⚡⚡')} ${chalk.blue('Youtube Playlist Link Fetcher')} ${chalk.keyword('orange')('⚡⚡')}
Usage: puf <commands> [url]
puf <commands> [url] ${chalk.dim('<command>')}
Commands:
-f, ${chalk.dim('--fetch')} Fetch url of items in the playlist
-e, ${chalk.dim('--export')} Export urls into json
Extra:
${chalk.dim('-f')} url --name Show links along with the title
${chalk.dim('-e')} url --name <name> Set desired name of the exported playlist
Help:
$ puf -f ${chalk.dim('https://goo.gl/QcSugM')} --name
$ puf -e ${chalk.dim('https://goo.gl/QcSugM')} --name course
`);
end(1);
}
if (!inf || isUrl(inf) === false) {
log(`\n ${chalk.red('✖')} Things don't work this way. Provide a valid url\n\n ${chalk.blue('✔')} ${chalk.dim('Type')} ${chalk.cyan('$ puf --help')} ${chalk.dim('for more help')}\n`);
end(1);
}
if (arg === '-f' || arg === '--fetch') {
checkConnection();
got(inf).then(res => {
const $ = cheerio.load(res.body);
const thumb = $('tr');
logUpdate();
if ((arg === '-f' || arg === '--fetch') && val === '--name') {
$(thumb).each((i, links) => {
const sources = `${url}${$(links).attr('data-video-id')}`;
const names = $(links).attr('data-title');
log(` ${chalk.bold.blue('⚡⚡')} ${chalk.green(names)}`);
log(` ${chalk.yellow('⏩ ')} ${sources} \n`);
spinner.stop();
});
} else {
$(thumb).each((i, links) => {
const sources = `${url}${$(links).attr('data-video-id')}`;
log(` ${chalk.yellow('⏩ ')} ${sources} \n`);
spinner.stop();
});
}
}).catch(err => {
logUpdate(`\n${err}\n`);
end(1);
});
}
if (arg === '-e' || arg === '--export') {
checkConnection();
got(inf).then(res => {
const $ = cheerio.load(res.body);
const tr = $('tr');
logUpdate(`\n ${chalk.blue('🍁')} Done! Playlist exported as : \n\n ${chalk.green('🌴')} ${chalk.yellow(rad)} into ${chalk.blue(process.cwd())}\n`);
for (let i = 0; i < tr.length; i++) {
const obj = {
playlist: []
};
for (let j = 0; j <= i; j++) {
obj.playlist.push({
id: tr.eq(j).attr('data-video-id'),
url: url + tr.eq(j).attr('data-video-id'),
name: tr.eq(j).attr('data-title')
});
}
jsonFile.writeFile(rad, obj, {spaces: 2}, err => {
end(1);
log(err);
});
}
spinner.stop();
}).catch(err => {
if (err) {
logUpdate(`${err}`);
end(1);
}
});
}