-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·132 lines (110 loc) · 3.42 KB
/
cli.js
File metadata and controls
executable file
·132 lines (110 loc) · 3.42 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
#!/usr/bin/env node
'use strict';
const os = require('os');
const dns = require('dns');
const fs = require('fs');
const isUrl = require('is-url');
const got = require('got');
const download = require('download');
const ora = require('ora');
const jsf = require('jsonfile');
const log = require('log-update');
const mista = require('mista');
const chalk = require('chalk');
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
updateNotifier({pkg}).notify();
const spinner = ora();
const file = `${Math.random().toString(15).substr(2, 5)}.json`;
const red = chalk.red.bold('›');
const end = process.exit;
const arg = process.argv[2];
const inf = process.argv[3];
const pth = process.argv[4];
let dir = `${process.argv[5]}/instagram`;
if (!pth) {
dir = `${os.homedir()}/instagram`;
}
const dim = str => {
return chalk.dim(str);
};
const cyn = chalk.cyan.bold('✓');
if (!arg || arg === '-h' || arg === '--help') {
log(`
Usage: mig ${chalk.green('<command>')} ${chalk.yellow('[url]')} ${chalk.blue('<opt>')}
Command:
-d, ${dim('--download')} Download all the media
-e, ${dim('--export')} Export links in as JSON
Option:
-p, ${dim('--path')} Specify path of the folder for files
Help:
-h, ${dim('--help')} Show help
`);
end(1);
}
const checkConnectionStatus = () => {
dns.lookup('instagram.com', err => {
if (err) {
log(`\n${red} ${dim('Please check your internet connection! ')} \n`);
end(1);
} else {
log();
spinner.text = 'Connecting...';
}
});
};
const displayMessage = () => {
log(`\n${red} ${dim('The given url does not contain multiple images/videos!')} \n`);
end(1);
};
const handleUrlError = () => {
return isUrl(inf) === false ? `${log(`\n${red} ${dim('Please provide a valid url!')} \n`)}${end(1)}` : `${log()}${spinner.text = 'Hold on, boi!'}${spinner.start()}`; // eslint-disable-line no-return-assign
};
inf === undefined ? `${log(`\n${red} ${dim('Please provide a url to download data!')} \n`)}${end(1)}` : handleUrlError(); // eslint-disable-line no-unused-expressions
const returnValidUrl = getUrl => {
return getUrl.split('?')[0] + '?__a=1';
};
if (arg === '-d' || arg === '--download') {
checkConnectionStatus();
const url = returnValidUrl(inf);
// You can simplify it using mista()
got(url, {json: true}).then(res => {
const base = res.body.graphql.shortcode_media.edge_sidecar_to_children.edges;
const store = {data: []};
for (let i = 0; i < base.length; i++) {
const vid = base[i].node.video_url;
const img = base[i].node.display_resources[2].src;
vid === undefined ? store.data.push(img) : store.data.push(vid); // eslint-disable-line no-unused-expressions
}
const arr = store.data;
log();
spinner.text = 'Downloading files...';
Promise.all(arr.map(x => download(x, dir))).then(() => {
log(`\n${cyn} Download Complete! \n\n${cyn} ${arr.length} files saved in ${chalk.blue(dir)} \n`);
spinner.stop();
});
}).catch(err => {
if (err) {
displayMessage();
}
});
}
if (arg === '-e' || arg === '--export') {
checkConnectionStatus();
mista(inf).then(res => {
log(`\n${cyn} Links Exported!\n\n${cyn} File saved as ${chalk.yellow(`${file}`)} in ${chalk.blue(dir)} \n`);
spinner.stop();
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
const dest = `${dir}/${file}`;
jsf.writeFile(dest, res, {spaces: 2}, err => {
end(1);
log(err);
});
}).catch(err => {
if (err) {
displayMessage();
}
});
}