-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·143 lines (125 loc) · 4.77 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
#!/usr/bin/env node
'use strict';
const os = require('os');
const fs = require('fs');
const dns = require('dns');
const fse = require('fs-extra');
const got = require('got');
const logUpdate = require('log-update');
const ora = require('ora');
const chalk = require('chalk');
const unicode = require('unicodechar-string');
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
updateNotifier({pkg}).notify();
const spinner = ora();
const arg = process.argv[2];
const nex = process.argv[3];
const pre = chalk.cyan.bold('›');
const pos = chalk.red.bold('›');
const url = `https://www.instagram.com/${arg}`;
const story = `https://api.storiesig.com/stories/${arg}`;
const baseDir = `${os.homedir()}/.instafy/`;
const dir = `${baseDir}${arg}.txt`;
const rem = `${baseDir}${nex}.txt`;
if (!arg || arg === '-h' || arg === '--help') {
console.log(`
Keep your browser history clean!
${chalk.cyan('Usage')} : instafy <username> | [command] <username>
${chalk.cyan('Command')} :
-r, --remove remove a user from istafy list
-c, --clean clean instafy directory
-h, --help show help
${chalk.cyan('Example')} :
$ instafy 9gag
`);
process.exit(1);
}
if (arg === '-c' || arg === '--clean') {
fse.emptyDir(baseDir, err => {
if (err) {
process.exit(1);
} else {
console.log(`\n${pre} Cleaned!\n`);
process.exit(1);
}
});
}
if (arg === '-r' || arg === '--remove') {
if (!nex) {
logUpdate(`\n${pos} Provide a username!\n`);
process.exit(1);
}
if (nex && fs.existsSync(rem)) {
fs.unlinkSync(rem);
} else {
logUpdate(`\n${pos} Sorry! ${chalk.bold(nex)} has not been instafyed yet\n`);
process.exit(1);
}
logUpdate(`\n${pre} Removed ${nex} from the list!\n`);
}
if (arg !== '-c' && arg !== '--clear' && arg !== '-r' && arg !== '--remove' && !nex) {
logUpdate();
spinner.text = `${chalk.dim('Let the stalking begin!')}`;
spinner.start();
dns.lookup('instagram.com', err => {
if (err) {
spinner.stop();
logUpdate(`\n${pos} ${chalk.dim('Please check your internet connection!')}\n`);
} else {
logUpdate();
spinner.text = `${chalk.white('Checking for new')}${chalk.yellow(' posts!')}`;
spinner.start();
if (!fs.existsSync(dir)) {
fse.ensureFile(dir, err => {
if (err) {
process.exit(1);
} else {
logUpdate();
spinner.text = `${chalk.dim(`Instafying ${arg}`)}`;
got(url).then(res => {
const count = res.body.split(',"edge_owner_to_timeline_media":{"count":')[1].split(',"page_info":')[0];
spinner.stop();
logUpdate(`\n${pre} ${chalk.dim('Run')} ${chalk.green(`instafy ${arg}`)} ${chalk.dim('next time to get post notifications!')}\n`);
const buffer = Buffer.from(`${count}`);
const stream = fs.createWriteStream(dir);
stream.once('open', () => {
stream.write(buffer);
stream.end();
});
}).catch(err => {
if (err) {
spinner.stop();
logUpdate(`\n${pos} ${chalk.yellow(arg)} ${chalk.dim('is not an Instagram user.')}\n`);
fs.unlinkSync(dir);
}
});
}
});
}
if (fs.existsSync(dir)) {
got(url).then(res => {
const remotePosts = res.body.split(',"edge_owner_to_timeline_media":{"count":')[1].split(',"page_info"')[0];
const localPosts = fs.readFileSync(dir, 'utf-8');
const name = res.body.split(',"full_name":"')[1].split('",')[0] || `${arg}`;
const changeRemote = parseInt(remotePosts, 10);
const changeLocal = parseInt(localPosts, 10);
logUpdate();
spinner.text = `${chalk.white('Checking for new')}${chalk.cyan(' stories')}`;
got(story, {json: true}).then(res => {
const num = res.body.items.length;
// X === Y ? 'A' : X > Y ? 'B' : X < Y ? C : N => (optional)
changeRemote === changeLocal ? logUpdate(`\n ${chalk.keyword('orange')(' Notification')} \n\n ${chalk.cyan('› Post')} : No new post(s) by ${unicode(name)}\n\n ${chalk.cyan('› Story')} : ${num} stories! \n`) : changeRemote > changeLocal ? logUpdate(`\n${chalk.keyword('orange')(' Notification')} \n\n ${chalk.cyan('› Post')} : ${changeRemote - changeLocal} new post(s) by ${unicode(name)}\n\n ${chalk.cyan('› Story')} : ${num} stories! \n\n ${chalk.cyan('› Check at ')} : ${url} \n`) : changeRemote < changeLocal ? logUpdate(`\n ${chalk.keyword('orange')(' Notification')} \n\n ${chalk.cyan('› Post')} : ${unicode(name)} deleted ${changeLocal - changeRemote} post(s)!\n\n ${chalk.cyan('› Story')} : ${num} stories! \n`) : logUpdate(); // eslint-disable-line no-unused-expressions
spinner.stop();
});
const buffer = Buffer.from(`${remotePosts}`);
const stream = fs.createWriteStream(dir);
stream.once('open', () => {
stream.write(buffer);
stream.end();
});
});
}
}
});
}