-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·159 lines (135 loc) · 3.47 KB
/
cli.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env node
'use strict';
const os = require('os');
const fs = require('fs');
const dns = require('dns');
const https = require('https');
const fse = require('fs-extra');
const got = require('got');
const isURL = require('is-url');
const chalk = require('chalk');
const logUpdate = require('log-update');
const ora = require('ora');
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
updateNotifier({pkg}).notify();
const arg = process.argv[2];
const inf = process.argv[3];
const pre = chalk.cyan.bold('›');
const pos = chalk.red.bold('›');
const args = ['-p', '--profile', '-c', '--cover', '-g', '--gif'];
if (!arg || arg === '-h' || arg === '--help' || args.indexOf(arg) === -1) {
console.log(`
Complete Twitter media downloader!
${chalk.cyan('Usage :')} twiger <command> [username/link]
${chalk.cyan('Command :')}
-p, --profile Download profile picture of a twitter user.
-c, --cover Download cover picture of a twitter user.
-g, --gif Download gifs.
${chalk.cyan('Help :')}
$ twiger -p iamdevloper
$ twiger -g <url>
${chalk.dim('Images are saved in Twgier under your home directory!')}
`);
process.exit(1);
}
const spinner = ora();
const url = `https://twitter.com/${inf}`;
const checkConnection = () => {
dns.lookup('twitter.com', err => {
if (err) {
logUpdate(`\n${pos} ${chalk.dim('Please check your internet connection!')}\n`);
process.exit(1);
} else {
logUpdate();
spinner.text = 'Twiggering';
spinner.start();
}
});
};
const folder = `${os.homedir()}/Twiger/`;
fse.ensureDir(folder, err => {
if (err) {
process.exit(1);
}
});
const checkMessage = () => {
logUpdate();
spinner.text = 'Please wait';
spinner.start();
};
const downloadMessage = () => {
logUpdate();
spinner.text = 'Downloading Media!';
};
const errMessage = () => {
spinner.stop();
logUpdate(`\n${pos} ${chalk.dim('Invalid username/link')}\n`);
};
const showOnce = () => {
checkMessage();
checkConnection();
};
const download = (media, file) => {
file = file || '';
const fileName = Math.random().toString(15).substr(4, 5);
const save = fs.createWriteStream(`${folder}${fileName}.${file}`);
https.get(media, (res, cb) => {
spinner.start();
res.pipe(save);
save.on('finish', () => {
logUpdate(`\n${pre} Media Saved!\n`);
save.close(cb);
spinner.stop();
save.on('error', () => {
process.exit(1);
});
});
});
return;
};
if (arg === '-p' || arg === '--profile') {
showOnce();
got(url).then(res => {
downloadMessage();
const body = res.body;
const link = body.split('data-resolved-url-large="')[1].split('"')[0].trim();
const ext = link.split('.').pop();
download(link, ext);
}).catch(err => {
if (err) {
errMessage();
}
});
}
if (arg === '-c' || arg === '--cover') {
showOnce();
got(url).then(res => {
downloadMessage();
const body = res.body;
const link = body.split('1500x500')[0].split('src="')[2];
download(`${link}1500x500`, '.jpg');
}).catch(err => {
if (err) {
errMessage();
}
});
}
if (arg === '-g' || arg === '--gif') {
if (isURL(inf) === false) {
logUpdate(`\n${pos} ${chalk.dim('Please provide a valid url')}\n`);
process.exit(1);
}
showOnce();
got(inf).then(res => {
downloadMessage();
const gif = res.body;
const id = gif.split('tweet_video_thumb/')[1].split(`.jpg`)[0];
const url = `https://video.twimg.com/tweet_video/${id}.mp4`;
download(url, 'mp4');
}).catch(err => {
if (err) {
errMessage();
}
});
}