-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·184 lines (164 loc) · 3.97 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env node
'use strict';
const os = require('os');
const dns = require('dns');
const fs = require('fs');
const fse = require('fs-extra');
const instory = require('instory');
const download = require('download');
const log = require('log-update');
const jsonfile = require('jsonfile');
const ora = require('ora');
const chalk = require('chalk');
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
updateNotifier({pkg}).notify();
const cbg = chalk.green.bold('›');
const cbr = chalk.red.bold('›');
const arg = process.argv[2];
const inf = process.argv[3];
const end = process.exit;
const dim = chalk.dim; // eslint-disable-line prefer-destructuring
const spinner = ora();
const dir = `${os.homedir()}/instagram/story_${arg}/`;
const file = `${dir}${arg}_${new Date().toLocaleString().substr(0, 10)}.json`;
if (!arg || arg === '-h' || arg === '--help') {
log(`
Usage: instory <username> [command]
Command:
-a, ${dim('--all')} Download all the stories
-i, ${dim('--images')} Download only images
-v, ${dim('--videos')} Download only videos
-c, ${dim('--check')} Check total number of stories
-e, ${dim('--export')} Export the downloadable urls
-r, ${dim('--remove')} Remove all the stories of a user
Help:
-h, ${dim('--help')} Display help
`);
end(1);
}
const connection = () => {
dns.lookup('storiesig.com', err => {
if (err) {
log(`\n${cbr} Please check your internet connection! \n`);
end(1);
} else {
log();
spinner.text = 'Please wait...';
spinner.start();
}
});
};
const mdir = () => {
if (!fs.existsSync(dir)) {
fse.ensureDir(dir, err => {
if (err) {
log(err);
end(1);
}
});
}
};
const downloadMessage = (storyCount, user) => {
spinner.text = ` ${chalk.white('Downloading')} ${chalk.keyword('yellow')(storyCount)} ${chalk.white('stories by')} ${chalk.keyword('orange')(user)}`;
};
const statusUpdate = () => {
log(`\n${cbg} Download Complete \n`);
spinner.stop();
};
const len = n => {
if (n.length === 0) {
log(`\n${cbr} No stories found! \n`);
fs.rmdir(dir);
end(1);
}
};
const handleError = () => {
log(`\n${cbr} ${chalk.yellow(arg)} is not an instagram user! \n`);
end(1);
};
const excludeDownload = opt => {
mdir();
connection();
if (opt) {
instory(arg, opt).then(res => {
len(res);
downloadMessage(`${res.length}`, arg);
Promise.all(res.map(x => download(x, dir))).then(() => {
statusUpdate();
});
}).catch(error => {
if (error) {
handleError();
}
});
} else {
instory(arg).then(res => {
len(`${res.story}`);
downloadMessage(`${res.story.length}`, arg);
Promise.all(res.story.map(x => download(x, dir))).then(() => {
statusUpdate();
});
}).catch(error => {
if (error) {
handleError();
}
});
}
};
if (inf === '-a' || inf === '--all') {
excludeDownload();
}
if (inf === '-i' || inf === '--images') {
excludeDownload('video');
}
if (inf === '-v' || inf === '--videos') {
excludeDownload('image');
}
if (inf === '-c' || inf === '--check') {
connection();
instory(arg).then(res => {
log(`\n${cbg} Total stories by ${chalk.white(arg)} : ${chalk.yellow(res.story.length)}\n`);
spinner.stop();
}).catch(error => {
if (error) {
handleError();
}
});
}
if (inf === '-e' || inf === '--export') {
connection();
instory(arg).then(res => {
fse.ensureFile(file, err => {
if (err) {
log(err);
end(1);
}
const obj = {story: res.story};
jsonfile.writeFile(file, obj, {spaces: 2}, err => {
if (err) {
log(err);
}
log(`\n${cbg} Saved! \n`);
spinner.stop();
});
});
}).catch(error => {
if (error) {
handleError();
}
});
}
if (inf === '-r' || inf === '--remove') {
if (!fs.existsSync(dir)) { // eslint-disable-line no-negated-condition
log(`\n${cbr} User not found!\n`);
end(1);
} else {
fse.remove(dir, err => {
if (err) {
log(err);
}
log(`\n${cbr} ${chalk.green(arg)}'s stories has been deleted! \n`);
});
}
}